Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
- Chat templates now receive `enableThinking` as `enable_thinking` on every request, including `responseFormat` and generic tool-call routes, as in llama.cpp, so templates such as GLM-4.6, GLM-4.7-Flash, GLM-OCR, SmolLM3, Apertus and DeepSeek-R1 distills render thinking as llama-server does for the same flag. `enableThinking` still defaults to `true`, so Apertus now deliberates and DeepSeek-R1 distills open a thought unless you pass `false` ([#1465](https://github.com/leehack/llamadart/issues/1465)).
- A `responseFormat` or tool-call grammar after a thought the prompt leaves open now lets the model finish the thought first, as llama-server does, instead of returning the JSON as reasoning; a thought the template opens stays open with `enableThinking: false`, as in llama-server ([#1465](https://github.com/leehack/llamadart/issues/1465)).
- A required tool call after a thought the prompt leaves open now lets Cohere2 MoE, Nemotron 3 Nano, MiniCPM5 and Hy3 models finish the thought first, as llama-server does; MiniCPM5 previously wrote the call inside the thought and returned no tool call.
- A template's own thinking setting passed in `chatTemplateKwargs`, such as `reasoning_effort` for Hy3 and Cohere2 MoE or `thinking_mode` for MiniMax M3, is no longer replaced by the value llamadart derives from `enableThinking`, so it renders as in llama-server ([#750](https://github.com/leehack/llamadart/issues/750)).
- Cohere2 MoE and Command R7B models on native llama.cpp keep the thinking, action and response markers their GGUFs store as control tokens, as llama-server does, so their thoughts, tool calls and text parse instead of all landing in reasoning or showing up as raw JSON ([#1505](https://github.com/leehack/llamadart/issues/1505)).
- A native release override's `assets.json.absent` record dated in the future is re-checked, and a record whose re-check fails or returns a page that is not JSON is kept and renewed instead of failing the build or retrying on every offline build; an incomplete cached core archive that matches its cached `SHA256SUMS` no longer tells an offline build to pre-seed `SHA256SUMS` ([#1506](https://github.com/leehack/llamadart/issues/1506)).
- `clear` removes a stray `.removal` directory in a model cache root instead of reporting it in use forever ([#1507](https://github.com/leehack/llamadart/issues/1507)).
Expand Down
5 changes: 5 additions & 0 deletions lib/src/core/engine/engine.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1631,6 +1631,11 @@ class LlamaEngine {
/// A boolean `enable_thinking` in [chatTemplateKwargs], or failing that a
/// boolean `thinking`, overrides [enableThinking] for rendering, parsing and
/// streaming, as in llama-server; other values leave [enableThinking].
/// A template's own thinking setting in [chatTemplateKwargs], such as the
/// `reasoning_effort` of Hy3 and Cohere2 MoE or the `thinking_mode` of
/// MiniMax M3, replaces the value llamadart derives from [enableThinking],
/// as in llama-server, while `enableThinking: false` still ends a thought
/// the prompt leaves open.
/// Use [templateNow] to set deterministic template time context.
///
/// Pass [responseFormat] to request strict structured output through
Expand Down
6 changes: 5 additions & 1 deletion lib/src/core/template/chat_template_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -180,16 +180,20 @@ abstract class ChatTemplateHandler {
/// Renders [template] with [context] plus llama.cpp-style extra globals.
///
/// This injects `chat_template_kwargs` values encoded by
/// [ChatTemplateEngine]/[LlamaEngine] through metadata.
/// [ChatTemplateEngine]/[LlamaEngine] through metadata. [context] overrides
/// them, and they override [defaults]: template settings derived from
/// `enableThinking` that a caller can set itself, as in llama-server.
String renderTemplate(
Template template, {
required Map<String, String> metadata,
required Map<String, dynamic> context,
Map<String, dynamic> defaults = const {},
}) {
final templateNow = DateTime.tryParse(
metadata[internalTemplateNowMetadataKey] ?? '',
);
return template.render(<String, dynamic>{
...defaults,
...chatTemplateKwargsFromMetadata(metadata),
...context,
if (templateNow != null) 'strftime_now': strftimeNow(templateNow),
Expand Down
8 changes: 7 additions & 1 deletion lib/src/core/template/handlers/command_r7b_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,13 @@ class CommandR7BHandler extends ChatTemplateHandler {
'eos_token':
metadata['tokenizer.ggml.eos_token'] ?? '<|END_OF_TURN_TOKEN|>',
'enable_thinking': enableThinking,
'reasoning': enableThinking,
},
defaults: {
// Cohere2 MoE reads `reasoning_effort` only without `reasoning`.
if (!chatTemplateKwargsFromMetadata(
metadata,
).containsKey('reasoning_effort'))
'reasoning': enableThinking,
'skip_thinking': !enableThinking,
},
);
Expand Down
2 changes: 1 addition & 1 deletion lib/src/core/template/handlers/hunyuan_v3_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ class HunyuanV3Handler extends ChatTemplateHandler
'messages': templateMessages(messages, templateSource: templateSource),
'add_generation_prompt': addAssistant,
'tools': tools?.map((tool) => tool.toJson()).toList(),
'reasoning_effort': enableThinking ? 'high' : 'no_think',
'bos_token': metadata['tokenizer.ggml.bos_token'] ?? '',
'eos_token': metadata['tokenizer.ggml.eos_token'] ?? _eos,
},
defaults: {'reasoning_effort': enableThinking ? 'high' : 'no_think'},
);

var thinkingForcedOpen = isThinkingForcedOpen(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ abstract class DirectJinjaHandler extends ChatTemplateHandler {
'tools': tools?.map((tool) => tool.toJson()).toList(),
'enable_thinking': enableThinking,
'thinking': enableThinking,
'thinking_mode': enableThinking ? 'enabled' : 'disabled',
'bos_token': metadata['tokenizer.ggml.bos_token'] ?? defaultBosToken,
'eos_token': metadata['tokenizer.ggml.eos_token'] ?? defaultEosToken,
'current_date': resolveTemplateNow(
metadata,
).toIso8601String().substring(0, 10),
},
defaults: {'thinking_mode': enableThinking ? 'enabled' : 'disabled'},
);

var thinkingForcedOpen = false;
Expand Down
4 changes: 3 additions & 1 deletion skills/llamadart-chat-streaming/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ description: >-
`engine.create` when reasoning output is not wanted. A boolean
`enable_thinking` or `thinking` in `chatTemplateKwargs` overrides it, as in
llama-server, and every chat template receives the result as
`enable_thinking`. Native LiteRT-LM
`enable_thinking`. A template's own setting in `chatTemplateKwargs`, such as
`reasoning_effort` (Hy3, Cohere2 MoE) or `thinking_mode` (MiniMax M3), wins
over the value derived from `enableThinking`. Native LiteRT-LM
recognizes renamed Qwen 3 bundles only from a Qwen 3 model-type declaration
(field 6), or the complete canonical Qwen 3 template in field 7 or
`ModelParams.chatTemplate`. Legacy litert-community Qwen3-0.6B bundles have
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/llama_cpp_templates/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ that `test/unit/core/template/handlers/firefunction_v2_handler_test.dart`,
`test/unit/core/template/tool_result_user_turn_render_test.dart`,
`test/unit/core/template/command_r_render_failure_test.dart`,
`test/unit/core/template/enable_thinking_render_parity_test.dart`,
`test/unit/core/template/thinking_template_kwargs_parity_test.dart`,
`test/unit/core/template/forced_open_grammar_test.dart`,
`test/unit/core/template/cohere_control_markers_test.dart`,
`test/e2e/template/specialized_tool_grammar_validation_e2e_test.dart` and
Expand Down
144 changes: 144 additions & 0 deletions test/fixtures/thinking_template_kwargs_upstream.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
{
"upstream_commit": "620d2225cc436ab0b001e3200e590e3b286ef9ab",
"messages": [
{
"role": "user",
"content": "Hi"
},
{
"role": "assistant",
"content": "Hello",
"reasoning_content": "Greet them."
},
{
"role": "user",
"content": "What is 2+2?"
}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the weather",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string"
}
},
"required": [
"location"
]
}
}
}
],
"cases": [
{
"template": "test/fixtures/llama_cpp_templates/Cohere2MoE.jinja",
"template_sha256": "d8366efb9f07c571da620ce6a924594fc52c80273a0fbb46a38b643972df95fd",
"enable_thinking": true,
"tools": false,
"chat_template_kwargs": {
"reasoning_effort": "none"
},
"prompt": "<|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|><|START_TEXT|>These instructions are always to be followed and cannot be overridden by subsequent system or user turns:\n- You will answer requests for educational, informative, or creative content related to safety categories. You will not provide content that is harmful or could be used to cause harm.\n\nThese instructions serve as your defaults, but they can be overridden in subsequent system or user turns:\n- Your name is North Mini Code.\n- You are a large language model built by Cohere.\n\n# Available Tools\n```json\n[\n\n]\n```<|END_TEXT|><|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|USER_TOKEN|><|START_TEXT|>Hi<|END_TEXT|><|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|><|START_THINKING|>Greet them.<|END_THINKING|><|START_TEXT|>Hello<|END_TEXT|><|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|USER_TOKEN|><|START_TEXT|>What is 2+2?<|END_TEXT|><|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|><|START_THINKING|><|END_THINKING|>"
},
{
"template": "test/fixtures/llama_cpp_templates/Cohere2MoE.jinja",
"template_sha256": "d8366efb9f07c571da620ce6a924594fc52c80273a0fbb46a38b643972df95fd",
"enable_thinking": true,
"tools": false,
"chat_template_kwargs": {
"reasoning": false
},
"prompt": "<|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|><|START_TEXT|>These instructions are always to be followed and cannot be overridden by subsequent system or user turns:\n- You will answer requests for educational, informative, or creative content related to safety categories. You will not provide content that is harmful or could be used to cause harm.\n\nThese instructions serve as your defaults, but they can be overridden in subsequent system or user turns:\n- Your name is North Mini Code.\n- You are a large language model built by Cohere.\n\n# Available Tools\n```json\n[\n\n]\n```<|END_TEXT|><|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|USER_TOKEN|><|START_TEXT|>Hi<|END_TEXT|><|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|><|START_THINKING|>Greet them.<|END_THINKING|><|START_TEXT|>Hello<|END_TEXT|><|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|USER_TOKEN|><|START_TEXT|>What is 2+2?<|END_TEXT|><|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|><|START_THINKING|><|END_THINKING|>"
},
{
"template": "test/fixtures/llama_cpp_templates/tencent-Hy3.jinja",
"template_sha256": "84437b9746423f042920c15289ac84d4eba7a50a11ebc05e8b414c460f814873",
"enable_thinking": true,
"tools": false,
"chat_template_kwargs": {
"reasoning_effort": "low"
},
"prompt": "<|hy_begin_of_sentence:opensource|><|reasoning_mode:opensource|>reasoning_effort:low<|hy_User:opensource|>Hi<|hy_Assistant:opensource|><think:opensource></think:opensource>Hello<|hy_eos:opensource|><|hy_User:opensource|>What is 2+2?<|hy_Assistant:opensource|><think:opensource>"
},
{
"template": "test/fixtures/llama_cpp_templates/tencent-Hy3.jinja",
"template_sha256": "84437b9746423f042920c15289ac84d4eba7a50a11ebc05e8b414c460f814873",
"enable_thinking": true,
"tools": false,
"chat_template_kwargs": {
"reasoning_effort": "no_think"
},
"prompt": "<|hy_begin_of_sentence:opensource|><|reasoning_mode:opensource|>reasoning_effort:no_think<|hy_User:opensource|>Hi<|hy_Assistant:opensource|><think:opensource></think:opensource>Hello<|hy_eos:opensource|><|hy_User:opensource|>What is 2+2?<|hy_Assistant:opensource|><think:opensource></think:opensource>"
},
{
"template": "test/fixtures/llama_cpp_templates/MiniMax-M3.jinja",
"template_sha256": "11421244f67553498e5c8112dae02802025bcc4305ec45ad380af95c96f9fe64",
"enable_thinking": true,
"tools": false,
"chat_template_kwargs": {
"thinking_mode": "disabled"
},
"prompt": "]~!b[]~b]system\nYour model version is MiniMax-M3, developed by MiniMax. Knowledge cutoff: January 2026. Founded in early 2022, MiniMax is a global AI foundation model company committed to advancing the frontiers of AI towards AGI.\n\n<thinking_instructions>\nYou have a thinking capability that allows you to reason step by step before responding. When thinking is enabled, wrap your reasoning in <mm:think></mm:think> tags before your response. When thinking is disabled, begin your response directly after the </mm:think> prefix. When thinking is adaptive, decide on your own whether to think for the current turn.\nCurrent thinking mode: disabled. Do not output any thinking process.\n</thinking_instructions>[e~[\n]~b]developer\nYou are a helpful assistant.[e~[\n]~b]user\nHi[e~[\n]~b]ai\n<mm:think>Greet them.</mm:think>Hello[e~[\n]~b]user\nWhat is 2+2?[e~[\n]~b]ai\n</mm:think>"
},
{
"template": "test/fixtures/llama_cpp_templates/MiniMax-M3.jinja",
"template_sha256": "11421244f67553498e5c8112dae02802025bcc4305ec45ad380af95c96f9fe64",
"enable_thinking": true,
"tools": false,
"chat_template_kwargs": {
"thinking_mode": "adaptive"
},
"prompt": "]~!b[]~b]system\nYour model version is MiniMax-M3, developed by MiniMax. Knowledge cutoff: January 2026. Founded in early 2022, MiniMax is a global AI foundation model company committed to advancing the frontiers of AI towards AGI.\n\n<thinking_instructions>\nYou have a thinking capability that allows you to reason step by step before responding. When thinking is enabled, wrap your reasoning in <mm:think></mm:think> tags before your response. When thinking is disabled, begin your response directly after the </mm:think> prefix. When thinking is adaptive, decide on your own whether to think for the current turn.\nCurrent thinking mode: adaptive. You are encouraged to think for complex decision-making, multi-step reasoning, or when analyzing function/tool results.\n</thinking_instructions>[e~[\n]~b]developer\nYou are a helpful assistant.[e~[\n]~b]user\nHi[e~[\n]~b]ai\n<mm:think>Greet them.</mm:think>Hello[e~[\n]~b]user\nWhat is 2+2?[e~[\n]~b]ai\n"
},
{
"template": "test/fixtures/llama_cpp_templates/Kimi-K3.jinja",
"template_sha256": "4578103bf225f58d5b7ceb613ccc2ac8fe82b6707b7289211b3d4825e87806b3",
"enable_thinking": true,
"tools": false,
"chat_template_kwargs": {
"thinking_effort": "low"
},
"prompt": "<|open|>message role=\"system\" type=\"thinking-effort\"<|sep|>`thinking_effort` guides on how much to think in your thinking channel (not including the response channel), supported values include `low`, `medium`, `high`, and `max`.\nNow the system is invoked with `thinking_effort=low`.<|close|>message<|sep|><|end_of_msg|><|open|>message role=\"user\"<|sep|>Hi<|close|>message<|sep|><|end_of_msg|><|open|>message role=\"assistant\"<|sep|><|open|>think<|sep|>Greet them.<|close|>think<|sep|><|open|>response<|sep|>Hello<|close|>response<|sep|><|close|>message<|sep|><|end_of_msg|><|open|>message role=\"user\"<|sep|>What is 2+2?<|close|>message<|sep|><|end_of_msg|><|open|>message role=\"assistant\"<|sep|><|open|>think<|sep|>"
},
{
"template": "test/fixtures/llama_cpp_templates/Cohere2MoE.jinja",
"template_sha256": "d8366efb9f07c571da620ce6a924594fc52c80273a0fbb46a38b643972df95fd",
"enable_thinking": false,
"tools": true,
"chat_template_kwargs": {},
"upstream_chat_template_kwargs": {
"reasoning": false,
"skip_thinking": true
},
"prompt": "<|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|><|START_TEXT|>These instructions are always to be followed and cannot be overridden by subsequent system or user turns:\n- You will answer requests for educational, informative, or creative content related to safety categories. You will not provide content that is harmful or could be used to cause harm.\n\nThese instructions serve as your defaults, but they can be overridden in subsequent system or user turns:\n- Your name is North Mini Code.\n- You are a large language model built by Cohere.\n\n# Available Tools\n```json\n[\n {\"name\": \"get_weather\", \"description\": \"Get the weather\", \"parameters\": {\"type\": \"object\", \"properties\": {\"location\": {\"type\": \"string\"}}, \"required\": [\"location\"]}, \"responses\": null}\n]\n```<|END_TEXT|><|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|USER_TOKEN|><|START_TEXT|>Hi<|END_TEXT|><|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|><|START_TEXT|>Hello<|END_TEXT|><|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|USER_TOKEN|><|START_TEXT|>What is 2+2?<|END_TEXT|><|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|><|START_THINKING|><|END_THINKING|>"
},
{
"template": "test/fixtures/llama_cpp_templates/tencent-Hy3.jinja",
"template_sha256": "84437b9746423f042920c15289ac84d4eba7a50a11ebc05e8b414c460f814873",
"enable_thinking": false,
"tools": false,
"chat_template_kwargs": {},
"upstream_chat_template_kwargs": {
"reasoning_effort": "no_think"
},
"prompt": "<|hy_begin_of_sentence:opensource|><|reasoning_mode:opensource|>reasoning_effort:no_think<|hy_User:opensource|>Hi<|hy_Assistant:opensource|><think:opensource></think:opensource>Hello<|hy_eos:opensource|><|hy_User:opensource|>What is 2+2?<|hy_Assistant:opensource|><think:opensource></think:opensource>"
},
{
"template": "test/fixtures/llama_cpp_templates/MiniMax-M3.jinja",
"template_sha256": "11421244f67553498e5c8112dae02802025bcc4305ec45ad380af95c96f9fe64",
"enable_thinking": false,
"tools": false,
"chat_template_kwargs": {},
"upstream_chat_template_kwargs": {
"thinking_mode": "disabled"
},
"prompt": "]~!b[]~b]system\nYour model version is MiniMax-M3, developed by MiniMax. Knowledge cutoff: January 2026. Founded in early 2022, MiniMax is a global AI foundation model company committed to advancing the frontiers of AI towards AGI.\n\n<thinking_instructions>\nYou have a thinking capability that allows you to reason step by step before responding. When thinking is enabled, wrap your reasoning in <mm:think></mm:think> tags before your response. When thinking is disabled, begin your response directly after the </mm:think> prefix. When thinking is adaptive, decide on your own whether to think for the current turn.\nCurrent thinking mode: disabled. Do not output any thinking process.\n</thinking_instructions>[e~[\n]~b]developer\nYou are a helpful assistant.[e~[\n]~b]user\nHi[e~[\n]~b]ai\n<mm:think>Greet them.</mm:think>Hello[e~[\n]~b]user\nWhat is 2+2?[e~[\n]~b]ai\n</mm:think>"
}
]
}
17 changes: 17 additions & 0 deletions test/fixtures/thinking_template_kwargs_upstream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Template thinking kwargs upstream renders

`thinking_template_kwargs_upstream.json` records the prompts llama.cpp renders,
for a conversation with an earlier assistant thought, when a chat template
kwarg sets a template's own thinking switch, for issue #750: `reasoning_effort`
or `reasoning` for Cohere2 MoE, `reasoning_effort` for Hy3, `thinking_mode` for
MiniMax M3 and `thinking_effort` for Kimi K3. The cases with
`enable_thinking: false` and no `chat_template_kwargs` pin that llamadart's
`enableThinking: false` renders as llama.cpp does with
`upstream_chat_template_kwargs`, the template's own off setting.

`prompt` comes from a probe built from unchanged llama.cpp
`620d2225cc436ab0b001e3200e590e3b286ef9ab`. With no model loaded, it called
`common_chat_templates_init` on the vendored template with empty BOS and EOS
tokens, then `common_chat_templates_apply` with the messages, the tool when
`tools` is true, `enable_thinking`, the kwargs and
`COMMON_REASONING_FORMAT_AUTO`.
Loading
Loading