Skip to content

fix(server): /v1/messages returns HTTP 422 for Claude Code >= 2.1.156 (system-role turns inside the messages array) #349

Closed

Description

@w200kr

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 only User and Assistant, so role:"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) takes Json(request): Json<AnthropicRequest> (anthropic.rs:68). There is no explicit 422 in mlxcel; this is axum's default when the Json extractor fails to deserialize.
  • append_message (src/server/anthropic_translator.rs:158) matches AnthropicRole exhaustively over User/Assistant only (anthropic_translator.rs:159-161).

Proposed Solution

  1. Add a System variant to AnthropicRole and to AnthropicRole::as_str in src/server/types/anthropic_request.rs so the request deserializes instead of returning 422.
  2. Map it in append_message (src/server/anthropic_translator.rs:159) as AnthropicRole::System => Role::System. The internal Role enum (src/server/types/request.rs:23) already has a System variant, so this mapping is available with no new type work.
  3. Verify the mid-array system turn actually renders through the chat template. Many templates emit system only at the head of the conversation, so a system turn at messages[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 system field is already converted to a Role::System message in anthropic_translator.rs (see the system-block handling around anthropic_translator.rs:84), so a Role::System message 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 around anthropic_translator.rs:560); add a case there for a system-role turn inside messages.

Acceptance Criteria

  • A request whose messages contains a system-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, and tool_use/tool_result behavior is unchanged.
  • A translator test covers a system-role turn inside messages.

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 only User/Assistant under #[serde(rename_all = "lowercase")], so role:"system" is an unknown variant.
  • AnthropicMessage.role: AnthropicRole (src/server/types/anthropic_request.rs) is the field that fails to parse.
  • anthropic_messages handler (src/server/routes/anthropic.rs) takes Json(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 failed Json).
  • append_message (src/server/anthropic_translator.rs:159) matches AnthropicRole exhaustively (User/Assistant only).

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/messages is 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/completions path already accepts system in messages.

Suggested fix direction

  1. Add a System variant to AnthropicRole (and as_str) so the request deserializes instead of 422-ing.
  2. Map it in anthropic_translator::append_message (AnthropicRole::System => Role::System).
  3. ⚠️ Then verify the mid-array system turn actually renders through the chat template. Most templates (Qwen, etc.) only emit system at the head, so a system turn at messages[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 messages contains a system-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, and tool_use/tool_result behavior unchanged.
  • A translator test covers a system-role turn inside messages.

Environment

mlxcel v0.3.1, macOS (Apple Silicon), model mlx-community/Qwen3.6-27B-4bit, Claude Code 2.1.169.

Activity

  1. changed the title [-][/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)[/+] on Jun 18, 2026
  2. changed the title [-]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)[/+] on Jun 18, 2026
  3. added
    status:ready
    type:bug
    priority:high
    area:core
    area:inference
    on Jun 18, 2026
  4. self-assigned this
    on Jun 21, 2026
  5. added
    status:in-progress
    status:review
    status:done
    and removed
    status:ready
    status:in-progress
    status:review
    on Jun 21, 2026
  6. added this to the 0.3 milestone on Jun 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:corearea:inferencepriority:highstatus:donetype:bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions