Skip to content

fix(afc): avoid assigning type: object to optional scalar parameters in parameters_json_schema - #3074

Open
chauvuusvn wants to merge 1 commit into
googleapis:mainfrom
chauvuusvn:fix/afc-optional-scalar-parameters-json-schema
Open

chauvuusvn wants to merge 1 commit into
googleapis:mainfrom
chauvuusvn:fix/afc-optional-scalar-parameters-json-schema

Conversation

@chauvuusvn

Copy link
Copy Markdown

Description

Fixes #3060

When defining tool functions with optional scalar parameters (e.g. start_line: int | None = None or param: Optional[str] = None), pydantic.TypeAdapter.json_schema() generates an anyOf schema:

{
  "anyOf": [
    {"type": "integer"},
    {"type": "null"}
  ]
}

In google/genai/_automatic_function_calling_util.py, parse_function_declaration_json_schema unconditionally assigned json_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

  • Updated parse_function_declaration_json_schema to only assign 'type': 'object' if all non-null subschemas within 'anyOf' are objects or $ref schemas.
  • Preserves 'type': 'object' for Pydantic model unions (Union[ModelA, ModelB], Optional[ModelA]).
  • Added comprehensive unit tests in google/genai/tests/afc/test_parse_function_declaration_json_schema.py verifying both optional scalar parameters and model unions.

Test Evidence

Ran pytest on targeted test suite:

google/genai/tests/afc/test_parse_function_declaration_json_schema.py::test_optional_scalar_parameters_do_not_contain_type_object PASSED
google/genai/tests/afc/test_parse_function_declaration_json_schema.py::test_model_unions_retain_type_object PASSED

Full suite passed: 97 passed.

…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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:XL Code changes > 100 lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Callable tools: optional scalar parameters (int | None) get "type": "object" in parameters_json_schema, so Gemini sends {}

2 participants