Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
4ebb3ee
GPT-OSS: render tool parameters as declared; serve changed renders cold
jonathan308 Nov 24, 2026
3064ea0
Red control: render the repaired tools for GPT-OSS again (do not merge)
jonathan308 Nov 24, 2026
caa67b6
Red control: avoid an unreachable-code warning
jonathan308 Nov 24, 2026
5c7797e
Merge remote-tracking branch 'origin/fix/gemma-string-first-union-v9-…
jonathan308 Nov 24, 2026
617338f
GPT-OSS: render the template utility's tools as the served prompt does
jonathan308 Nov 24, 2026
c698689
Merge branch 'fix/gptoss-raw-tool-schema' into test/gptoss-raw-tool-s…
jonathan308 Nov 24, 2026
2c16e75
Red control: the template utility renders the tools unchanged again
jonathan308 Nov 24, 2026
e310af3
Template utility: pass the declarations to Gemma's tool normalization
jonathan308 Nov 24, 2026
1cf1e61
Merge branch 'fix/gptoss-raw-tool-schema' into test/gptoss-raw-tool-s…
jonathan308 Nov 24, 2026
a3a504d
Red control: the template utility drops the declarations again
jonathan308 Nov 24, 2026
734e33a
GPTOSSToolSchemaRenderTests: pass the prompt to #require as an interp…
jonathan308 Nov 24, 2026
3d8efaa
Changelog: the template utility shows the render of the declarations …
jonathan308 Nov 24, 2026
666d9b7
Merge branch 'fix/gptoss-raw-tool-schema' into test/gptoss-raw-tool-s…
jonathan308 Nov 24, 2026
de58437
GPTOSSToolSchemaRenderTests: check other families with their own buil…
jonathan308 Nov 24, 2026
14e0344
Merge branch 'fix/gptoss-raw-tool-schema' into test/gptoss-raw-tool-s…
jonathan308 Nov 24, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions changelog.d/gptoss-tool-schema-render.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
### GPT-OSS tool parameters render as declared

GPT-OSS's Harmony template prints a tool parameter's `type` list as a union
(`string | integer`), a `oneOf` as the union of its members and a parameter
without a type as `any`. The schema repair that runs before every template
gave each such parameter one type, the first member's or `string`, so GPT-OSS
was told `string` for numbers, booleans, objects and `{}` and wrote strings,
or reasoned at length about the mismatch. GPT-OSS now renders each tool from
the request's own declaration and keeps the repair's type only where the
declaration states one type (an `Optional` field, an `enum`, an object or an
array). Other model families are unchanged, and a request whose tools arrive
already repaired renders exactly as before.

The prompt contract stays `darkbloom-request-normalization-v9`, which renders
the repaired types, so a coordinator request whose GPT-OSS tool render this
changes is served without the prompt cache, like requests whose tool-call
history carries a null. `darkbloom logs` notes each such request and the
daemon state counts them (`promptCacheSkippedToolRawSchema`).

The template utility (`/tokenize`) renders a body's tools as that body
declares them, for GPT-OSS and for Gemma 4's untyped properties. It therefore
shows the render of the caller's own declarations: a coordinator request
renders the same only once the coordinator forwards the caller's declarations
rather than the repaired ones.
17 changes: 17 additions & 0 deletions docs/architecture/inference.md
Original file line number Diff line number Diff line change
Expand Up @@ -5183,6 +5183,23 @@ path that renders a coordinator request: serving, prompt recount and the
diffusion media path. Such a request is also
served without the coordinator's prompt cache ([prefix cache](prefix-cache.md)).

GPT-OSS's Harmony template renders a schema as written: a `type` list as
`a | b`, a `oneOf` as the union of its members and a node without a type as
`any`. GPT-OSS therefore renders each tool's parameters from the body's own
declaration (`ChatTemplateControls.declaredTools`) through the same repair
with its invented types left out: a type is written only where the node
states one (a single concrete member beside `null`, a union whose typed
members share one type, `enum` / `const` values, structure or assertions of
one family); a positional boolean becomes `{}`
(`provider-swift/Sources/ProviderCore/Inference/Tools/GPTOSSToolSchemaRender.swift`,
`ToolSchemaNormalization.repairedParameters`). A tool whose declaration the
repair would not change keeps its prepared tool, so a body whose tools arrive
already repaired renders byte-identically. The prompt-token floor renders the
same tools. The auto tool grammar already compiles each call's arguments from
the declarations, so `{}` stays free and a union admits each member. Such a
coordinator request is served without the prompt cache
([prefix cache](prefix-cache.md)).

A template `raise_exception` at request time is classified `jinja_template`
and returned as HTTP 500, the provider-fault class, so a caller control that
a template is known to reject must be resolved before rendering. The
Expand Down
8 changes: 8 additions & 0 deletions docs/architecture/prefix-cache.md
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,14 @@ whether invariant 7 or 8 changed a render type
`provider-swift/Sources/ProviderCore/Inference/PrefixCache/GemmaToolUnionTypeCache.swift`);
its counter is `promptCacheSkippedToolUnionType`.

