Skip to content

server: quoted added spellings stay plain through the chat route with real tokenizers (#2399) - #2400

Merged
ashhart merged 2 commits into
mainfrom
lead/quoted-markers
Nov 7, 2026
Merged

ashhart merged 2 commits into
mainfrom
lead/quoted-markers

Conversation

@ashhart

@ashhart ashhart commented Nov 6, 2026

Copy link
Copy Markdown
Owner

Part of #2399.

#2399 reports Qwen3.8-Flash-Next ending a reply where it quotes <|im_end|>. Main already encodes request text with added-token spellings masked (core/tokenizer/tokenizer.zig encodeMasked, since #1117), and stops only on the end token's id, so the end in the report was drawn by the model. Our tests showed the mask with small fixture tokenizers. This one shows it with the real tokenizers and chat templates people report on.

What it checks

server/quoted_markers_test.zig, run by zig build test-quoted-markers-host, reads the directories in TF_TOKENIZER_DIRS (:-separated, ; on Windows), each holding a model's tokenizer.json, tokenizer_config.json and chat template. Unset, it skips with a message, so CI is unchanged.

For each directory it builds a chat request through the chat route's own path (openai.chatInput, then chat.prepare, which renders and encodes as /v1/chat/completions does): a user message, an assistant message with reasoning and a tool call, a tool result, and the tool offered. Then, one field at a time, every added-token spelling of that vocabulary is quoted in backticks in that field: the user message, the assistant message, the reasoning, the tool-call arguments, the tool result and the tool schema. For each it checks:

  • every added token id appears exactly as often as in the same request without the quotes, that is, only where the template writes it;
  • each quoted spelling is in the decoded prompt, so no field passes by being dropped or escaped;
  • the template's own <|im_end|> still encodes as its id.

A second test keeps the refusal honest: input that main refuses with UnsafeAddedTokenMask is still refused.

With the mask removed, the first test fails (396 id mismatches); with only the refusal removed, the second fails. Both restored.

Result

Run on macOS with Qwen3.8-Flash-Next and Qwen3.8-27B: 20 of 20 pass; without the variable, both skip. Both vocabularies have the same 33 added tokens (ids 248044 to 248076). <|im_end|> (248046), <tool_call> (248058) and </tool_call> (248059) are added tokens; <|tool_call_begin|> is not, and encodes to 7 ordinary tokens. All 33 spellings stay plain text in all six fields.

The tokenizer files are not in the repository. No engine or kernel code changes.

@ashhart
ashhart force-pushed the lead/quoted-markers branch from 7e9cc39 to 5557407 Compare November 7, 2026 07:59
@ashhart
ashhart merged commit 1f370dc into main Nov 7, 2026
0 of 11 checks passed
@ashhart
ashhart deleted the lead/quoted-markers branch November 10, 2026 16:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant