[GH-ISSUE #56] Anthropic API: mid-conversation "system" message crashes the chat template ("System message must be at the beginning") #31

Closed
opened 2026-11-04 01:39:10 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @Nicolas0315 on GitHub (Sep 28, 2026).
Original GitHub issue: https://github.com/Niko1221/Strata/issues/56

Summary

anthropic_to_messages (serve/frontend.py) passes messages with role: "system" that appear inside messages[] straight to the chat template, which raises:

jinja2.exceptions.TemplateError: System message must be at the beginning.

The request fails and the client connection is dropped (Claude Code shows API Error: Connection dropped (ECONNRESET)).

Who sends this

Claude Code (2.1.283) puts extra context (e.g. SessionStart hook output wrapped in <system-reminder>) into the conversation as a {"role": "system", ...} entry after the first user message, in addition to the top-level system field. Captured request shape:

system: list
roles:  ["user", "system"]

Repro (commit d551edf)

curl -s 'http://127.0.0.1:8080/v1/messages' -H 'Content-Type: application/json' -H 'anthropic-version: 2023-06-01' -d '{
  "model":"x","max_tokens":100,
  "system":[{"type":"text","text":"You are terse."}],
  "messages":[
    {"role":"user","content":[{"type":"text","text":"1+1? digits only"}]},
    {"role":"system","content":[{"type":"text","text":"<system-reminder>answer in digits</system-reminder>"}]}
  ]}'

→ TemplateError in the server log, no response.

Suggested fix

Only the first system message can go to the template, so convert later ones to user (the content is already wrapped in <system-reminder>, so meaning is preserved). Minimal patch:

     for m in req.get("messages", []):
+        if m.get("role") in ("system", "developer"):     # mid-conversation system (e.g. Claude Code hooks)
+            m = dict(m, role="user")
         content = m.get("content")

Merging them into the leading system prompt would also work, but changes the prompt prefix and breaks KV reuse on every turn, so converting in place seems better.

Verified locally (Windows / RTX 4090 / IQ3_S, together with the query-string fix from the other issue): the repro returns 200 with the expected answer, and Claude Code completes a real edit-and-test task end to end (~94 s, prompt ~17-25k tokens, KV reuse working).

Originally created by @Nicolas0315 on GitHub (Sep 28, 2026). Original GitHub issue: https://github.com/Niko1221/Strata/issues/56 ## Summary `anthropic_to_messages` (serve/frontend.py) passes messages with `role: "system"` that appear *inside* `messages[]` straight to the chat template, which raises: ``` jinja2.exceptions.TemplateError: System message must be at the beginning. ``` The request fails and the client connection is dropped (Claude Code shows `API Error: Connection dropped (ECONNRESET)`). ## Who sends this Claude Code (2.1.283) puts extra context (e.g. SessionStart hook output wrapped in `<system-reminder>`) into the conversation as a `{"role": "system", ...}` entry after the first user message, in addition to the top-level `system` field. Captured request shape: ``` system: list roles: ["user", "system"] ``` ## Repro (commit d551edf) ```bash curl -s 'http://127.0.0.1:8080/v1/messages' -H 'Content-Type: application/json' -H 'anthropic-version: 2023-06-01' -d '{ "model":"x","max_tokens":100, "system":[{"type":"text","text":"You are terse."}], "messages":[ {"role":"user","content":[{"type":"text","text":"1+1? digits only"}]}, {"role":"system","content":[{"type":"text","text":"<system-reminder>answer in digits</system-reminder>"}]} ]}' ``` → TemplateError in the server log, no response. ## Suggested fix Only the first system message can go to the template, so convert later ones to `user` (the content is already wrapped in `<system-reminder>`, so meaning is preserved). Minimal patch: ```diff for m in req.get("messages", []): + if m.get("role") in ("system", "developer"): # mid-conversation system (e.g. Claude Code hooks) + m = dict(m, role="user") content = m.get("content") ``` Merging them into the leading system prompt would also work, but changes the prompt prefix and breaks KV reuse on every turn, so converting in place seems better. Verified locally (Windows / RTX 4090 / IQ3_S, together with the query-string fix from the other issue): the repro returns 200 with the expected answer, and Claude Code completes a real edit-and-test task end to end (~94 s, prompt ~17-25k tokens, KV reuse working).
Author
Owner

@Niko1221 commented on GitHub (Sep 28, 2026):

Thanks - fixed in v0.1.17 the way you suggested: a system message after the first one becomes a user message in place (the first stays the system prompt), so the prompt's start does not change and the conversation cache keeps reusing it. The same applies to the OpenAI API, where some clients send a late developer message. Tested with your request (and Claude Code's shape end to end on the real engine: ?beta=true + a mid-conversation system message answered correctly). Please reopen if you hit another shape it rejects.

<!-- gh-comment-id:5871075543 --> @Niko1221 commented on GitHub (Sep 28, 2026): Thanks - fixed in [v0.1.17](https://github.com/Niko1221/Strata/releases/tag/v0.1.17) the way you suggested: a system message after the first one becomes a user message in place (the first stays the system prompt), so the prompt's start does not change and the conversation cache keeps reusing it. The same applies to the OpenAI API, where some clients send a late `developer` message. Tested with your request (and Claude Code's shape end to end on the real engine: `?beta=true` + a mid-conversation system message answered correctly). Please reopen if you hit another shape it rejects.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#31
No description provided.