A GPT-OSS request is served cold the same way when one of its declared tools
renders differently from the repaired declaration normalization v9 renders:
a `type` list, a union of several types or a node without a type, which
GPT-OSS now renders as written (`GPTOSSToolSchemaRender.changesRender`,
`provider-swift/Sources/ProviderCore/Inference/PrefixCache/GPTOSSToolSchemaCache.swift`);
its counter is `promptCacheSkippedToolRawSchema`. A body whose tools arrive
already repaired renders as before and keeps its cache context.

Native MiMo historical checkpoints retain only actual uniform-chunk frontiers
whose target and optional assistant state completed their native observation
and promotion fences. Their fork target is the deepest successfully captured
Expand Down
7 changes: 7 additions & 0 deletions docs/developer/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -1550,6 +1550,13 @@ its `README.md`). `TemplateRenderStackTests` renders that template, and
nested 64 levels deep, the most a request may carry, on the render stack
(`TemplateRenderStack`) in the debug test build.

The same directory holds `tool-schema-corpus.json`, the distinct tool
`parameters` of a realistic tool-call schema corpus (descriptions and titles
reduced to `d`). `GPTOSSToolSchemaRenderTests` renders each through that
template as the ingress repair forwards it and requires a byte-identical
prompt and an unchanged cache context, and renders each as written without a
template failure (`GPTOSSToolSchemaRender`).

