Skip to content

server: continue a final assistant message on /v1/messages and with continue_final_message - #3548

Open
ashhart wants to merge 3 commits into
mainfrom
lead/assistant-prefill
Open

ashhart wants to merge 3 commits into
mainfrom
lead/assistant-prefill

Conversation

@ashhart

@ashhart ashhart commented Nov 16, 2026

Copy link
Copy Markdown
Owner

Anthropic's Messages API treats a final assistant message as the start of the reply ("prefill"): the model continues it, so a client can force JSON with { or begin an answer's first words. TensorFold opened a new turn after that message instead. This continues it on /v1/messages, and on /v1/chat/completions behind the continue_final_message flag vLLM-compatible clients send.

What this changes

  • /v1/messages: when the last message is the assistant's, the reply continues it. content holds only the continuation, and usage.input_tokens and count_tokens count the prefill as input.

    • Behaviour change: a conversation that ends in an assistant message used to start a new turn, and now continues that message, which is Anthropic's meaning for it. One existing golden, anthropic/refuse-mid-system, ends that way. Its input drops from 21 tokens to 16 (checked by hand) and its request-log line carries the flag.
  • /v1/chat/completions: opt-in with "continue_final_message": true. "add_generation_prompt": false is accepted beside it, and true is refused. Without the flag nothing changes.

  • The prompt:

    • The server renders the conversation without a generation prompt, then finds where the final message's text ends using a second render with a probe placed right after that text. The probe must sit exactly at the cut with everything else unchanged. No request text may follow the cut, and the kept prompt must end with the message's own text.
    • Leading whitespace the Qwen and GLM templates strip is allowed.
    • This holds on every renderer we ship, including the DeepSeek-V4 encoder and Kimi. On Qwen3.8, GLM-5.3, DeepSeek-V4 and Kimi the result equals the plain thinking-off reply prompt followed by the prefill's tokens.
  • Thinking:

    • A request that turns thinking on itself is refused, since a reply can't continue inside a reasoning block. On chat that means chat_template_kwargs.enable_thinking: true, thinking on, or a reasoning_effort other than none. On Messages it means thinking enabled or adaptive.
    • When thinking is on only because of the server's default, the continued request runs exactly like an explicit enable_thinking: false: thinking-off prompt, plain content, and the same history_len, warm pass and prompt-cache prompt. A client that sends a trailing assistant message to a thinking model without asking for thinking gets its continuation, not a new 400.
  • Refused with a 400 naming the reason:

    • trailing whitespace in the final assistant text (Messages, Anthropic's wording);
    • a last message that isn't the assistant's when the flag is set;
    • tool calls in the final message;
    • a template that rewrites or drops the text, or writes request text after it;
    • a non-boolean flag.
  • Ordinary request otherwise: drafts stay on, structured output constrains the continuation from its first token, history_len is the conversation before the continued message, and the next turn's warm pass treats prefill and reply as one assistant message. Cortheon skips continued requests.

  • Issue: n/a. This came from the 16 Nov check of competitor gap docs against main (continuing a trailing assistant message is listed for colibri, and it's Anthropic's documented prefill); no issue or PR covered it.

  • Stacked on: main.

  • atc overlap --json: to run after review. Known text conflicts, for whichever lands later:

Checks on this head

  • Head commit: ebbd893 on main 08f1de5.
  • Lean checker: 337 on base and head, 0 new (tools/pr_check.py: 0 errors, 1 advisory warning, the PR number).
  • Tests:
    • zig build test-prefill-host (new): 32/33 with 1 skip (the real-tokenizer test) without TF_TOKENIZER_DIRS. With it set to the published Qwen3.8-27B snapshot, 33/33: 21 prompt tokens, the last 3 the message's own, after 11 of history. The real-tokenizer check was also run on Nemotron 3.5 Lightning and Qwen3.5-0.8B.
    • Negative controls: with the prompt path disabled the 4 route tests fail, and restoring main's thinking default fails the two thinking tests.
    • zig build test-server-cpu: 1488/1498 (10 skipped).
    • zig build test-golden: 335/348 equal, 0 unexpected (main's 13 known differences); security 55/55. The 12 prefill goldens run in their own server step.
    • zig build test-thinking-request-host test-prompt-interior-host: pass.
    • zig build server-cpu and zig build native -Dcpu=apple_m1 build. zig fmt --check and git diff --check are clean.
  • CI: pending.
  • Not run: a real model on Metal or CUDA, so reply tokens on a checkpoint are untested; /tokenize, Responses and Ollama don't take the flag.

Receipt

n/a for speed: a continued request is an ordinary prompt through the usual lane rounds. Its prompt is new by design, and every other request's prompt is unchanged (goldens). A device run on a served checkpoint is the follow-up.

Checklist

  • The title and description match the head commit.

  • New tests fail before the change, pass after it, and skip cleanly without their dependency.

  • Comments and docstrings are one line. No measurements or history in the source.

  • No personal data, machine names, internal hosts or local paths in the diff or its commits. No AI attribution lines.

  • Every token still goes through the lane rounds.

  • Synthena (Ash's Agent)

ashhart added a commit that referenced this pull request Nov 16, 2026
ashhart added a commit that referenced this pull request Nov 16, 2026
@ashhart
ashhart force-pushed the lead/assistant-prefill branch 4 times, most recently from b4af3a9 to c694798 Compare November 23, 2026 14:11
…sages' prefill does, instead of opening a new turn.
…g: chat_template_kwargs.enable_thinking or thinking
A final assistant message whose content is only whitespace renders no text in the continued turn: on Qwen 3.8 and GLM-5.3 the template strips it, pyLstrip is empty and both endsWith checks pass (a lone newline also passes the first, after the think close). The reply then generates without that text.

Refuse it with the same trailing-whitespace message the Messages route returns, before either cut() check. Tokens on valid continuations are unchanged.
@ashhart
ashhart force-pushed the lead/assistant-prefill branch from c694798 to 93ed612 Compare November 23, 2026 20:36

This branch has not been deployed

No deployments
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