Repository navigation
Description
Summary
The Anthropic Messages endpoint (/v1/messages) rejects every request from Claude Code 2.1.156 and later with HTTP 422, before any generation runs. Starting with that build, Claude Code interleaves {"role":"system", ...} turns inside the messages array. mlxcel models only user and assistant roles for those turns, so JSON deserialization fails and the whole request is rejected. The endpoint is effectively unusable as a Claude Code backend for any current build.
Background
The Anthropic spec places system text in the top-level system field and allows only user/assistant in messages. Claude Code 2.1.156+ no longer follows that for mid-conversation system reminders: it emits role:"system" entries inside messages. The reported version bisect:
| Claude Code | messages roles | result |
|---|---|---|
| 2.1.145 | [user] |
200 |
| 2.1.153 | [user] |
200 |
| 2.1.156 | [user, system, ...] |
422 |
| 2.1.159 / 162 / 165 / 169 | [user, system, ...] |
422 |
The behavior changed between 2.1.153 and 2.1.156. Everything else Claude Code sends (top-level system, tools, tool_use/tool_result, metadata, image blocks) already returns 200. The OpenAI-compatible /v1/chat/completions path is unaffected because it already accepts system in messages.
The failure is confirmed statically against the current source:
AnthropicRole(src/server/types/anthropic_request.rs:139) is#[serde(rename_all = "lowercase")]with onlyUserandAssistant, sorole:"system"is an unknown variant.AnthropicMessage.role: AnthropicRole(src/server/types/anthropic_request.rs:132) is the field that fails to parse.anthropic_messages(src/server/routes/anthropic.rs:65) takesJson(request): Json<AnthropicRequest>(anthropic.rs:68). There is no explicit 422 in mlxcel; this is axum's default when theJsonextractor fails to deserialize.append_message(src/server/anthropic_translator.rs:158) matchesAnthropicRoleexhaustively overUser/Assistantonly (anthropic_translator.rs:159-161).
Proposed Solution
- Add a
Systemvariant toAnthropicRoleand toAnthropicRole::as_strinsrc/server/types/anthropic_request.rsso the request deserializes instead of returning 422. - Map it in
append_message(src/server/anthropic_translator.rs:159) asAnthropicRole::System => Role::System. The internalRoleenum (src/server/types/request.rs:23) already has aSystemvariant, so this mapping is available with no new type work. - Verify the mid-array system turn actually renders through the chat template. Many templates emit
systemonly at the head of the conversation, so a system turn atmessages[1]may be dropped or fall through to the fallback renderer. If a template cannot place a mid-conversation system message, fold it into the adjacent user turn (or merge it into the leading system block) rather than relying on the template.
Implementation Notes
- The top-level
systemfield is already converted to aRole::Systemmessage inanthropic_translator.rs(see the system-block handling aroundanthropic_translator.rs:84), so aRole::Systemmessage in the stream is already a shape the downstream path expects. - Adding the enum variant alone removes the 422, but item 3 (template rendering) is the part that determines whether the system text actually influences generation. Treat the deserialization fix and the rendering check as one change, not two.
- Existing translator tests live in
src/server/anthropic_translator.rs(for example the system-role assertions aroundanthropic_translator.rs:560); add a case there for asystem-role turn insidemessages.
Acceptance Criteria
- A request whose
messagescontains asystem-role turn deserializes (no 422) and the system text influences generation. - Claude Code 2.1.156 or later connects and completes a turn end-to-end against
/v1/messages. - Top-level
system,tools, andtool_use/tool_resultbehavior is unchanged. - A translator test covers a
system-role turn insidemessages.
Original Suggestion
Title: fix(server): /v1/messages rejects Claude Code >= 2.1.156 with HTTP 422 (system-role turns inside messages; AnthropicRole has no System variant)
Problem / Background
Pointing Claude Code ≥ 2.1.156 at the mlxcel Anthropic Messages endpoint (/v1/messages, e.g. via ANTHROPIC_BASE_URL) makes every request fail with HTTP 422 before any generation:
Failed to deserialize the JSON body into the target type:
messages[1].role: unknown variant `system`, expected `user` or `assistant`
The Anthropic spec only allows user/assistant in messages (system text goes in the top-level system field). However, Claude Code ≥ 2.1.156 interleaves {"role":"system", …} turns inside the messages array (mid-conversation system reminders). mlxcel's AnthropicRole only models User/Assistant, so axum's Json extractor fails to deserialize and rejects the whole request with 422.
Call chain (this repo):
AnthropicRole(src/server/types/anthropic_request.rs) defines onlyUser/Assistantunder#[serde(rename_all = "lowercase")], sorole:"system"is an unknown variant.AnthropicMessage.role: AnthropicRole(src/server/types/anthropic_request.rs) is the field that fails to parse.anthropic_messageshandler (src/server/routes/anthropic.rs) takesJson(request): Json; the extractor returns 422 on the deserialize error (there is no explicit 422 anywhere in mlxcel, it is axum's default for a failedJson).append_message(src/server/anthropic_translator.rs:159) matchesAnthropicRoleexhaustively (User/Assistantonly).
Everything else Claude Code sends already returns 200: top-level system (string or blocks), tools, tool_use/tool_result, metadata, image blocks. Only a system role inside messages is rejected.
Version bisect (measured)
Captured the actual POST /v1/messages body each Claude Code build sends, via a local mock Anthropic endpoint driven by claude -p "hi":
| Claude Code | messages roles |
result |
|---|---|---|
| 2.1.145 | [user] |
200 ✅ |
| 2.1.153 | [user] |
200 ✅ |
| 2.1.156 | [user, system, …] |
422 ❌ |
| 2.1.159 / 162 / 165 / 169 | [user, system, …] |
422 ❌ |
The behavior changed between 2.1.153 and 2.1.156 (2.1.153 is the last build that keeps system top-level; 2.1.154/2.1.155 were not available to test).
Reproduction
# 200 — top-level system (spec-compliant)
curl -s -w '\n%{http_code}\n' http://127.0.0.1:8080/v1/messages -H 'content-type: application/json' \
-d '{"model":"","max_tokens":32,"system":"be terse","messages":[{"role":"user","content":"hi"}]}'
# 422 — system role inside messages (what Claude Code >= 2.1.156 sends)
curl -s -w '\n%{http_code}\n' http://127.0.0.1:8080/v1/messages -H 'content-type: application/json' \
-d '{"model":"","max_tokens":32,"messages":[{"role":"user","content":"hi"},{"role":"system","content":"be terse"},{"role":"user","content":"greet me"}]}'Impact
- mlxcel's
/v1/messagesis unusable as a Claude Code backend for any build ≥ 2.1.156 — it fails on the very first turn. - Workaround: pin Claude Code to ≤ 2.1.153.
- Scope is deserialization-level rejection, not corruption. Only the Anthropic endpoint is affected; the OpenAI
/v1/chat/completionspath already acceptssysteminmessages.
Suggested fix direction
- Add a
Systemvariant toAnthropicRole(andas_str) so the request deserializes instead of 422-ing. - Map it in
anthropic_translator::append_message(AnthropicRole::System => Role::System). ⚠️ Then verify the mid-array system turn actually renders through the chat template. Most templates (Qwen, etc.) only emitsystemat the head, so a system turn atmessages[1]may be dropped or fall through to the simple fallback renderer. If so, fold it into the adjacent user turn (or merge into the leading system block) rather than relying on the template to place a mid-conversation system message.
Acceptance Criteria
- A request whose
messagescontains asystem-role turn deserializes (no 422) and the system text influences generation. - Claude Code ≥ 2.1.156 connects and completes a turn end-to-end against
/v1/messages. - top-level
system,tools, andtool_use/tool_resultbehavior unchanged. - A translator test covers a
system-role turn insidemessages.
Environment
mlxcel v0.3.1, macOS (Apple Silicon), model mlx-community/Qwen3.6-27B-4bit, Claude Code 2.1.169.
Activity
[-][/v1/messages] Claude Code 2.1.169+의 messages 내 system 롤 턴에서 HTTP 422 (AnthropicRole이 user/assistant만 허용)[/-][+]fix(server): /v1/messages rejects Claude Code >= 2.1.156 with HTTP 422 (system-role turns inside messages; AnthropicRole has no System variant)[/+][-]fix(server): /v1/messages rejects Claude Code >= 2.1.156 with HTTP 422 (system-role turns inside messages; AnthropicRole has no System variant)[/-][+]fix(server): /v1/messages returns HTTP 422 for Claude Code >= 2.1.156 (system-role turns inside the messages array)[/+]