Codex

500 “System message must be at the beginning” (Codex + llama.cpp + Qwen)

Last checked

The error

500 System message must be at the beginning.

Error: Jinja Exception: System message must be at the beginning.
Unable to generate parser for this template. Automatic parser generation failed

Returned by llama-server on the first turn, every time. Nothing is wrong with your model file or GPU.

Codex sends its instructions as one system message, then sends developer messages (sandbox permissions, your developer instructions) as additional system messages. llama.cpp maps every developer message to the system role. Qwen3.5 and later chat templates allow only one system message and throw an exception on the second one, so llama-server returns 500. The fix is to patch the one raise_exception line in the Jinja template so later system messages render as labelled user messages instead.

Why it happens

Qwen3.5 and later chat templates contain a guard that raises an exception if any system message is not the very first message in the list. Codex’s developer messages get remapped to system by llama.cpp, giving the template two system messages and triggering the guard.

  1. Codex uses the Responses API and sends its main instructions in an instructions field, then puts sandbox permissions and developer instructions in developer messages.
  2. llama.cpp’s Responses endpoint turns instructions into the first system message, then renames every developer message to system (for all templates except GPT-OSS). The request now has two or more system messages.
  3. Qwen3.5+ chat templates enforce a single system message with: {%- if not loop.first %} {{- raise_exception(‘System message must be at the beginning.’) }}. The second system message is not first, so the template throws.
  4. Older Qwen3-8B templates did not have this guard, which is why this error starts with the Qwen3.5 generation.

The fix

  1. 1 Save the template currently in use: curl -s http://localhost:8080/props | jq -r .chat_template > qwen_template.jinja
  2. 2 Open qwen_template.jinja, find the raise_exception(‘System message must be at the beginning.’) line, and replace that entire line with: {{- ‘<|im_start|>user [‘ ~ message.role ~ ‘ instructions] ‘ ~ content ~ ‘<|im_end|> ‘ }}
  3. 3 Save the edited file as qwen-patched.jinja. Only that one line changes.
  4. 4 Restart llama-server with the patched template: llama-server -m /path/to/model.gguf --jinja --chat-template-file /path/to/qwen-patched.jinja
  5. 5 Verify by running Codex again. The first system message stays a system message; Codex’s permissions block comes through as a user message starting with [system instructions].
curl -s http://localhost:8080/props | jq -r .chat_template > qwen_template.jinja

The trade-off: rules become suggestions

This is a workaround, not a cure. Codex’s sandbox mode, approval policy, and developer instructions now reach the model as a user message rather than a system message.

Most Qwen models still follow them, but the model may treat them as a suggestion rather than a rule. Keep Codex’s sandbox on while you use a patched template, so a missed instruction cannot do much.

Has it been fixed upstream?

Not as of 27 October 2026. The llama.cpp issue tracking this error has no maintainer fix linked, and the Qwen3.8-27B template on Hugging Face still contains the raise_exception line.

Before patching, check two things: your GGUF repository may have pushed a fixed template (compare the file date with your download), and a newer llama.cpp build may change how developer messages are mapped. Users in the llama.cpp thread report the same error from other agents that send mid-conversation system messages, so the patch helps beyond Codex.

Still failing?

  • “Unexpected message role.” Your llama.cpp build is not mapping developer to system. Update llama.cpp.
  • Tool calls stop working after the patch. Some community templates drop tool support. Patch only the one line above on the template your server already uses.
  • It works, then breaks after a model update. The new GGUF brought its own template. Point --chat-template-file at your patched copy again.

Related errors

Hit a different error?

Paste any agent error and get the cause and fix in seconds.

Open the decoder

Frequently asked questions

What causes “System message must be at the beginning” in Codex?

Codex sends developer messages alongside its instructions, and llama.cpp turns each one into a system message. Qwen3.5 and later chat templates allow only one system message, at the very start, so the template raises an exception and llama-server returns a 500.

Does updating llama.cpp fix it?

Not as of 27 October 2026. The error comes from the model’s chat template, and the current Qwen3.8 template still raises it. Update anyway, then use a patched template with --jinja --chat-template-file until the template changes.

Will the patched template change how the model behaves?

Slightly. Codex’s later instructions arrive as a user message labelled “[system instructions]” instead of a system message, so the model may follow them less strictly. Keep Codex’s sandbox on while you use it.

Does this affect other agents besides Codex?

Yes. Any agent that sends mid-conversation developer or system messages to llama-server will hit this error with a Qwen3.5+ model. The same template patch fixes it for those agents too.

Stop firefighting agent errors

Decoding errors one at a time is the manual version of what BetterClaw automates. Run your agents on a no-code AI agent platform with managed models, retries and config validation built in.

Free plan available · Pro $49/mo · BYOK · 7-day free trial