Skip to content

server : accumulate generated text and tokens as parse input - #29876

Merged
aldehir merged 3 commits into
ggml-org:masterfrom
aldehir:common-chat-input
Oct 7, 2026
Merged

aldehir merged 3 commits into
ggml-org:masterfrom
aldehir:common-chat-input

Conversation

@aldehir

@aldehir aldehir commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Overview

I am working on implementing token-aware parsing in #29307. Before I mark that ready, I want to submit PRs for changes that touch different components so all the stakeholders can agree.

This PR implements common_chat_input struct to accumulate the generated text and tokens to feed into the parser. It uses two arrays, where each token id is present at the start of the token's bytes. This structure is to ensure that lookup during parsing happen in O(1).

text:   "<think>\nUse </think> to close.\n</think>\n\nDone."
tokens: [248068, -1,   -1,    -1,    -1, -1, -1,                     // <think>
         198,    9947, -1,    -1,                                    // \nUse
         672,    -1,   -1,    26003, -1, -1, -1, -1, 29,             //  </think>
         310,    -1,   -1,    3160,  -1, -1, -1, -1, -1, 13, 198,    //  to close.\n
         248069, -1,   -1,    -1,    -1, -1, -1, -1,                 // </think>
         271,    -1,   16936, -1,    -1, -1, 13]                     // \n\nDone.

Requirements

@aldehir
aldehir requested review from a team and pwilkin as code owners October 2, 2026 18:56
@github-actions github-actions Bot added testing Everything test related server labels Oct 2, 2026
Comment thread common/chat.cpp
input.append(piece, tokens[i]);
}
if (input.text != text) {
// the pieces do not give back the same text, keep the text without tokens

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should print a warning here so we can figure out what the reason behind the discrepancy is.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I don't think that's necessary. It will generate a warning on every request, and if this is an issue it should be caught when writing the parser since parsing the generation prompt would fail if token based but it falls back to text.

@pwilkin pwilkin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks OK to me, looking forward to this one :)

@aldehir
aldehir merged commit 18b5f8b into ggml-org:master Oct 7, 2026
12 checks passed
auRose94 pushed a commit to auRose94/llama.cpp that referenced this pull request Oct 28, 2026
…g#29876)

* server : collect text and token input

* common : add a tokenize helper that aligns tokens with bytes

* common : simplify tokenize logic
thefallentree added a commit to thefallentree/llama.cpp-gp100 that referenced this pull request Nov 14, 2026
…nd in the parsed output

Input: the server tokenized the rendered chat prompt as one string with
parse_special, so a file the agent read and sent back as a tool result
turned "</think>", "<tool_call>", "<|im_end|>" into the special tokens.
llama_tokenize_with_literals() takes byte ranges in which no special token
is matched, not even a user-defined one (Qwen's <think> and <tool_call> are
USER_DEFINED and match even with parse_special = false). The text runs
between real special tokens are still tokenized as a whole, so the tokens
across the part borders are those of the whole text. The jinja input marking
(ggml-org#30389) gives the ranges; tokenize_chat_prompt() follows the shape of the
open upstream PR ggml-org#32033 (--chat-input-special / --no-chat-input-special),
also for the count-tokens endpoints and mtmd (mtmd_tokenize_from_parts joins
adjacent text parts and passes the literal ranges). Fork default: disabled,
client text is text; a request may set "chat_input_special".

Output: with the vocab's special token table, a PEG literal or until
delimiter that names a special token matches only that token id in the
generated tokens (ggml-org#29876), so "</think>" or "<tool_call>" written as
ordinary tokens in reasoning or content no longer cuts it. A small stand-in
for the token-aware parsing of the open upstream PR ggml-org#29307. Text without
tokens parses as before.

Refs: mkyt docs/decisions.md P6-50, P6-51; ggml-org#28249, ggml-org#32033, ggml-org#29307, ggml-org#29876, ggml-org#30389, ggml-org#28250.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
thefallentree added a commit to thefallentree/llama.cpp-gp100 that referenced this pull request Nov 17, 2026
…nd in the parsed output

Input: the server tokenized the rendered chat prompt as one string with
parse_special, so a file the agent read and sent back as a tool result
turned "</think>", "<tool_call>", "<|im_end|>" into the special tokens.
llama_tokenize_with_literals() takes byte ranges in which no special token
is matched, not even a user-defined one (Qwen's <think> and <tool_call> are
USER_DEFINED and match even with parse_special = false). The text runs
between real special tokens are still tokenized as a whole, so the tokens
across the part borders are those of the whole text. The jinja input marking
(ggml-org#30389) gives the ranges; tokenize_chat_prompt() follows the shape of the
open upstream PR ggml-org#32033 (--chat-input-special / --no-chat-input-special),
also for the count-tokens endpoints and mtmd (mtmd_tokenize_from_parts joins
adjacent text parts and passes the literal ranges). Fork default: disabled,
client text is text; a request may set "chat_input_special".

Output: with the vocab's special token table, a PEG literal or until
delimiter that names a special token matches only that token id in the
generated tokens (ggml-org#29876), so "</think>" or "<tool_call>" written as
ordinary tokens in reasoning or content no longer cuts it. A small stand-in
for the token-aware parsing of the open upstream PR ggml-org#29307. Text without
tokens parses as before.

Refs: mkyt docs/decisions.md P6-50, P6-51; ggml-org#28249, ggml-org#32033, ggml-org#29307, ggml-org#29876, ggml-org#30389, ggml-org#28250.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
thefallentree added a commit to thefallentree/llama.cpp-gp100 that referenced this pull request Nov 19, 2026
…nd in the parsed output

Input: the server tokenized the rendered chat prompt as one string with
parse_special, so a file the agent read and sent back as a tool result
turned "</think>", "<tool_call>", "<|im_end|>" into the special tokens.
llama_tokenize_with_literals() takes byte ranges in which no special token
is matched, not even a user-defined one (Qwen's <think> and <tool_call> are
USER_DEFINED and match even with parse_special = false). The text runs
between real special tokens are still tokenized as a whole, so the tokens
across the part borders are those of the whole text. The jinja input marking
(ggml-org#30389) gives the ranges; tokenize_chat_prompt() follows the shape of the
open upstream PR ggml-org#32033 (--chat-input-special / --no-chat-input-special),
also for the count-tokens endpoints and mtmd (mtmd_tokenize_from_parts joins
adjacent text parts and passes the literal ranges). Fork default: disabled,
client text is text; a request may set "chat_input_special".

Output: with the vocab's special token table, a PEG literal or until
delimiter that names a special token matches only that token id in the
generated tokens (ggml-org#29876), so "</think>" or "<tool_call>" written as
ordinary tokens in reasoning or content no longer cuts it. A small stand-in
for the token-aware parsing of the open upstream PR ggml-org#29307. Text without
tokens parses as before.

Refs: mkyt docs/decisions.md P6-50, P6-51; ggml-org#28249, ggml-org#32033, ggml-org#29307, ggml-org#29876, ggml-org#30389, ggml-org#28250.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
thefallentree added a commit to thefallentree/llama.cpp-gp100 that referenced this pull request Nov 19, 2026
…nd in the parsed output

Input: the server tokenized the rendered chat prompt as one string with
parse_special, so a file the agent read and sent back as a tool result
turned "</think>", "<tool_call>", "<|im_end|>" into the special tokens.
llama_tokenize_with_literals() takes byte ranges in which no special token
is matched, not even a user-defined one (Qwen's <think> and <tool_call> are
USER_DEFINED and match even with parse_special = false). The text runs
between real special tokens are still tokenized as a whole, so the tokens
across the part borders are those of the whole text. The jinja input marking
(ggml-org#30389) gives the ranges; tokenize_chat_prompt() follows the shape of the
open upstream PR ggml-org#32033 (--chat-input-special / --no-chat-input-special),
also for the count-tokens endpoints and mtmd (mtmd_tokenize_from_parts joins
adjacent text parts and passes the literal ranges). Fork default: disabled,
client text is text; a request may set "chat_input_special".

Output: with the vocab's special token table, a PEG literal or until
delimiter that names a special token matches only that token id in the
generated tokens (ggml-org#29876), so "</think>" or "<tool_call>" written as
ordinary tokens in reasoning or content no longer cuts it. A small stand-in
for the token-aware parsing of the open upstream PR ggml-org#29307. Text without
tokens parses as before.

Refs: mkyt docs/decisions.md P6-50, P6-51; ggml-org#28249, ggml-org#32033, ggml-org#29307, ggml-org#29876, ggml-org#30389, ggml-org#28250.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
thefallentree added a commit to thefallentree/llama.cpp-gp100 that referenced this pull request Nov 20, 2026
…nd in the parsed output

Input: the server tokenized the rendered chat prompt as one string with
parse_special, so a file the agent read and sent back as a tool result
turned "</think>", "<tool_call>", "<|im_end|>" into the special tokens.
llama_tokenize_with_literals() takes byte ranges in which no special token
is matched, not even a user-defined one (Qwen's <think> and <tool_call> are
USER_DEFINED and match even with parse_special = false). The text runs
between real special tokens are still tokenized as a whole, so the tokens
across the part borders are those of the whole text. The jinja input marking
(ggml-org#30389) gives the ranges; tokenize_chat_prompt() follows the shape of the
open upstream PR ggml-org#32033 (--chat-input-special / --no-chat-input-special),
also for the count-tokens endpoints and mtmd (mtmd_tokenize_from_parts joins
adjacent text parts and passes the literal ranges). Fork default: disabled,
client text is text; a request may set "chat_input_special".

Output: with the vocab's special token table, a PEG literal or until
delimiter that names a special token matches only that token id in the
generated tokens (ggml-org#29876), so "</think>" or "<tool_call>" written as
ordinary tokens in reasoning or content no longer cuts it. A small stand-in
for the token-aware parsing of the open upstream PR ggml-org#29307. Text without
tokens parses as before.

Refs: mkyt docs/decisions.md P6-50, P6-51; ggml-org#28249, ggml-org#32033, ggml-org#29307, ggml-org#29876, ggml-org#30389, ggml-org#28250.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

server testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants