test(gemini): add parity test between native and chat schema normalization - #27814
Closed
mateo-berri wants to merge 7 commits into
Closed
test(gemini): add parity test between native and chat schema normalization#27814mateo-berri wants to merge 7 commits into
mateo-berri wants to merge 7 commits into
Conversation
The /v1beta/models/{model}:generateContent passthrough forwarded
generationConfig.response_schema verbatim, so schemas containing $defs,
$ref, anyOf-with-null, default, or title were rejected by Gemini even
though /chat/completions already handles them.
GoogleGenAIConfig.transform_generate_content_request now calls a new
_normalize_response_schema helper that mirrors the chat/completions
path: Gemini 2.0+ models get the schema promoted to responseJsonSchema
via _build_json_schema (preserving $defs/$ref natively), older models
keep responseSchema but the schema is flattened with
_build_vertex_schema. VertexAIGoogleGenAIConfig (which overrides the
transform entirely) calls the same helper before building the request.
…eSchema co-present Previously, when both responseJsonSchema and responseSchema were present on Gemini 2.0+, _normalize_response_schema processed responseJsonSchema first (no-op normalization) then unconditionally promoted responseSchema to responseJsonSchema, clobbering the caller-supplied value. Now skip the promotion (and drop the redundant responseSchema) when the caller already supplied responseJsonSchema. Co-authored-by: Mateo Wang <mateo-berri@users.noreply.github.com>
Drop the docstring on _normalize_response_schema and the two inline comments that just restated what the surrounding code/asserts already say. Function name + variable names carry the intent; PR description covers the why-it-exists context.
_build_json_schema is a no-op (returns its argument unchanged), so the deepcopy + round-trip on the responseJsonSchema branch allocated a full schema copy on every request with no observable effect. Forward the caller's value as-is, and just move the popped responseSchema value when promoting on Gemini 2.0+. Co-authored-by: Mateo Wang <mateo-berri@users.noreply.github.com>
…ation Per @Sameerlite review: lock the two Gemini schema-normalization paths together. If either GoogleGenAIConfig._normalize_response_schema (native generateContent) or VertexGeminiConfig.apply_response_schema_transformation (/chat/completions) drifts, the parity test fails — forcing both to be updated together.
|
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
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.
Addresses @Sameerlite review on #27775:
Adds
test_response_schema_normalization_parity_across_chat_and_native_pathswhich feeds the same schema through bothGoogleGenAIConfig._normalize_response_schema(nativegenerateContent) andVertexGeminiConfig.apply_response_schema_transformation(/chat/completions) and asserts equality on Gemini 2.0+ and Gemini 1.5. If either path drifts, the test fails — forcing both to be updated together.This branch builds on the tip of #27775; the parity test is the only delta.
Note
Medium Risk
Changes how
responseSchema/responseJsonSchemaare emitted for Gemini native/Vertex GenAI requests, which can alter structured-output behavior across model versions. Risk is mitigated by extensive new tests, but schema edge cases could still regress.Overview
Adds native
generateContentresponse-schema normalization so Gemini 2.0+ promotesresponseSchematoresponseJsonSchema(preserving$defs/$ref), while Gemini 1.5 keepsresponseSchemabut flattens it via_build_vertex_schema.Applies the same normalization in the Vertex GenAI transformation path and adds a broad test suite, including a parity test ensuring native (
GoogleGenAIConfig._normalize_response_schema) and chat (VertexGeminiConfig.apply_response_schema_transformation) schema normalization stay in sync.Reviewed by Cursor Bugbot for commit bf20bd8. Bugbot is set up for automated code reviews on this repo. Configure here.