Codex's background memory extraction asks for text.format json_schema and
parses the reply text as JSON. A translated model that answered
```json\n{...}\n``` failed it with "expected value at line 1 column 1".
A client that asked for structured output (a format field, or one told in
the system prompt: Request.FormatTold, set by inSystem) now gets the JSON
inside a single unlabelled or json-labelled fence when that is a complete
valid JSON value: streamed (fenceEncoder, which holds text only while it
could still be a fence and passes other text on at once) and whole
(unfencedResult in render). Invalid or truncated JSON, other languages,
text outside the fence and plain-text clients are left as they came.
Relayed (passthrough) replies stay byte for byte. Rebased onto #2126's
asObject/told format: inSystem keeps its told check and sets FormatTold.
lidi1011's patch from lidi1011/magpie fix/structured-output-code-fence
(e67b4ba7b, 8bc440bbb), taken as is apart from the format.go conflict.
Run (macOS, sandbox HOME, pinned caches):
- go test -tags nogui -run 'Fence|Format|Structured|JSON' ./internal/gateway: ok
- with the encoder and render hooks removed by hand,
TestStructuredOutputFencedAnswerUnfenced (json_schema/json_object, whole
and streamed) and TestStructuredOutputFenceAfterSystemFallback FAIL with
text = "```json\n{...}\n```"; restored, they pass.
- on b9c589ebb plus these commits: gofmt -l (nothing), go vet ./... on
darwin, GOOS=linux -tags nogui and GOOS=windows, go test -tags nogui ./...:
every package ok but two filed load flakes, TestMaxRPMWaitsForRoomInTheMinute
(#1791) and TestCopilotSecretToolUsername (#1627), on a machine at load
~200; each passed -count=3 alone.
- rebased onto e98440ae6 (27 more commits on main) and
run again there: gofmt -l, go vet ./... (darwin, linux nogui, windows),
go test -tags nogui ./internal/gateway ./internal/proc ./internal/agent.
- then onto af27b70b4 (9 commits; gateway routing.go/gateway.go and agent
codex.go touched, not these lines): gofmt -l, go vet on the three OSes,
and the tests named above plus the new remote-magpie and Codex ones.
Not tried: a real Codex memory run against a live model that fences its
JSON; a Gemini stream whose thoughtSignature lands mid-text flushes the
held text and is left fenced (falls back to today's behaviour).
Co-authored-by: lidi1011 <283302260+lidi1011@users.noreply.github.com>
klarkxy's Codex on Xiaomi MiMo's Token Plan got back "text.format type
'json_schema' is not supported, only 'text' and 'json_object' are
supported" and the turn ended there. Codex's /responses reaches the
upstream by passthrough when the protocols match, and passthrough never
looked at a format refusal; the translated path did, but only by moving
the whole format into the system prompt, which drops the object
requirement the vendor would have kept.
Now, on either path, a json_schema the upstream turns away is asked
again as {"type":"json_object"} with the schema appended to the system
prompt (Responses instructions, Chat's first system/developer message,
Anthropic system). If json_object is turned away too, the format field
goes and only the words stay. Each step is remembered per provider,
model and protocol once a request with it is taken, so later turns go
out already rewritten. Anthropic's output_config.format has no object
mode, so it goes straight to words. DeepSeek, which takes json_object
only, gains the same middle step on the translated path.
Run:
- format_refused_test.go, new:
- TestPassthroughSchemaRefusedAskedAsObject replays the reporter's
exact error and probe body through /backend-api/codex/responses.
It covers object taken (2 asks, then json_object) and object refused
too (3 asks, then no format). Turn 2 is checked to take 1 ask, the
instructions to keep "You are Codex." first, and the input to be
unchanged.
- TestPassthroughFormatRefusedChatAndAnthropic.
- TestRefusedFormatToldInWords was rewritten for both outcomes on the
translated path.
- Break check: with the old gateway.go, the passthrough test gets back
the reporter's exact error.
- gofmt -l is clean. go vet passes for darwin, GOOS=linux -tags nogui
and GOOS=windows. go test ./internal/gateway/ -count=1 is ok (1017s).
Not tried: a real Xiaomi MiMo key (none here; the error envelope is
assumed to be OpenAI's), and Gemini passthrough, which has no rewrite.
The IR held a schema only from Anthropic's output_config.format, and only
Messages (and Claude Code's --json-schema) were asked for one. A Responses
request to a provider magpie speaks Chat or Messages to (OpenRouter has no
Responses URL) lost text.format; a Chat request translated to Responses or
Messages lost response_format; an Anthropic schema translated to Chat or
Responses was lost too.
Format (format.go) now carries json_schema (name, description, schema,
strict) or json_object from Responses, Chat, Anthropic and Gemini, and each
builder asks for it in its upstream's field. Where a target can't say it
(json_object on Messages or Claude Code; the vendor bridges Cursor, Devin,
Kiro, Qoder, Command Code, Zed) it is told in the system prompt. An upstream
answering 400/422 naming the field (DeepSeek's "This response_format type is
unavailable now") is asked again with it told in words, and so from then on.
Relayed requests are unchanged.