Repository navigation
fix(afc): avoid assigning type: object to optional scalar parameters in parameters_json_schema - #3074
Open
chauvuusvn wants to merge 1 commit into
Conversation
…parameters_json_schema - Only set type: object when all non-null subschemas in anyOf are objects or refs - Prevents scalar unions (e.g. int | None, str | None) from receiving type: object - Adds unit tests for optional scalar parameters and model unions (fixes googleapis#3060)
This branch has not been deployed
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.
Description
Fixes #3060
When defining tool functions with optional scalar parameters (e.g.
start_line: int | None = Noneorparam: Optional[str] = None),pydantic.TypeAdapter.json_schema()generates ananyOfschema:{ "anyOf": [ {"type": "integer"}, {"type": "null"} ] }In
google/genai/_automatic_function_calling_util.py,parse_function_declaration_json_schemaunconditionally assignedjson_schema_dict['type'] = 'object'whenever'anyOf'was present without a top-level'type'. This caused scalar unions to receive'type': 'object', leading Gemini models to generate{}instead of scalar values /None.Changes
parse_function_declaration_json_schemato only assign'type': 'object'if all non-null subschemas within'anyOf'are objects or$refschemas.'type': 'object'for Pydantic model unions (Union[ModelA, ModelB],Optional[ModelA]).google/genai/tests/afc/test_parse_function_declaration_json_schema.pyverifying both optional scalar parameters and model unions.Test Evidence
Ran pytest on targeted test suite:
Full suite passed: 97 passed.