Skip to content

Gemma: render a string-first scalar union as its typed member; serve those requests cold - #2658

Closed
jonathan308 wants to merge 3 commits into
fix/tool-call-history-nulls-v9-cold-c8from
fix/gemma-string-first-union-v9-cold
Closed

jonathan308 wants to merge 3 commits into
fix/tool-call-history-nulls-v9-cold-c8from
fix/gemma-string-first-union-v9-cold

Conversation

@jonathan308

@jonathan308 jonathan308 commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Gemma 4 showed a tool parameter typed as a string-first union of string and one other scalar type as STRING, so it quoted typed values: "-4200" for -4200, "21.5", "false". Examples are {"type": ["string", "integer"]} and anyOf / oneOf of the two. Gemma 4 now sees the other member (INTEGER, NUMBER or BOOLEAN). The union stays in the request.

The prompt contract stays darkbloom-request-normalization-v9, so a coordinator request whose Gemma tool declaration this changes is served without the prompt cache. Every other request is unchanged.

Base

Stacked on #2651, which adds the served-cold machinery this reuses. This PR's change is commits 73cdc2d07e (string-first unions), ecd7435619 (test-only Swift 6 fix) and 4af17dfdcf (properties declared without a type). #2651 is based on the C7 candidate 9bd3e01f7d, whose tree D7 012821f59a equals.

What changes

  • Gemma render (Gemma4ToolSchemaEnforcement invariant 7):
    • When it applies: a property node that is typed string and has anyOf or oneOf members of string and exactly one other scalar type (integer, number, boolean).
      • A null member is skipped, because nullable already carries it.
      • Nodes with an enum, a const or an allOf keep STRING.
    • What it does: the render type becomes that other member.
    • What stays the same: the left-over mixed-enum case keeps STRING so its enum stays visible. Unions with more members or non-scalar members keep STRING. Member-reversed unions already rendered their first member.
  • Properties declared without a type (Gemma4ToolSchemaEnforcement invariant 8):
    • The problem: the shared repair gives a property declared with no type or schema keyword ({}, any value) the type string, so Gemma saw STRING and quoted its values.
    • The fix: Gemma's render now reads the caller's declarations as sent (ChatTemplateControls.declaredTools) and renders such a property with an empty type, type:<|"|><|"|>. That is how the served template renders a typeless property; its STRING/ARRAY/OBJECT branches all stay off.
    • Render paths: the declarations are passed on every path that renders a coordinator request: serving, prompt recount and the diffusion media path. Without them, the injected type stays.
    • Typed declarations: enum-only, items and properties declarations keep their render type.
  • Validation is unchanged: enforcement feeds only the template input. The validator and constrained decoding read the request's tools, which still carry the anyOf / oneOf the shared repair wrote, so both members are accepted as before.
  • Other families: unchanged. ToolSchemaNormalization (ensureParameterTypes) and its output are untouched.
  • Served cold (GemmaToolUnionTypeCache):
    • The check: ChatTemplateFixes.changesGemmaToolRenderType runs the same Gemma tool path (metadata scope, sanitize, enforcement), with the declarations as sent, on the request's tools and reports whether invariant 7 or 8 changed a render type. Declared tools are a superset of the rendered ones, because tool choice only narrows them.
    • When it applies: it runs after the Render an earlier tool call's null arguments; serve those requests cold #2651 check, on a context that still plans a cache attempt.
    • What it does: the request then gets no scope and no receipt nonce, so there is no lookup or ready receipt, no cached-token usage and no checkpoint write.
    • Observability: a public fixed log line in darkbloom logs, and stats.promptCacheSkippedToolUnionType in the daemon state. No wire change.
  • Coordinator, no penalty: the argument accepted for Render an earlier tool call's null arguments; serve those requests cold #2651 applies unchanged.
    • A planned attempt that gets no lookup or ready receipt just expires: no quarantine, proof fence or holder invalidation.
    • Only that request loses cache routing. Its exact prompt-work count stops qualifying (the provider's recount differs by the type token), so it serves without calibrated first-content admission.
    • Billing uses the provider's usage, and refusals are unchanged.

Test plan

  • CI (dev) on this head.
  • New GemmaToolUnionTypeTests, each request built through the serving decode, so the shared repair runs:
    • every string-first shape renders its typed member: type, anyOf and oneOf × integer / number / boolean;
    • ["string","integer","null"] renders INTEGER and nullable;
    • a nested object property is covered;
    • the mixed enum, member-reversed unions, three-member unions, string|object and plain types keep their type;
    • the wire keeps anyOf / oneOf;
    • Qwen 3.5, Qwen 3.5 MoE, Qwen 3.8, Nemotron 3.5, GPT-OSS and Llama keep string;
    • served-cold cases: a planned Gemma request is served cold; an unchanged render, an unplanned request, a request already served cold and other families keep their context.
  • New tests for invariant 8:
    • {} and {description} render an empty type, and serve cold, only with the declarations;
    • a nested property is covered;
    • typed, enum-only and structured declarations keep their type;
    • GPT-OSS is unchanged.
  • ProductionPromptParityTests:
    • Pinned set: the set of vector bodies whose Gemma tool render type changes is pinned to exactly six: the three Gemma models × endpoint_messages_tool_turn and tools, which declare city: {}. No vector body declares a string-first union (checked locally).
    • For each of the six, the test checks that:
      • it is served cold;
      • the provider's render differs from the vector;
      • the vector still matches the same body with each such property typed string, the way the repair renders it under v9. The typeless check is the production declaresAnyValue.
    • Other cases: every other case is checked exactly as before. The Render an earlier tool call's null arguments; serve those requests cold #2651 classification is unchanged.
  • Red control: Red control for #2658: tests without the change (do not merge) #2659. It keeps the API but never changes the render type.

Components touched

  • provider-swift (Swift CLI)
  • native runtime / MLX
  • landing (Next.js)
  • enclave (Swift)
  • CI / provider release
  • docs

Protocol / interface changes

  • No protocol/interface changes. The prompt contract ID and normalization version (v9) are unchanged, and no message or field is added.
  • Yes — described above and matching side updated

Documentation impact

  • Canonical documentation updated: docs/architecture/inference.md (Gemma render type) and docs/architecture/prefix-cache.md (served cold).
  • No documentation needed — reason:

Changelog

  • Added changelog.d/gemma-tool-union-render-type.md and ran python3 scripts/changelog.py check.
  • No user-visible change, or an explicitly authorized release/history change.

Notes

  • GPT-OSS is not included. Its template prints raw type lists and {} honestly, but the shared repair runs once at ingress and leaves no marker on a {} it typed string. Keeping GPT-OSS's schemas as written therefore needs the raw schema or new repair markers on the render path. That is planned separately.
  • When each invariant takes effect:
    • Invariant 7 (string-first unions): applies to today's forwarded requests. The coordinator forwards repaired tools (anyOf plus the injected type), which is the shape it reads.
    • Invariant 8 (properties declared without a type): inert with today's coordinator. A forwarded {} already arrives typed, so the declarations the provider receives have no typeless property, and no request is served cold for it. It takes effect only when the coordinator forwards the caller's original tool schemas.
    • Parity vectors: they are built from client bodies. That is why their city: {} bodies are classified, and the parity test checks both shapes.
  • Expected effect: in a sampled Gemma run, all 15 string-first union failures had a member-reversed sibling that passed. The mixed enum case is left as is.

Before And After

flowchart LR
  A["value: type [string, integer]"] --> R["shared repair:<br/>anyOf [string, integer], type string"]
  R --> B["Before: Gemma renders type:STRING<br/>model writes \"-4200\""]
  R --> C["After: Gemma enforcement renders type:INTEGER<br/>model writes -4200"]
  R --> W["wire keeps anyOf: validator accepts both"]
Loading
sequenceDiagram
  participant H as handleInferenceRequest
  participant N as ToolCallHistoryNullCache
  participant U as GemmaToolUnionTypeCache
  H->>N: servingContext(planned, request, modelType, managedSuper)
  N-->>H: context (cold if history null)
  H->>U: servingContext(context, request, modelType)
  U-->>H: context (cold if a Gemma render type changed)
Loading

Refactor Pass

Refactor pass: done; no behaviour-preserving changes warranted.

…those requests cold

Gemma 4's template prints one render type per tool parameter and never
anyOf/oneOf, and the shared schema repair types a union by its first non-null
member. A union of string and one other scalar type with string first, such as
{"type": ["string", "integer"]} or an anyOf/oneOf of the two, therefore
rendered STRING and Gemma quoted typed values ("-4200" for -4200).

Gemma's tool enforcement (Gemma4ToolSchemaEnforcement invariant 7) now renders
such a property as the other member (INTEGER, NUMBER or BOOLEAN); a null member
is skipped as nullable carries it. Unions with more members, non-scalar
members, an enum, a const or allOf keep STRING. The union stays in the request
and only the render input changes, so the validator and constrained decoding
still accept both members. Other families keep the shared repair's render type.

The prompt contract stays v9, which renders STRING. A coordinator-planned
request whose declared tools get a changed Gemma render type
(ChatTemplateFixes.changesGemmaToolUnionType, the same tool path run on the
declared tools) is served without the prompt cache, like a request whose
tool-call history carries a null: no scope or receipt nonce, logged and counted
(promptCacheSkippedToolUnionType). ProductionPromptParityTests pins the set of
vector bodies this applies to: none.
@vercel

vercel Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
d-inference-landing Ready Ready Preview Nov 24, 2026 3:36pm UTC
darkbloom-status Ready Ready Preview Nov 24, 2026 3:36pm UTC
eigen-homepages-darkbloom Ready Ready Preview Nov 24, 2026 3:36pm UTC

Request Review

A stored static of [([String: Any], String)] is not Sendable under Swift 6
strict concurrency; the table is now computed on each use. Test-only.
…those requests cold

The shared schema repair gives a property the caller declared without any
type or schema keyword ({}: any value) the type string, so Gemma 4 saw STRING
and quoted its values. Gemma's tool enforcement (invariant 8) now reads the
caller's declarations as sent (ChatTemplateControls.declaredTools, which the
serving, recount and diffusion media paths pass to ChatTemplateFixes.normalizeTools)
and renders such a property with an empty type, type:<|"|><|"|>, as the served
template renders a typeless property. Without the declarations the injected
type stays. Other families are unchanged.

The same served-cold rule covers it: ChatTemplateFixes.changesGemmaToolRenderType
reports invariant 7 or 8, and the handler passes the declarations. Under the
v9 contract the vectors' Gemma bodies that declare city: {} now render
differently, so ProductionPromptParityTests pins exactly those six bodies
(three Gemma models x endpoint_messages_tool_turn and tools), checks they are
served cold and differ from the vector, and matches the vector for the same
body with each such property typed string (Gemma4ToolSchemaEnforcement.
declaresAnyValue), as the repair renders it under v9.
@jonathan308

Copy link
Copy Markdown
Collaborator Author

Closing: this branch's head (4af17df) is already contained in dev.

@jonathan308 jonathan308 closed this Dec 2, 2026

This branch was successfully deployed

3 active deployments
Preview – d-inference-landing — 4af17dfd Deployed Nov 24, 2026 by vercel[bot]
Preview – eigen-homepages-darkbloom — 4af17dfd Deployed Nov 24, 2026 by vercel[bot]
Preview – darkbloom-status — 4af17dfd Deployed Nov 24, 2026 by vercel[bot]
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