Skip to content

Comments

fix(genai): route response_schema directly to GenerateContentConfig#1611

Open
Phạm Gia Linh (phamgialinhlx) wants to merge 1 commit intolangchain-ai:mainfrom
phamgialinhlx:main
Open

fix(genai): route response_schema directly to GenerateContentConfig#1611
Phạm Gia Linh (phamgialinhlx) wants to merge 1 commit intolangchain-ai:mainfrom
phamgialinhlx:main

Conversation

@phamgialinhlx
Copy link

@phamgialinhlx Phạm Gia Linh (phamgialinhlx) commented Feb 24, 2026

Description

When using with_structured_output with Pydantic models that produce $defs/$ref in their JSON schema (e.g., models with Optional fields, Union types, or recursive structures), Vertex AI silently
returns empty arrays instead of valid structured output.

The root cause is that response_schema was being passed through GenerationConfig, whose strict Schema type rejects $defs/$ref fields. This fix routes response_schema directly to
GenerateContentConfig, bypassing GenerationConfig, so the SDK's process_schema() pipeline can inline $defs/$ref before sending the request to Vertex AI.

AI agents were used to assist with this contribution.

Relevant issues

Type

🐛 Bug Fix

Changes(optional)

  • with_structured_output now binds via response_schema instead of response_json_schema
  • _prepare_request extracts response_schema from kwargs before _prepare_params and passes it directly to _build_request_config
  • _add_response_params no longer handles response_schema (only response_json_schema)
  • _build_request_config accepts an optional response_schema and forwards it to GenerateContentConfig

Testing(optional)

Updated unit tests in test_chat_models.py:

  • test_response_json_schema_param_mapping: verifies response_schema bypasses GenerationConfig and appears on GenerateContentConfig
  • test_ref_preservation: updated to check response_schema key
  • test_union_schema_support: updated to check response_schema key
  • test_response_schema_mime_type_validation: validation now tested via _prepare_request

Note(optional)

This is backwards compatible. The response_json_schema kwarg continues to work as before through GenerationConfig. Only response_schema and the with_structured_output code path are affected.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant