fix: correct chat format usage #1790
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Background
In our unit tests, we widely use the model
M4-ai/TinyMistral-248M-v2-Instruct-GGUFas the LlamaCpp test model.However, because this model was released early, its GGUF metadata does not contain a
chat_template:https://huggingface.co/Qwen/Qwen3-0.6B-GGUF/tree/main?show_file_info=Qwen3-0.6B-Q8_0.gguf
Despite this, the model is actually a chat model, and according to its documentation we should apply the correct chat template when performing chat completions:
https://huggingface.co/M4-ai/TinyMistral-248M-v2-Instruct-GGUF#recommended-prompt-template
However, Llama.cpp defaults to the
llama-2chat format:The correct format for this model in Llama.cpp is actually
qwen:Therefore, the model should be loaded with:
from llama_cpp import Llama from outlines import from_llamacpp llamacpp_model = Llama.from_pretrained( repo_id="M4-ai/TinyMistral-248M-v2-Instruct-GGUF", filename="TinyMistral-248M-v2-Instruct.Q4_K_M.gguf", + chat_format="qwen", ) model = from_llamacpp(llamacpp_model)Why this issue surfaces now
Previously, we did not emphasize chat completion behavior, so this problem did not appear in tests.
Under the new best-effort chat completion strategy, the incorrect chat template becomes visible.
For example, the unit test,
pytest tests/models/test_llamacpp.py::test_llamacpp_jsonfails immediately if the wrong chat template is applied. As you can see, the model's behavior will be erratic due to an incorrect template: