mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #4836] [Engine] /v1/messages applies output_config.format, so client.messages.parse works #5206
Labels
No labels
needs-format
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Niko1221/Strata#5206
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Niko1221/Strata/pull/4836
Author: @gdevenyi
Created: 10/31/2026
Status: 🔄 Open
Base:
main← Head:api/anthropic-output-format📝 Commits (1)
ad7d226/v1/messages applies output_config.format json_schema: the schema reaches the prompt and count_tokens, a valid answer comes back as canonical JSON so client.messages.parse works, a failed check is a 200 with strata_structured_output_error; requests without the field are unchanged📊 Changes
3 files changed (+293 additions, -0 deletions)
View changed files
📝
docs/DETAILS.md(+4 -0)📝
serve/server.py(+66 -0)➕
serve/test_anthropic_output_format.py(+223 -0)📄 Description
Problem and result
/v1/messagesdroppedoutput_config.formatwithout any error. The server read onlyeffort. The schema never reached the prompt and the answer was not checked. Soclient.messages.parse()raised on a fenced or non-JSON reply. A client withoutparsegot plain text and no sign that its schema was dropped.Anthropic's structured outputs page (read through a fetch tool's extraction, not raw text): "The request carries the schema in
output_config.formatwithtype: "json_schema". SDK helpers set this for you."Before (
c62d5e3a, mock engine): 200, schema not in the prompt. A fenced{"a": 1}madeparseraiseValidationError.After, for requests that send the field:
count_tokens.parsereturnsa=1, also when streaming.strata_structured_output_error.max_tokensstop comes back as it is.type, a missing schema, or a schema that is not limited to objects at its root is a 400 namingoutput_config.format.Requests without the field are unchanged on the wire (see Validation).
Related discussion
No issue asks for this, and no scope was agreed. I am an outside contributor, so the two decisions below are open to your call. The threads were read before this change and not rechecked at the final commit:
strata_field on the message.Approach
The field maps to the
response_formatshape and goes through the existingprepare_format, in both the request andcount_tokens. The events are held with keep-alives, checked withvalidated_json, then replayed, asstructured_chunksdoes for chat. Without the field the event stream is not wrapped. The change is 66 added lines inserve/server.pyand 4 indocs/DETAILS.md.A failed check is a 200 plus a flag, unlike the 502 on chat. Claude Code 2.1.316 sends this field on a prompt hook. A local prototype that returned 502 for a plain-text reply made it retry ("API error (attempt 6/10): 502") until "prompt hook (Stop) timed out after 30002ms". On real hardware each retry would be a whole generation. The page also allows a 200 that does not match the schema: on
max_tokens, "The output might be incomplete and not match your schema."parse()checks the answer on the client.Tools are allowed, as on
/v1/responses, not refused as on chat. The page says JSON outputs and strict tool use "work together". A turn that calls a tool skips the check.Compatibility: only requests that send the field change. They now get the answer after the check, with keep-alives before it, not piece by piece. On a DeepSeek turn with no
</think>that passes the check, the repeated thinking block is dropped, also when streaming, socontenthas only the text. The server window prints the failure without the model's values.Validation
Revision: this PR's commit (7b6f3e94) on
c62d5e3a. Linux, Python 3.12,anthropicSDK 2.3.0. Mock engine, except the real-engine lines marked below.Passed:
python -m unittest serve.test_server serve.test_responses serve.test_anthropic_base_url serve.test_chat_tool_results serve.test_context_overflow serve.test_images serve.test_anthropic_output_format serve.test_structured:Ran 463 tests,FAILED (failures=1, skipped=4). The one failure is listed below. Nojsonschemain this run.With
jsonschema4.26.0 on the path,python -m unittest serve.test_anthropic_output_format serve.test_structured:Ran 26 tests,OK, no skips. This covers a wrong type, an extra property and a missing required field: each is a 200 with the flag.python -m unittest serve.test_anthropic_output_formatwithserve/server.pyfromc62d5e3a:Ran 13 tests,FAILED (failures=18, errors=3, skipped=1). Three tests pass there on purpose. They guardmax_tokens, requests with no format andcount_tokens.Local script, old server against this commit: 17 requests that do not send
output_config.format(messages, stream, effort, null format, system, thinking, tools,max_tokens,count_tokens, a bad stop, chat and responses). The status, headers, body and rendered prompt were identical (cmp). Ids, timestamps, timings and keep-alive lines were normalized, and the Date and request-id headers left out.Local script, real SDK against the mock server:
messages.parsegivesa=1for plain and fenced replies.messages.streamwithoutput_formatgivesa=1, also on a DeepSeek turn with no</think>. Atool_usereply with no text passes through.Local run, Claude Code 2.1.316 prompt hook against the mock server: a reply of
{"ok": true}arrives as{"ok":true}and the condition is met. A reply ofokfails at once (error parsing response as JSON: ok), with no retries.Real engine: RTX 5080 16 GB, engine 0.1.55 from release v0.2.9, Qwen3.8-Flash-Next IQ3_S, context 65536, 127.0.0.1. The same SDK requests on
c62d5e3aand on 7b6f3e94, one short prompt ("Alice is 30 years old and lives in Paris.") with a three-fieldPersonmodel. Right values means name, age and city all correct:c62d5e3ajsonschema4.26.0messages.parse, thinking offmessages.parse, thinking onmessages.streamwithoutput_format, thinking offmessages.streamwithoutput_format, thinking onOn
c62d5e3aevery run raisedValidationError, because the model wrote plain text such as "Understood. How can I help you with Alice?". On this commit no run was flagged.Real engine, no field, a fresh server on each commit: 9 requests at temperature 0 (4 whole, 4 streamed, thinking off and on, and
count_tokens). Status, body, event counts and text were the same on both, with ids and wall time left out, the bodies compared up to 1500 characters and the streamed text up to 800.count_tokensis 64 on both, and 149 on this commit with a small schema in the field.Real engine, this commit, a real
max_tokensstop (max_tokens: 4, whole and streamed, 1 run each): 200,stop_reason: max_tokens, the cut text as it is, no flag.Real engine, this commit, a real tool call with the field (2 whole, 2 streamed):
tool_useas it is, no flag.Real engine, this commit with
jsonschema4.26.0, a real failed check, no patch:minimum10,maximum5), 3 whole and 2 streamed: all 5 were a 200 with the flag, onmessage_deltawhen streamed. The whole replies kept the model's text as it was. The SDK'sparseon a model with those limits raisedValidationErroron the client (1 run).Real engine, this commit: a wrong
type, a missing schema and an array root, 1 request each: a 400 namingoutput_config.format.Failed:
serve.test_server.GpuChoice.test_hip_ordinal:AssertionError: '0,1' != '1,0'. The same onc62d5e3aalone. Not caused by this change.Not run:
On real hardware:
DeepSeek, or any model other than Qwen3.8 IQ3_S. So the turn with no
</think>is mock only.A real reply seen fenced or surrounded by text. The server sends only the compact JSON, and I did not log the raw replies, so I cannot say how many real replies needed that.
A failed check without
jsonschema. In that session the 4 prompts asking for a plain word all came back as JSON, and the poem prompt ran only withjsonschema.A failed check seen through the stream on a non-JSON reply: the streamed failures were the schema no value can meet.
Keep-alives on a long generation: every turn took about a second or less. One keep-alive line was seen, but no run was long enough to show the hold.
--paralleland other load.Claude Code's prompt hook against the real model, and the 502 retry cost, which was seen on a local prototype only.
Text before a tool call: the 4 real tool turns had no text.
Other prompts and schemas: the
parsetally is one prompt and one simple model.Windows and macOS.
CUDA, HIP, SYCL and Metal builds: the change is Python only.
Limits
parse) is one short prompt, one simple schema and one model. I have no broader measurement that a model follows a schema.parseusers.jsonschema, the check is only "one JSON object", as on chat.jsonschemainstalled, a dangling local$refsuch as#/$defs/nois a 200 with the flag on every reply. This comes fromprepare_format.parseraisesValidationErrorwhen the model writes text before a tool call: the SDK parses every text block. Observed with "Let me check." plus a call.message_delta.typeother thanjson_schema, got a 200 on the old server and is now a 400. Anthropic's page states no object-root rule.stop_reason: refusal.parsewould fail on it. I could not make a turn do this.c62d5e3atoo: a request with onlyoutput_config.formatskips the config'sreasoning_effort, andcount_tokensdoes not applytool_choice: none.🤖 Generated with Claude Code
https://claude.ai/code/session_01W53iURzvDQMjnYemag8uvv
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.