`scripts/verify-prompt-parity.sh` uses `scripts/prepare-prompt-fixtures.py` to
download only the committed manifests' hash-pinned public prompt assets, never
weights. The downloader identifies itself as `darkbloom-prompt-fixtures/1` to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public final class AtomicProviderStats: Sendable {
// The same for a Gemma tool union rendered as its typed member
// (`GemmaToolUnionTypeCache`).
private let _promptCacheSkippedToolUnionType = ManagedAtomic<UInt64>(0)
// The same for a GPT-OSS tool declaration rendered as written
// (`GPTOSSToolSchemaCache`).
private let _promptCacheSkippedToolRawSchema = ManagedAtomic<UInt64>(0)

public init() {}

Expand Down Expand Up @@ -184,6 +187,12 @@ public final class AtomicProviderStats: Sendable {
_promptCacheSkippedToolUnionType.add(1)
}

public var promptCacheSkippedToolRawSchema: UInt64 { _promptCacheSkippedToolRawSchema.load() }

public func incrementPromptCacheSkippedToolRawSchema() {
_promptCacheSkippedToolRawSchema.add(1)
}

public func incrementGenerationErrorsAfterOutput() {
_generationErrorsAfterOutput.add(1)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2510,7 +2510,10 @@ public struct MultiModelBatchSchedulerEngine: MLXServerEngine, Sendable {
try MiMoV26TemplateFix.validateRequest(chat)
}
let messages = request.messages.map { $0.templateMessageDict() }
let tools = request.tools?.map { $0.toolSpec() }
// The body's tools are its declarations, unrepaired: GPT-OSS renders
// them as its served prompt does (`GPTOSSToolSchemaRender`).
let tools = GPTOSSToolSchemaRender.servedTools(request.tools,
modelId: request.model, modelType: resolved.modelType)?.map { $0.toolSpec() }
// Drop JSON `null` / `Optional` leaves the Jinja bridge
// can't convert before rendering (mirrors `streamChatCompletion`).
let fixContext = ChatTemplateFixContext(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import MLXLMServer

/// A GPT-OSS tool declaration that now renders as written
/// (`GPTOSSToolSchemaRender`), while the prompt contract the coordinator plans
/// and counts with (normalization v9) renders the repaired one. Such a request
/// is served without the coordinator's prompt cache, as
/// `ToolCallHistoryNullCache` serves its requests; every other request keeps
/// its context. A body whose tools arrive already repaired renders as before
/// and keeps its context.
enum GPTOSSToolSchemaCache {
/// The cache context `request` is served with: `context`, or one without a
/// scope or receipt nonce when `context` still plans a cache attempt and
/// the request's GPT-OSS tool render changed. `servedCold` reports that case.
static func servingContext(
_ context: RemotePrefixCacheContext, for request: OpenAIChatCompletionRequest,
declared: [OpenAITool]?, modelType: String?
) -> (context: RemotePrefixCacheContext, servedCold: Bool) {
guard context.scope != nil || context.receiptNonce != nil,
GPTOSSToolSchemaRender.changesRender(
request.tools, declared: declared, modelId: request.model, modelType: modelType)
else { return (context, false) }
return (RemotePrefixCacheContext(cacheScope: nil, cacheReceiptNonce: nil), true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ enum ProviderPromptContractPipeline {
}
return value
}
let tools = prepared.tools?.map { $0.toolSpec() }
let tools = GPTOSSToolSchemaRender.renderedTools(prepared.tools, declared: templateControls.declaredTools,
modelId: request.model, modelType: modelType)?.map { $0.toolSpec() }
let context = ChatTemplateFixContext(
modelId: request.model,
modelType: modelType)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright © 2026 Eigen Labs.

import Foundation
import MLXLMCommon
import MLXLMServer

/// GPT-OSS's Harmony template renders a tool schema as written: a `type` list
/// as `string | integer`, a `oneOf` as the union of its members and a node
/// without a type as `any`. The shared repair (`ToolSchemaNormalization`)
/// writes one type into each of those nodes, the first member's or `string`,
/// and the model then wrote strings for numbers, booleans and objects. GPT-OSS
/// renders each tool's parameters from the body's own declaration through the
/// repair without the types it invents (`repairedParameters`,
/// `writesRenderTypes` false). A tool that this leaves as the repair leaves it
/// renders as before, so a body whose tools arrive already repaired renders
/// unchanged.
enum GPTOSSToolSchemaRender {
static let redControl = true

static func applies(modelId: String?, modelType: String?) -> Bool {
GPTOSSHarmonyTemplateFix.applies(to: .init(modelId: modelId, modelType: modelType))
}

/// The tools GPT-OSS renders: `tools`, each tool whose declaration in
/// `declared` (by name, the first of a repeated name) the repair's render
/// types change with that declaration's parameters without them. Other
/// families, and every tool without such a declaration, keep `tools`.
static func renderedTools(
_ tools: [OpenAITool]?, declared: [OpenAITool]?, modelId: String?, modelType: String?
) -> [OpenAITool]? {
guard !redControl, applies(modelId: modelId, modelType: modelType), let tools, let declared else { return tools }
let byName = Dictionary(declared.map { ($0.function.name, $0) }, uniquingKeysWith: { first, _ in first })
return tools.map { tool in
guard let declaration = byName[tool.function.name],
let parameters = parametersWithoutRenderTypes(declaration)
else { return tool }
var copy = tool
copy.function.parameters = parameters
return copy
}
}

/// Whether `renderedTools` changes any of `tools`. Declared tools are a
/// superset of the rendered ones (tool choice only narrows them), so
/// passing the request's tools never misses a changed prompt.
static func changesRender(
_ tools: [OpenAITool]?, declared: [OpenAITool]?, modelId: String?, modelType: String?
) -> Bool {
guard !redControl, applies(modelId: modelId, modelType: modelType), let tools, let declared else { return false }
let names = Set(tools.map(\.function.name))
var seen: Set<String> = []
return declared.contains { declaration in
let name = declaration.function.name
guard names.contains(name), seen.insert(name).inserted else { return false }
return parametersWithoutRenderTypes(declaration) != nil
}
}

/// `declaration`'s parameters through the repair without its render
/// types, or nil when that equals the repair's own result (or either is
/// out of the repair's bounds).
static func parametersWithoutRenderTypes(_ declaration: OpenAITool) -> MLXLMCommon.JSONValue? {
guard let repaired = repairedParameters(declaration, writesRenderTypes: true),
let plain = repairedParameters(declaration, writesRenderTypes: false),
plain != repaired
else { return nil }
return plain
}

/// The tools GPT-OSS's served prompt shows for `declared` when nothing
/// repaired them yet (the template utility): each tool's parameters
/// through the repair without its render types, which equals the
/// repair's own result wherever the served prompt keeps it. Other
/// families keep `declared`.
static func servedTools(_ declared: [OpenAITool]?, modelId: String?, modelType: String?) -> [OpenAITool]? {
guard !redControl, applies(modelId: modelId, modelType: modelType), let declared else { return declared }
return declared.map { tool in
guard let parameters = repairedParameters(tool, writesRenderTypes: false) else { return tool }
var copy = tool
copy.function.parameters = parameters
return copy
}
}

private static func repairedParameters(_ declaration: OpenAITool, writesRenderTypes: Bool)
-> MLXLMCommon.JSONValue?
{
guard let parameters = declaration.function.parameters,
let data = try? JSONEncoder().encode([parameters]),
data.count <= ToolSchemaNormalization.maxNormalizationBytes,
let node = (try? JSONSerialization.jsonObject(with: data)) as? [Any], let root = node.first
else { return nil }
var budget = ToolSchemaNormalization.maxNormalizedNodes
return ToolSchemaNormalization.repairedParameters(
root, writesRenderTypes: writesRenderTypes, budget: &budget).flatMap(jsonValue)
}

private static func jsonValue(_ node: Any) -> MLXLMCommon.JSONValue? {
guard let data = try? JSONSerialization.data(withJSONObject: [node]) else { return nil }
return (try? JSONDecoder().decode([MLXLMCommon.JSONValue].self, from: data))?.first
}
}
Loading
Loading