fix(genai): route response_schema directly to GenerateContentConfig#1611
Open
Phạm Gia Linh (phamgialinhlx) wants to merge 1 commit intolangchain-ai:mainfrom
Open
fix(genai): route response_schema directly to GenerateContentConfig#1611Phạm Gia Linh (phamgialinhlx) wants to merge 1 commit intolangchain-ai:mainfrom
Phạm Gia Linh (phamgialinhlx) wants to merge 1 commit intolangchain-ai:mainfrom
Conversation
Fixes structured output with $defs/$ref schemas on Vertex AI. The GenerationConfig Schema type rejects these fields, so response_schema now bypasses it and goes directly to GenerateContentConfig where the SDK's process_schema() pipeline can inline them correctly.
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
When using
with_structured_outputwith Pydantic models that produce$defs/$refin their JSON schema (e.g., models withOptionalfields,Uniontypes, or recursive structures), Vertex AI silentlyreturns empty arrays instead of valid structured output.
The root cause is that
response_schemawas being passed throughGenerationConfig, whose strictSchematype rejects$defs/$reffields. This fix routesresponse_schemadirectly toGenerateContentConfig, bypassingGenerationConfig, so the SDK'sprocess_schema()pipeline can inline$defs/$refbefore sending the request to Vertex AI.AI agents were used to assist with this contribution.
Relevant issues
with_structured_output()returns empty nested lists on Vertex AI due to responseJsonSchema #1608Type
🐛 Bug Fix
Changes(optional)
with_structured_outputnow binds viaresponse_schemainstead ofresponse_json_schema_prepare_requestextractsresponse_schemafrom kwargs before_prepare_paramsand passes it directly to_build_request_config_add_response_paramsno longer handlesresponse_schema(onlyresponse_json_schema)_build_request_configaccepts an optionalresponse_schemaand forwards it toGenerateContentConfigTesting(optional)
Updated unit tests in
test_chat_models.py:test_response_json_schema_param_mapping: verifiesresponse_schemabypassesGenerationConfigand appears onGenerateContentConfigtest_ref_preservation: updated to checkresponse_schemakeytest_union_schema_support: updated to checkresponse_schemakeytest_response_schema_mime_type_validation: validation now tested via_prepare_requestNote(optional)
This is backwards compatible. The
response_json_schemakwarg continues to work as before throughGenerationConfig. Onlyresponse_schemaand thewith_structured_outputcode path are affected.