Skip to content

utils: serialize union tool parameter types as a JSON Schema type array - #785

Open
AP24110011695 wants to merge 1 commit into
ollama:mainfrom
AP24110011695:fix/tool-union-type-schema
Open

AP24110011695 wants to merge 1 commit into
ollama:mainfrom
AP24110011695:fix/tool-union-type-schema

Conversation

@AP24110011695

Copy link
Copy Markdown

Problem

convert_function_to_tool builds each property's type by joining the parameter's types with commas:

def lookup(key: int | str): ...

currently produces:

{"type": "string, integer"}

"string, integer" is not a valid JSON Schema type (it must be a single string or an array of strings), so models receive a schema no validator accepts. The value is also nondeterministic - the types come from a set, so the join order varies per run.

Fix

Tool.Function.Parameters.Property.type is already typed Union[str, Sequence[str]], so unions are emitted as a sorted type array (["integer", "string"]); single types (including Optional after the null is discarded) stay scalar strings. The sorting makes output deterministic.

Tests

test_convert_function_to_tool_union_types fails on main (35d9667) and passes with this change; test_convert_function_to_tool_optional_type_stays_scalar pins the existing Optional behavior.

convert_function_to_tool joined a parameter's multiple types with
commas ('string, integer'), which is not a valid JSON Schema type and
nondeterministic (set iteration order). Property.type already accepts a
sequence of strings, so emit a sorted type array for unions and keep
the scalar string for single types.
@AP24110011695

Copy link
Copy Markdown
Author

Also updated the pre-existing union assertion in test_convert_function_to_tool — it compared the old comma-joined string through a set-comprehension workaround; it now asserts the sorted type array directly.

@AP24110011695
AP24110011695 force-pushed the fix/tool-union-type-schema branch from 2de2356 to 5fbb8f3 Compare November 1, 2026 12:06

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.

1 participant