Skip to content

fix: make ChatRequest serializer None-safe for tools without parameters or function - #736

Open
betacatsling wants to merge 1 commit into
ollama:mainfrom
betacatsling:fix/chat-request-serializer-none-safe
Open

betacatsling wants to merge 1 commit into
ollama:mainfrom
betacatsling:fix/chat-request-serializer-none-safe

Conversation

@betacatsling

Copy link
Copy Markdown

Summary

Fixes #733.

ChatRequest.serialize_model assumes every serialized tool has a non-None function containing a non-None parameters mapping. When a Tool is created without function, or a Tool.Function is created without parameters, model_dump() (and model_dump_json()) without exclude_none=True raises PydanticSerializationError: TypeError: argument of type 'NoneType' is not iterable because the serializer evaluates 'parameters' in None / 'defs' in None.

This makes the serializer tolerate missing function/parameters so request objects can be dumped freely (e.g. for logging or debugging) regardless of how the tools were constructed. The defs$defs rename behavior is unchanged when parameters is present.

Validation

  • New regression tests in tests/test_type_serialization.py:
    • test_chat_request_serialization_tool_without_parameters
    • test_chat_request_serialization_tool_without_function
    • test_chat_request_serialization_tool_defs_alias
  • Before this change: the first two tests fail with PydanticSerializationError: Error calling function 'serialize_model': TypeError: argument of type 'NoneType' is not iterable.
  • After: pytest tests/test_type_serialization.py — 17 passed; full suite pytest tests/ — 100 passed.
  • ruff check and ruff format --check pass on changed files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ChatRequest.model_dump() crashes when a tool has no parameters or no function

1 participant