fix: emit additional_properties with value type schema in dict branch#5052
Open
weiguangli-io wants to merge 1 commit intogoogle:mainfrom
Open
fix: emit additional_properties with value type schema in dict branch#5052weiguangli-io wants to merge 1 commit intogoogle:mainfrom
weiguangli-io wants to merge 1 commit intogoogle:mainfrom
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
Collaborator
|
Hi @weiguangli-io , Thank you for your contribution! We appreciate you taking the time to submit this pull request. |
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.
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]]