Skip to content

types: serialize Tool parameters $defs by alias when dumped directly - #789

Open
suhailult777 wants to merge 1 commit into
ollama:mainfrom
suhailult777:fix-787
Open

suhailult777 wants to merge 1 commit into
ollama:mainfrom
suhailult777:fix-787

Conversation

@suhailult777

Copy link
Copy Markdown

Fixes #787

Tool.Function.Parameters stores the JSON Schema $defs key in the field defs. Only ChatRequest's serializer renamed it back, so dumping a Tool on its own (Tool.model_dump() / model_dump_json()) emitted defs, and $ref: '#/$defs/...' targets pointed at a missing key.

This adds a wrap model_serializer on Parameters that renames defs to $defs, so every dump path emits $defs. It keeps the pydantic>=2.9 floor (no serialize_by_alias).

  • Two existing asserts in tests/test_utils.py pinned the old 'defs': None key; they now expect '$defs': None.
  • New regression test in tests/test_type_serialization.py: direct dump, JSON dump, same output as the ChatRequest path, round trip back into Tool.
  • ChatRequest's rename is now a no-op but is left as-is, to keep this PR focused (ChatRequest.model_dump() crashes when a tool has no parameters or no function #733 touches that code).

Tests: pytest tests 118 passed; ruff check / ruff format --check clean.

I used AI assistance and reviewed and tested every line.

Tool.Function.Parameters stored the JSON Schema `$defs` key under the
field name `defs`. Only ChatRequest's serializer renamed it back, so
Tool.model_dump() / model_dump_json() emitted `defs` and broke
`$ref: '#/$defs/...'` targets. Rename it in a wrap serializer on
Parameters itself so every dump path produces `$defs`.

Fixes ollama#787

This branch has not been deployed

No deployments
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.

Tool.model_dump() serializes the $defs parameter alias as 'defs' (nested-in-request dumps do not)

1 participant