Repository navigation
fix: emit additional_properties with value type schema in dict branch - #5052
weiguangli-io wants to merge 1 commit into
Conversation
…parameter
The dict origin branch in _parse_schema_from_parameter previously returned
Schema(type=OBJECT) without any value type information for parameterized
dict types like dict[str, str]. This caused Gemini to receive a bare
{"type": "object"} with no constraints, leading to empty tool arguments
and infinite retry loops when combined with output_schema.
Now the dict branch parses the value type argument and sets
additional_properties with the corresponding schema, so dict[str, V]
correctly emits {"type": "object", "additionalProperties": <V schema>}.
Fixes google#4868
|
Hi @weiguangli-io , Thank you for your contribution! We appreciate you taking the time to submit this pull request. |
|
@rohityan Thanks for the thorough review! I've investigated and here's the analysis:
These are independent paths. This PR adds Test evidence:
The behavior is consistent with how |
|
Thank you @weiguangli-io for your contribution! 🎉 Your changes have been successfully imported and merged via Copybara in commit 93f57f4. Closing this PR as the changes are now in the main branch. |
Merge google#5052 Fixes google#4868 PiperOrigin-RevId: 960118193
Summary
Fixes #4868
The
dictorigin branch in_parse_schema_from_parameterpreviously returnedSchema(type=OBJECT)without any value type information for parameterized dict types likedict[str, str]. This caused Gemini to receive a bare{"type": "object"}with no field constraints, leading to:[{}])output_schemaChanges
_function_parameter_parse_util.py: When the dict type has 2 type args (key and value), parse the value type and setadditional_propertieson the schema. For example,dict[str, str]now emits{"type": "OBJECT", "additionalProperties": {"type": "STRING"}}instead of just{"type": "OBJECT"}.test_build_function_declaration.py: Added tests fordict[str, str],dict[str, int], andlist[dict[str, str]]to verifyadditional_propertiesis correctly set. Updated existingtest_listto also assert onadditional_propertiesfor the dict items.How it works
The fix follows the same pattern used by the
listbranch, which recursively calls_parse_schema_from_parameteron the element type to buildschema.items. Similarly, the dict branch now recursively calls_parse_schema_from_parameteron the value type (args[1]) to buildschema.additional_properties.Test plan
test_build_function_declaration.py)dict[str, str],dict[str, int], andlist[dict[str, str]]