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

Author: betacatslingCreated Sep 14, 2026Updated Sep 14, 2026

Actual behavior

Serializing a ChatRequest whose tools lack parameters (or lack function entirely) raises PydanticSerializationError unless exclude_none=True is passed:

python
from ollama._types import ChatRequest, Tool

ChatRequest(model='m', tools=[Tool(function=Tool.Function(name='f'))]).model_dump()
# PydanticSerializationError: Error calling function `serialize_model`:
# TypeError: argument of type 'NoneType' is not iterable

ChatRequest(model='m', tools=[Tool()]).model_dump_json()
# same error

The wrap serializer in ChatRequest evaluates 'parameters' in tool['function'] and 'defs' in tool['function']['parameters'], which fails when function or parameters serialized as None.

Expected behavior

model_dump() / model_dump_json() succeed regardless of whether exclude_none is set; the defs$defs rename still applies when parameters is present.

Environment

  • ollama-python: main @ fa8509936be08809bd15ca90a371165eb9abeb50
  • pydantic 2.10.6, Python 3.11