Skip to content

F4b-Gemini — native structured output via responseSchema + JSON Schema translator#106

Merged
fxspeiser merged 1 commit into
mainfrom
feature/f4b-gemini-native-structured
Jun 7, 2026
Merged

F4b-Gemini — native structured output via responseSchema + JSON Schema translator#106
fxspeiser merged 1 commit into
mainfrom
feature/f4b-gemini-native-structured

Conversation

@fxspeiser

Copy link
Copy Markdown
Owner

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

  • `GeminiRequestBody.generationConfig` gains optional `responseMimeType` + `responseSchema`
  • `buildGeminiRequest` accepts `opts.jsonSchema`; `sendGemini` threads it through
  • New `jsonSchemaToGeminiSchema` translator — upper-cases types, recurses into properties/items, passthrough Gemini-supported keywords, type-array collapse to nullable, drops `$schema` + `additionalProperties`. Never throws; degraded translations emit a relaxed superset and rely on F4c's tolerant parser + downstream `validateSchema` as safety net

Tests

11 new tests (3 builder + 8 translator) covering primitives, recursion, passthrough, type-array collapse, drop-list, 3-level nesting, degenerate input.

Scoreboard

  • TS: 1,316 / 1,316
  • typecheck + build: clean

🤖 Generated with Claude Code

…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>
@fxspeiser fxspeiser merged commit a06dd47 into main Jun 7, 2026
8 checks passed
@fxspeiser fxspeiser deleted the feature/f4b-gemini-native-structured branch June 7, 2026 23:00
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