fix(utils): emit multi-type tool params as JSON array, not a comma-joined string#695
Open
anxkhn wants to merge 1 commit into
Open
fix(utils): emit multi-type tool params as JSON array, not a comma-joined string#695anxkhn wants to merge 1 commit into
anxkhn wants to merge 1 commit into
Conversation
convert_function_to_tool serialized a parameter with a union type (e.g. Union[int, str]) as the comma-joined string "integer, string" instead of the JSON-Schema array ["integer", "string"]. The server's PropertyType.UnmarshalJSON reads that as a single, invalid type name, so the parameter's type constraint is silently dropped. Because types is a set, the joined string order also varied between runs. Emit a single type as a bare string (unchanged) and multiple types as a sorted list, which matches Tool.Function.Parameters.Property.type (Union[str, Sequence[str]]) and the array form the server expects. Sorting removes the non-determinism. Update test_function_with_all_types to assert the multi-type property is the array ['integer', 'string'] rather than parsing a string. Signed-off-by: Anas Khan <83116240+anxkhn@users.noreply.github.com>
vaderollama
approved these changes
Jul 14, 2026
vaderollama
left a comment
There was a problem hiding this comment.
LGTM — fixes a real bug where multi-type tool params (e.g. int | str) were serialized as the comma-joined string 'integer, string' instead of the JSON-Schema array ['integer', 'string']. The server parses a string type as a single type name, so the multi-type constraint was silently dropped. Using sorted() for determinism is a nice touch. Tests updated to assert the array format.
@ParthSareen — ready to merge.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
convert_function_to_toolserializes a tool parameter that has more than one type(for example
int | str) as the comma-joined string'integer, string'instead ofthe JSON-Schema array
['integer', 'string'].Why this is a bug
The generated
typestring is not valid JSON Schema. The Ollama server parses aparameter
typethat is a JSON string as a single type name, so'integer, string'is read as one (nonexistent) type and the multi-type constraint is silently dropped.
The array form is the shape the schema expects:
Tool.Function.Parameters.Property.typeis
Optional[Union[str, Sequence[str]]](ollama/_types.py), and an array is alreadyused as the canonical multi-type input elsewhere in the tests
(
tests/test_client.py,'type': ['integer', 'number']).There is also a non-determinism bug:
typesis built as aset, so the samefunction could serialize to
'integer, string'on one run and'string, integer'on the next.
Fix
In
ollama/_utils.py, sort thetypesset and emit:Sorting also makes the output deterministic. The empty-
typesedge case (aparameter typed only
None) keeps its prior''output via''.join([]).Tests
Updated
tests/test_utils.py::test_function_with_all_typesso the multi-typeparameter (
v: int | str | Noneon Python >= 3.10) is asserted to be a JSON array:This replaces the previous fragile string-parsing assertion (which stripped
brackets and split on commas). The new assertion fails on the old comma-joined
string and passes with the fix.
Verified locally:
uv run pytest ollama tests -q-> 97 passed (doctests included)uvx hatch fmt --check -f(format) anduvx hatch fmt --check -l(lint) -> passuv lock --check-> ok;uv exportvsrequirements.txt-> no diffSingle-type parameters (e.g.
x: int->'integer') are unaffected.