Repository navigation
fix(server): answer malformed chat, messages and responses bodies with JSON errors - #2872
Conversation
…h JSON errors POST /v1/chat/completions, /v1/responses, /v1/messages and /v1/messages/count_tokens, and their aliases without /v1, extracted their typed body with axum's Json extractor, so a body it rejected got axum's text/plain answer: 400 for malformed or empty JSON, 422 for a schema error such as a missing or wrongly typed `model`, and 415 for a missing or non-JSON Content-Type. The disaggregated router's /v1/chat/completions did the same, and its 413 was text/plain too. Each handler now takes the extractor's Result and answers a rejection with an invalid_request_error JSON body, as /v1/completions has done since lablup#2582. Two shared helpers in routes/mod.rs carry the mapping: json_rejection_status (422 becomes 400; 400, 413 and 415 are kept) and parse_json_request (OpenAI envelope), which replaces parse_completion_request. The Anthropic routes answer in their own envelope through parse_anthropic_request, so count_tokens answers a body exactly as /v1/messages does. The rejection is still answered first, before the 501 availability check, dispatch or SSE setup, which is where the extractor used to fail the request. On the single-node server the existing server-wide 413 layer still answers every 413 in the OpenAI envelope, on the Anthropic routes too; this change leaves it alone. A Vertex AI predict instance with a schema error now carries the route's JSON error object instead of the plain-text rejection as a string.
|
|
serde quotes a wrongly typed value in full and axum passes that text through, so the JSON rejection message echoed the offending value back whole. The single-node body limit defaults to about 1.4 GB, and JSON escaping can enlarge the echo further. Truncate the message at a character boundary in one shared helper used by the OpenAI and Anthropic envelopes, which also covers /v1/completions and the disaggregated router. Add single-node and router regression tests and note the bound in docs/server-features.md.
|
Hardening review, pushed as Finding fixed
The change from 422 to 400 is deliberate. Auth order is preserved: an unauthenticated bad body still gets 401. Security and performance: the fix bounds attacker-controlled text in the response; no added work on valid requests. Validation on GB10: 21 focused tests pass, and a curl matrix of 8 paths and 7 bodies ran against a real checkpoint. The router path is covered by a unit test only. Merge gate: first-time-contributor workflow runs wait for approval and were not started, so by maintainer decision the gate ran on GB10 against one tree integrating #2888, #2872, #2869, #2871 and #2870 on |
Summary
When axum's
Jsonextractor rejects a request body,POST /v1/chat/completions,/v1/responses,/v1/messagesand/v1/messages/count_tokensanswer with axum's plain-text 400, 415 or 422. With this change they answer with a JSONinvalid_request_errorin each route's own envelope, as #2582 did for/v1/completions. The rejection is still answered before the availability check, dispatch, or SSE setup./v1/chat/completions,/chat/completions/v1/responses,/responses/v1/messages,/messages/v1/messages/count_tokens,/messages/count_tokens/v1/messages(#2337)/v1/chat/completions{or empty bodytext/plain{"model":5},{}(nomodel)text/plaintext/plainContent-Typetext/plaintext/plainBehavior change: these routes now return 400 instead of 422 for schema errors, and every rejection is
application/json. Nothing indocs/orcompat/pinned the old behavior. The 422 note inscripts/server_chat_smoke.shis updated.Related issues
No linked public issue. Follow-up to #2582.
Type of change
feat— new user-visible featurefix— bug fixperf— performance improvementrefactor— internal restructuring without behavior changechore— build, CI, dependencies, release infrastructuredocs— documentation onlytest— tests onlyTest plan
Local validation: 7 new tests, 62 real-checkpoint HTTP requests per build, and the full workspace suite all passed (15,065 passed, 0 failed).
cargo fmt --all -- --check(enforced by CI — violations block merge)cargo clippy --workspace --all-targets --features metal,accelerate -- -D warnings: clean locally; fork CI skips clippy.cargo test --workspace --profile test-fast --features metal,accelerate --no-fail-fast -- --test-threads=1: 15,065 passed, 0 failed, 493 ignored.cargo deny check: not run. This PR changes no dependency manifest, lockfile ordeny.toml. CI runs cargo-deny only for those paths, so the fork run skipped it.mlx-community/Qwen3-0.6B-4bit, served by pre-fix and post-fixmlxcel-serverbuilds (test-fastprofile,metal,accelerate).Environment: Apple M5 Pro, macOS 26.6.2, Metal, Rust 1.97.1. Base commit
91285843.Remote checks: Fork CI for
e0c3ca28: 13 jobs passed, 17 skipped by repository/path conditions, no failures. Upstream CI awaits maintainer approval, and the CLA check is pending.Regression tests
The tests reuse the #2582 harness, with a scripted provider and a dispatch count.
{, empty,{"model":5},{"model":5,"stream":true},{}, noContent-Type, andtext/plain. Each case asserts the status, a JSON body with no SSE, the envelope, and zero dispatches.count_tokensanswers byte-identically to/messages.DefaultBodyLimit::max(8).Before the fix, 5 tests failed. The control and the parity test passed. The first failure:
Real-checkpoint matrix
The matrix is 8 paths × 7 bodies plus 6 controls at
temperature: 0andmax_tokens: 8.invalid_request_error: 28 in the OpenAI envelope and 28 in the Anthropic envelope.count_tokensmatches/messageson all 14 pairs in both builds.[DONE]or onemessage_stop.Full workspace: 0 failures
mlxcel-coreNotes for reviewers
Helpers.
routes/mod.rsaddsjson_rejection_status, which maps 422 to 400 and keeps 400, 413 and 415, andparse_json_request, which builds the OpenAI envelope. Together they replaceparse_completion_request.anthropic.rsadds a privateparse_anthropic_requestthat uses the same status.Order. Each handler resolves
Result<Json<T>, JsonRejection>first, the same point where the extractor used to fail.413.
openai_payload_too_largeinapp.rsstill rewrites every 413 into the OpenAI envelope, server-wide, including on/v1/messages. This PR leaves it unchanged.Vertex AI. A predict instance with a schema error now returns the route's JSON error object instead of a plain-text string.
Out of scope, all of which still answer in plain text:
server_errorJSON for these./v1/audio/speech.Json<Value>routes, for syntax errors and 415 only.If this lands, I can follow up with the remaining OpenAI-compatible routes (
/v1/audio/speechand theJson<Value>routes above). Each one is the same change usingparse_json_request. The llama-native routes would stay for a compatibility decision.Checklist
fix:).docs/server-features.md(routes, status mapping, and the server-wide 413). No paired Korean page exists for it.Used bycomments to the shared helpers inroutes/mod.rs..envfiles committed.