Skip to content

server: /v1/completions takes suffix, a fill-in-the-middle prompt from the tokenizer's own FIM tokens - #3528

Draft
ashhart wants to merge 2 commits into
mainfrom
lead/completions-fim
Draft

ashhart wants to merge 2 commits into
mainfrom
lead/completions-fim

Conversation

@ashhart

@ashhart ashhart commented Nov 16, 2026

Copy link
Copy Markdown
Owner

Code-completion clients (editor plugins, and APIs in the style of DeepSeek's and Codestral's FIM endpoints) send OpenAI's suffix on /v1/completions: prompt is the code before the cursor, suffix the code after it, and the reply is the middle. TensorFold ignored suffix. This builds the model's fill-in-the-middle prompt from the tokenizer's own FIM tokens.

What this changes

  • The prompt: in prefix-suffix-middle order, from the tokenizer's own tokens. First whatever the tokenizer puts before any prompt (a beginning-of-text token, if it has one), then <|fim_prefix|>, the prompt, <|fim_suffix|>, the suffix and <|fim_middle|>.

    • These are the Qwen spellings. The Qwen3.5 and Qwen3.8 tokenizers hold them as added tokens: ids 248060, 248062 and 248061 on Qwen3.8.
    • A tokenizer must hold all three whole. Nemotron 3.5's has none, so it refuses suffix.
  • Client text: the prompt and suffix encode as plain text, so a marker spelled inside the code stays text and can't open a second FIM region.

  • Where a reply ends: at the model's own end tokens, and at <|fim_pad|> and <|endoftext|> where the vocabulary has them. They are left out of the text like any end token, and ignore_eos: true turns them off too.

    • Without suffix, the end set is exactly the model's, as before. The generation now takes its end tokens from the request, which equals srv.eos for every existing request.
  • Streaming works.

  • No suffix: an absent, null or empty suffix leaves the request unchanged.

  • Refused with a 400 naming suffix:

    • a non-string suffix;
    • a prompt of token ids or a list, or a body with messages;
    • a tokenizer without the three markers;
    • suffix with echo: true, since echo would return the FIM prompt, markers included.
  • Issue: n/a. This came from the 16 Nov check of competitor gap docs against main (llama.cpp's infill); no issue or PR covered it.

  • Stacked on: server: /v1/completions takes echo, the prompt's text and legacy logprobs ahead of an unchanged reply #3526 (echo on /v1/completions), commits 01b7063cde and 12564d479c. This PR adds a1e24f3b9e. Both PRs touch chat.Input, chat.zig's generation setup, the golden corpus and docs/api.md. Stacking them saves a six-file conflict at landing. Land server: /v1/completions takes echo, the prompt's text and legacy logprobs ahead of an unchanged reply #3526 first, or this PR's head brings both.

  • atc overlap --json: to run after review.

Checks on this head

  • Head commit: a1e24f3 on server: /v1/completions takes echo, the prompt's text and legacy logprobs ahead of an unchanged reply #3526's head 12564d4 (main e7e2424).
  • Lean checker: 337 on base and head, 0 new (tools/pr_check.py: 0 errors, 1 advisory warning).
  • Tests:
    • zig build test-completions-fim-host (new step): 26/26. Against main's sources, 3 of the 5 new tests fail; the other two are an unchanged-behaviour check and the real-tokenizer check.
    • The real-tokenizer check, with TF_TOKENIZER_DIRS set to the published Qwen3.8-27B, Qwen3.5-0.8B and Nemotron 3.5 Lightning snapshots: the Qwen prompts carry their three whole marks, and Nemotron refuses.
    • zig build test-logprob-offers-host test-eos-policy-host: pass, so echo's tests still pass on this head.
    • zig build test-golden: 328/341 equal, 0 unexpected. That's both PRs' six new cases (suffix-null, suffix-empty, suffix-unsupported from this one), regenerated with freeze_cases.py. Security goldens 55/55.
    • zig fmt --check, git diff --check: clean.
  • CI: pending.
  • Not run: whether served models write good middles. That's a quality question for a device run with a coder checkpoint.

Receipt

n/a for speed: without suffix, requests are unchanged, with the same prompt ids and the same end set. With it, the prompt is new and the reply runs through the usual lane rounds. A device run on a served Qwen 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)

…robs ahead of an unchanged reply

echo: true puts the prompt's decoded ids in front of text and, with logprobs, each prompt
token's entry (null for the first) ahead of the reply's, whose entries stay as without echo.
max_tokens 0 with echo returns the prompt alone. Engines without prompt_logprobs, streams,
messages bodies and non-boolean values are refused by name; prompt_logprobs keeps its own count.
…m the tokenizer's own FIM tokens

With a nonempty suffix the server writes <|fim_prefix|>, the prompt, <|fim_suffix|>, the suffix and
<|fim_middle|> from the tokenizer's own tokens, the code read as plain text, and the reply also ends at
<|fim_pad|> and <|endoftext|> where the vocabulary holds them. Tokenizers without the three tokens,
token-id or list prompts, messages bodies and non-string values are refused by name; without a suffix
nothing changes.
@ashhart
ashhart force-pushed the lead/completions-fim branch from 391bb1d to 4f4f63f Compare November 22, 2026 04:54

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