F4b-Gemini — native structured output via responseSchema + JSON Schema translator#106
Merged
Merged
Conversation
…Type. Second slice of the F4b family (after #102 shipped OpenAI). Same shape: when SendArgs.jsonSchema is set, the Gemini adapter wires generationConfig.responseMimeType="application/json" plus a responseSchema translated from the caller's JSON Schema into Gemini's OpenAPI-subset Schema proto. What lands src/providers/gemini.ts: - GeminiRequestBody.generationConfig gains optional responseMimeType ("application/json") and responseSchema (Record<string,unknown>). - buildGeminiRequest accepts opts.jsonSchema. When supplied, body gets responseMimeType + responseSchema = jsonSchemaToGeminiSchema(...). - sendGemini threads args.jsonSchema into the builder. NEW jsonSchemaToGeminiSchema translator: - Upper-cases `type` (STRING/NUMBER/INTEGER/BOOLEAN/ARRAY/OBJECT) — Gemini's Schema proto rejects lowercase JSON Schema type names. - Recurses into properties + items. - Passes through Gemini-supported keywords: description, enum, format, nullable, required, minItems, maxItems, minimum, maximum. - Handles type-arrays (["string","null"]) → STRING + nullable:true. - Drops $schema and additionalProperties (Gemini-unsupported). - Never throws on degenerate input; degraded translation paths emit a relaxed superset and rely on F4c's tolerant parser + downstream validateSchema as the safety net. test/providers/gemini-native-json.test.ts (11 tests): builder (3): responseSchema attached when jsonSchema supplied; omitted in baseline; body remains JSON-serializable. translator (8): primitive type uppercasing; recursion into properties + items; passthrough of required/enum/format/description; type-array collapse to nullable; $schema and additionalProperties dropped; deeply nested schemas (3 levels); degenerate input doesn't throw. Scoreboard TS: 1,316 / 1,316 (+11 over PR #102's 1,305) typecheck + build: clean Defense-in-depth chain now covers OpenAI via response_format: json_schema (PR #102) Gemini via responseSchema + responseMimeType (this PR) Anthropic via tool_use input_schema (follow-up) Universal schema-in-prompt + tolerant parser (F4c) (already in main) The translation layer is intentionally permissive: when a passed schema uses constructs Gemini's Schema proto doesn't support, we strip the offending keyword and emit a looser shape. The model may then return a slightly richer-than-intended object, which validateSchema catches at the parse/validation boundary. Bias toward "native output mostly works" + parse-side defense rather than "fail the call because the schema can't be perfectly translated." Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
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.
Summary
Re-opened after #103 auto-closed when its base branch (#102's `feature/f4b-native-structured-output`) was deleted. Same diff as #103, rebased onto current main (which now has F4b OpenAI squashed in).
Second slice of the F4b family — when `SendArgs.jsonSchema` is set, the Gemini adapter wires `generationConfig.responseMimeType="application/json"` plus a `responseSchema` translated from the caller's JSON Schema into Gemini's OpenAPI-subset Schema proto.
What lands
Tests
11 new tests (3 builder + 8 translator) covering primitives, recursion, passthrough, type-array collapse, drop-list, 3-level nesting, degenerate input.
Scoreboard
🤖 Generated with Claude Code