Repository navigation
fix: accept system-role turns inside /v1/messages array - #380
Conversation
Claude Code >= 2.1.156 interleaves `{"role":"system", ...}` turns inside the `messages` array for mid-conversation reminders. `AnthropicRole` only modeled `user`/`assistant`, so axum's `Json` extractor rejected every such request with HTTP 422 before any generation ran. The OpenAI `/v1/chat/completions` path was unaffected.
Add a `System` variant to `AnthropicRole` (and `as_str`) so the request deserializes, then fold each mid-conversation system turn into an adjacent user turn so its text reaches the model regardless of the chat template. Many production templates (Qwen-family, Llama 3) special-case `messages[0]` for the system block and their per-turn loop only handles user/assistant/tool, so a bare `system` turn at `messages[1]` would be silently dropped and never influence generation.
Strategy: leading system turns merge into the single head system block together with the top-level `system` field (neither is dropped); a mid-conversation reminder is prepended to the following user turn, keeping its positional meaning without emitting a second `system` message or creating a new user turn; a trailing reminder with no following user turn is appended to the preceding user turn or merged into the head block.
Tests: deserialization of a system-role turn inside `messages` (no 422), folding into the following user turn, top-level plus mid-array system both surviving, leading-array merge with top-level system, trailing reminder, and a multimodal fold that preserves the image. A head-only Qwen-style template render proves the folded text is not dropped.
The mid-array system fold added the reminder as a separate ContentPart::Text on multimodal user turns. MessageContent::text() joins parts with an empty string, so the reminder ran into the user's words ("watch outsee this" instead of "watch out\n\nsee this"). Merge the reminder into the existing text part with the same blank-line separator the text-only path uses, falling back to a fresh part only for image-only turns. Strengthen the multimodal test to assert the exact separated output and add a consecutive mid-array system turns case.
Implementation Review SummaryIntent
Findings Addressed
Remaining Items
Verification
Edge-case tests added: exact-separator assertion on the multimodal fold and a consecutive-mid-array-system-turns case. |
PR Finalization CompleteTests30 tests in DocumentationAdded an Lint / Format
All checks pass. Ready for merge. |
* fix(server): accept system-role turns inside /v1/messages array
Claude Code >= 2.1.156 interleaves `{"role":"system", ...}` turns inside the `messages` array for mid-conversation reminders. `AnthropicRole` only modeled `user`/`assistant`, so axum's `Json` extractor rejected every such request with HTTP 422 before any generation ran. The OpenAI `/v1/chat/completions` path was unaffected.
Add a `System` variant to `AnthropicRole` (and `as_str`) so the request deserializes, then fold each mid-conversation system turn into an adjacent user turn so its text reaches the model regardless of the chat template. Many production templates (Qwen-family, Llama 3) special-case `messages[0]` for the system block and their per-turn loop only handles user/assistant/tool, so a bare `system` turn at `messages[1]` would be silently dropped and never influence generation.
Strategy: leading system turns merge into the single head system block together with the top-level `system` field (neither is dropped); a mid-conversation reminder is prepended to the following user turn, keeping its positional meaning without emitting a second `system` message or creating a new user turn; a trailing reminder with no following user turn is appended to the preceding user turn or merged into the head block.
Tests: deserialization of a system-role turn inside `messages` (no 422), folding into the following user turn, top-level plus mid-array system both surviving, leading-array merge with top-level system, trailing reminder, and a multimodal fold that preserves the image. A head-only Qwen-style template render proves the folded text is not dropped.
* fix(server): separate folded system reminder from multimodal user text
The mid-array system fold added the reminder as a separate ContentPart::Text on multimodal user turns. MessageContent::text() joins parts with an empty string, so the reminder ran into the user's words ("watch outsee this" instead of "watch out\n\nsee this"). Merge the reminder into the existing text part with the same blank-line separator the text-only path uses, falling back to a fresh part only for image-only turns. Strengthen the multimodal test to assert the exact separated output and add a consecutive mid-array system turns case.
* docs(changelog): add Unreleased entry for lablup#380 system-role fix
Summary
/v1/messagesrejected every request from Claude Code 2.1.156 and later with HTTP 422 before any generation. Starting with that build, the client interleaves{"role":"system", ...}turns inside themessagesarray for mid-conversation reminders, andAnthropicRoleonly modeleduser/assistant, so axum'sJsonextractor failed to deserialize the body. This restores the endpoint for current client builds and guarantees the interleaved system text actually reaches the model.What changed
src/server/types/anthropic_request.rs: add aSystemvariant toAnthropicRole(and toas_str, returning"system") so the request deserializes instead of 422-ing. Updated the role doc comments to explain the non-spec interleaving.src/server/anthropic_translator.rs: mapAnthropicRole::Systemto the internalRole::Systeminappend_message, then add afold_system_messagespass that relocates any non-head system turn so its text renders under any chat template.Why folding is required (not just the enum variant)
Adding the variant alone stops the 422 but does not guarantee the system text influences generation. Many production chat templates (Qwen-family, Llama 3) special-case
messages[0]for the system block and their{% for message in messages %}loop only handlesuser/assistant/tool. A baresystemturn atmessages[1]is silently dropped by such a template, so the reminder would never reach the model. The fold pass moves the text into a turn every template renders.Merge strategy
systemturns (before the first user/assistant/tool turn) merge into the single head system block, together with the top-levelsystemfield when present, so neither is dropped.systemturn is prepended to the following user turn. Claude Code emits the reminder immediately before a user turn, so this preserves its positional/contextual meaning; folding into an existing turn means we never depend on a template rendering a non-headsystemrole, and no new user turn is created so user/assistant alternation is unchanged.systemturn with no following user turn is appended to the preceding user turn, or merged into the head block as a last resort.Test plan
cargo test --lib server::anthropic_translator(29 passed, including 7 new cases: deserialization without 422, fold into following user turn, top-level plus mid-array system both surviving, leading-array merge with top-level system, trailing reminder preserved, multimodal fold preserving the image, and the unchanged top-level-system path)cargo test --lib server::types::anthropic_request(10 passed, including a new deserialization regression test for a system-role turn insidemessages)cargo clippy --lib --tests -- -D warnings(clean)cargo fmt --check(clean)The new translator tests render the folded output through a head-only Qwen-style template (system only at
messages[0], per-turn loop handles user/assistant only) and assert the reminder text survives, proving it was folded rather than left as an orphan mid-array system turn that such a template would drop.Follow-up (manual)
Live end-to-end verification with a real Claude Code 2.1.156+ client against
/v1/messagescould not be run here and is left as a manual check.Closes #349