Skip to content

server : clean up built-in template detection - #11026

Merged
ngxson merged 4 commits into
ggml-org:masterfrom
ngxson:xsn/server_chat_template_detect
Dec 31, 2024
Merged

ngxson merged 4 commits into
ggml-org:masterfrom
ngxson:xsn/server_chat_template_detect

Conversation

@ngxson

@ngxson ngxson commented Dec 31, 2024

Copy link
Copy Markdown
Collaborator

Ref: #10900 (comment)

Bug: when starting the server, templates longer than 2048 bytes is shown as "not supported" in the log output, while it's still being formatted correctly in /v1/chat/completions

@github-actions github-actions Bot added the python python script changes label Dec 31, 2024
Comment thread common/common.cpp Outdated
static const char * template_key = "tokenizer.chat_template";
// call with NULL buffer to get the total size of the string
int32_t res = llama_model_meta_val_str(model, template_key, NULL, 0);
if (res < 2) {

@slaren slaren Dec 31, 2024 •

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.

What's the reason for the 2 here? llama_model_meta_val_str returns either -1 if the key is not found, or the length of the string.

@ngxson ngxson Dec 31, 2024 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Hmm yeah someone added this in the PR to fix the null terminator. I suppose that it's to make sure model_template.size() - 1 stay positive.

In any case, we can change it to

Suggested change
if (res < 2) {
if (res < 0) {

Then below:

return std::string(model_template.data(), res);

What do you think?

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.

model_template.size() should always be at least 1, since the size is res + 1. So I don't expect the -1 to cause issues, but that should also work.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I ended up just flipping the condition to if (res > 0), it's more readable this way.

@ngxson
ngxson merged commit 45095a6 into ggml-org:master Dec 31, 2024
arthw pushed a commit to arthw/llama.cpp that referenced this pull request Feb 26, 2025
* server : clean up built-in template detection

* fix compilation

* add chat template test

* fix condition
Seunghhon pushed a commit to Seunghhon/llama.cpp that referenced this pull request Apr 26, 2026
* server : clean up built-in template detection

* fix compilation

* add chat template test

* fix condition
ljubomirj pushed a commit to ljubomirj/llama.cpp that referenced this pull request May 6, 2026
* server : clean up built-in template detection

* fix compilation

* add chat template test

* fix condition
my-other-github-account pushed a commit to my-other-github-account/llama.cpp that referenced this pull request May 15, 2026
* server : clean up built-in template detection

* fix compilation

* add chat template test

* fix condition
AlexiAlp pushed a commit to minghaop/llama.cpp that referenced this pull request Jun 2, 2026
* server : clean up built-in template detection

* fix compilation

* add chat template test

* fix condition
AlexiAlp pushed a commit to minghaop/llama.cpp that referenced this pull request Jun 2, 2026
* server : clean up built-in template detection

* fix compilation

* add chat template test

* fix condition
fukuro-kun pushed a commit to fukuro-kun/fukuro-llama-cpp-turboquant that referenced this pull request Jul 5, 2026
* server : clean up built-in template detection

* fix compilation

* add chat template test

* fix condition
zommiommy pushed a commit to zommiommy/llama.cpp that referenced this pull request Aug 18, 2026
* server : clean up built-in template detection

* fix compilation

* add chat template test

* fix condition
frostyautumnleaf pushed a commit to frostyautumnleaf/llama.cpp that referenced this pull request Oct 5, 2026
* server : clean up built-in template detection

* fix compilation

* add chat template test

* fix condition
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

examples python python script changes server

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants