Repository navigation
fix(gemini): normalize response_schema on native generateContent #27775
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
f9f861d
7b4e749
14a64ad
d007005
c81dea4
dac62e5
49f601e
9b8474c
9e52261
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -335,6 +335,170 @@ def test_transform_generate_content_request_system_instruction_with_tools(): | |
| assert result["model"] == "gemini-3-flash-preview" | ||
|
|
||
|
|
||
| def test_transform_generate_content_request_normalizes_response_schema_2_5(): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You wrote that chat completion already handles this. So if we have a method there as well, can we have a test which makes sure that if any one of the 2 path's method changes, this test fails, so that we make sure that both paths are always updated with the schema changes. See if this is possible somehow |
||
| """For Gemini 2.0+, ``response_schema`` with ``$defs``/``$ref`` should be | ||
| promoted to ``responseJsonSchema`` (which Gemini 2.0+ accepts natively), | ||
| not forwarded as ``responseSchema`` (which rejects ``$defs``).""" | ||
| config = GoogleGenAIConfig() | ||
|
|
||
| schema = { | ||
| "$defs": { | ||
| "Highlight": { | ||
| "type": "object", | ||
| "properties": { | ||
| "title": {"type": "string"}, | ||
| "detail": {"type": "string"}, | ||
| }, | ||
| "required": ["title"], | ||
| } | ||
| }, | ||
| "type": "object", | ||
| "properties": { | ||
| "park_name": {"type": "string"}, | ||
| "highlights": { | ||
| "type": "array", | ||
| "items": {"$ref": "#/$defs/Highlight"}, | ||
| }, | ||
| }, | ||
| "required": ["park_name", "highlights"], | ||
| } | ||
|
|
||
| result = config.transform_generate_content_request( | ||
| model="gemini-2.5-flash-lite", | ||
| contents=[{"role": "user", "parts": [{"text": "hi"}]}], | ||
| tools=None, | ||
| generate_content_config_dict={ | ||
| "responseMimeType": "application/json", | ||
| "responseSchema": schema, | ||
| }, | ||
| system_instruction=None, | ||
| ) | ||
|
|
||
| gen_config = result["generationConfig"] | ||
| assert "responseSchema" not in gen_config | ||
| assert "responseJsonSchema" in gen_config | ||
| normalized = gen_config["responseJsonSchema"] | ||
| assert "$defs" in normalized | ||
| assert normalized["properties"]["highlights"]["items"] == { | ||
| "$ref": "#/$defs/Highlight" | ||
| } | ||
|
|
||
|
|
||
| def test_transform_generate_content_request_flattens_response_schema_1_5(): | ||
| """For Gemini 1.5, ``responseSchema`` is kept but flattened via | ||
| ``_build_vertex_schema`` so ``$defs``/``$ref`` are unpacked.""" | ||
| config = GoogleGenAIConfig() | ||
|
|
||
| schema = { | ||
| "$defs": { | ||
| "Highlight": { | ||
| "type": "object", | ||
| "properties": {"title": {"type": "string"}}, | ||
| "required": ["title"], | ||
| } | ||
| }, | ||
| "type": "object", | ||
| "properties": { | ||
| "highlights": { | ||
| "type": "array", | ||
| "items": {"$ref": "#/$defs/Highlight"}, | ||
| } | ||
| }, | ||
| "required": ["highlights"], | ||
| } | ||
|
|
||
| result = config.transform_generate_content_request( | ||
| model="gemini-1.5-pro", | ||
| contents=[{"role": "user", "parts": [{"text": "hi"}]}], | ||
| tools=None, | ||
| generate_content_config_dict={"responseSchema": schema}, | ||
| system_instruction=None, | ||
| ) | ||
|
|
||
| gen_config = result["generationConfig"] | ||
| assert "responseSchema" in gen_config | ||
| assert "responseJsonSchema" not in gen_config | ||
| normalized = gen_config["responseSchema"] | ||
| assert "$defs" not in normalized | ||
| items = normalized["properties"]["highlights"]["items"] | ||
| assert "$ref" not in items | ||
| assert "title" in items["properties"] | ||
| assert items["properties"]["title"]["type"].lower() == "string" | ||
|
|
||
|
|
||
| def test_transform_generate_content_request_passes_through_response_json_schema(): | ||
| """If the caller already used ``responseJsonSchema``, it should be | ||
| preserved (Gemini 2.0+ accepts standard JSON Schema as-is).""" | ||
| config = GoogleGenAIConfig() | ||
|
|
||
| schema = { | ||
| "type": "object", | ||
| "properties": {"name": {"type": "string"}}, | ||
| "required": ["name"], | ||
| } | ||
|
|
||
| result = config.transform_generate_content_request( | ||
| model="gemini-2.5-flash-lite", | ||
| contents=[{"role": "user", "parts": [{"text": "hi"}]}], | ||
| tools=None, | ||
| generate_content_config_dict={"responseJsonSchema": schema}, | ||
| system_instruction=None, | ||
| ) | ||
|
|
||
| gen_config = result["generationConfig"] | ||
| assert gen_config["responseJsonSchema"] == schema | ||
| assert "responseSchema" not in gen_config | ||
|
|
||
|
|
||
| def test_transform_generate_content_request_preserves_response_json_schema_when_response_schema_co_present(): | ||
| """When both ``responseJsonSchema`` and ``responseSchema`` are supplied on | ||
| Gemini 2.0+, the caller's ``responseJsonSchema`` must win — the | ||
| ``responseSchema`` value must not clobber it.""" | ||
| config = GoogleGenAIConfig() | ||
|
|
||
| caller_json_schema = { | ||
| "type": "object", | ||
| "properties": {"answer": {"type": "string"}}, | ||
| "required": ["answer"], | ||
| } | ||
| redundant_response_schema = { | ||
| "type": "object", | ||
| "properties": {"other": {"type": "string"}}, | ||
| } | ||
|
|
||
| result = config.transform_generate_content_request( | ||
| model="gemini-2.5-flash-lite", | ||
| contents=[{"role": "user", "parts": [{"text": "hi"}]}], | ||
| tools=None, | ||
| generate_content_config_dict={ | ||
| "responseJsonSchema": caller_json_schema, | ||
| "responseSchema": redundant_response_schema, | ||
| }, | ||
| system_instruction=None, | ||
| ) | ||
|
|
||
| gen_config = result["generationConfig"] | ||
| assert "responseSchema" not in gen_config | ||
| assert gen_config["responseJsonSchema"] == caller_json_schema | ||
|
|
||
|
|
||
| def test_transform_generate_content_request_without_schema_unchanged(): | ||
| """No schema in config → no normalization side effects.""" | ||
| config = GoogleGenAIConfig() | ||
|
|
||
| result = config.transform_generate_content_request( | ||
| model="gemini-2.5-flash-lite", | ||
| contents=[{"role": "user", "parts": [{"text": "hi"}]}], | ||
| tools=None, | ||
| generate_content_config_dict={"temperature": 0.7}, | ||
| system_instruction=None, | ||
| ) | ||
|
|
||
| assert result["generationConfig"]["temperature"] == 0.7 | ||
| assert "responseSchema" not in result["generationConfig"] | ||
| assert "responseJsonSchema" not in result["generationConfig"] | ||
|
|
||
|
|
||
| def test_validate_environment_with_dict_api_key(): | ||
| """ | ||
| Test that validate_environment correctly handles api_key as a dict. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.