From 59eb022e791c84b570bf16a3ed97980ce8c04c58 Mon Sep 17 00:00:00 2001 From: Jhin Lee Date: Sun, 8 Nov 2026 18:34:45 -0500 Subject: [PATCH 1/3] Record LiteRT-LM thinking as N/A for bundle templates that cannot think Refs #1827 --- doc/testing_matrix.md | 5 +- .../litert_lm_chat_features_smoke_test.dart | 35 ++++++++++ tool/litert_lm_chat_features_smoke.dart | 67 +++++++++++++++++-- tool/testing/test_matrix.dart | 3 +- .../docs/getting-started/model-families.md | 10 ++- 5 files changed, 111 insertions(+), 9 deletions(-) diff --git a/doc/testing_matrix.md b/doc/testing_matrix.md index 2947d60c0..cfb65da64 100644 --- a/doc/testing_matrix.md +++ b/doc/testing_matrix.md @@ -762,7 +762,10 @@ files stay in its suite-owned temporary directory. Record source revision, runtime artifact identity and device with the result. This row qualifies image wiring, history and lifecycle for this model and CPU; it does not qualify audio, thinking, GPU or perceptual quality generally. The -independent `litert-lm-chat-features-smoke` retains its thinking oracle. +independent `litert-lm-chat-features-smoke` retains its thinking oracle. It +records the thinking row as `not_applicable` (with the reason) when the loaded +bundle's chat template never reads `enable_thinking` and always closes the +think block, as both Qwen3.5 `.litertlm` bundles do. ### LiteRT-LM lifecycle regression diff --git a/test/unit/tooling/litert_lm_chat_features_smoke_test.dart b/test/unit/tooling/litert_lm_chat_features_smoke_test.dart index c81339ba1..fd64ceda3 100644 --- a/test/unit/tooling/litert_lm_chat_features_smoke_test.dart +++ b/test/unit/tooling/litert_lm_chat_features_smoke_test.dart @@ -196,6 +196,41 @@ void main() { }, ); + group('thinkingSupportForTemplate', () { + const closedAlways = + '{%- if m.role == "assistant" %}\n\n\n\n{% endif -%}' + '{%- if add_generation_prompt %}<|im_start|>assistant\n' + '\n\n\n\n{% endif -%}'; + const honoursSwitch = + '{%- if add_generation_prompt %}<|im_start|>assistant\n' + '{%- if enable_thinking is defined and enable_thinking is false %}' + '\n\n\n\n{%- else %}\n{%- endif %}' + '{% endif -%}'; + + test('reports a template that always closes the think block as N/A', () { + final support = smoke.thinkingSupportForTemplate(closedAlways); + expect(support.canProduceThinking, isFalse); + expect(support.reason, contains('enable_thinking')); + }); + + test('keeps the assertion for a template that reads enable_thinking', () { + expect( + smoke.thinkingSupportForTemplate(honoursSwitch).canProduceThinking, + isTrue, + ); + }); + + test('keeps the assertion without a readable or closed-think template', () { + expect(smoke.thinkingSupportForTemplate(null).canProduceThinking, isTrue); + expect( + smoke + .thinkingSupportForTemplate('<|im_start|>assistant\n\n') + .canProduceThinking, + isTrue, + ); + }); + }); + test('prints usage and exits 64 without a model path', () async { final smoke = await compileDartScript( 'tool/litert_lm_chat_features_smoke.dart', diff --git a/tool/litert_lm_chat_features_smoke.dart b/tool/litert_lm_chat_features_smoke.dart index 0e9abd54c..fa6aeeb99 100644 --- a/tool/litert_lm_chat_features_smoke.dart +++ b/tool/litert_lm_chat_features_smoke.dart @@ -3,6 +3,7 @@ import 'dart:io'; import 'package:llamadart/backend.dart'; import 'package:llamadart/llamadart.dart'; +import 'package:llamadart/src/backends/litert_lm/litert_lm_bundle_template.dart'; import 'audio_chat_smoke_support.dart'; import 'litert_lm_tokenizer_fixture.dart'; @@ -83,6 +84,9 @@ Future main(List args) async { ); final toolLoopCutoff = await _runToolLoopCutoffScenario(engine); + final thinkingSupport = thinkingSupportForTemplate( + readLiteRtLmBundleChatTemplate(modelPath), + ); final thinking = await _runScenario( engine: engine, messages: const [ @@ -229,7 +233,13 @@ Future main(List args) async { 'plain': plain.toJson(), 'cutoff': cutoff.toJson(), 'toolLoopCutoff': toolLoopCutoff, - 'thinking': thinking.toJson(), + 'thinking': { + if (!thinkingSupport.canProduceThinking) ...{ + 'status': 'not_applicable', + 'reason': thinkingSupport.reason, + }, + ...thinking.toJson(), + }, 'textHistory': textHistory.toJson(), 'sessionStructuredOutput': sessionStructuredOutput, 'toolCall': toolCall.toJson(), @@ -254,6 +264,7 @@ Future main(List args) async { plain: plain, cutoff: cutoff, thinking: thinking, + thinkingSupport: thinkingSupport, textHistory: textHistory, toolCall: toolCall, requiredUnsupportedExpected: requiredUnsupportedExpected, @@ -331,10 +342,44 @@ Future> runSessionStructuredOutputScenario( return result; } +/// Whether the chat template a loaded bundle renders can produce thinking. +class ThinkingSupport { + const ThinkingSupport.supported() : canProduceThinking = true, reason = null; + + const ThinkingSupport.unsupported(String this.reason) + : canProduceThinking = false; + + final bool canProduceThinking; + final String? reason; +} + +final RegExp _closedThinkBlock = RegExp(r'\s*'); + +/// Judges [template], the chat template embedded in a loaded bundle. +/// +/// A template that never reads `enable_thinking` and closes the think block in +/// its generation prompt cannot produce thinking whatever the request asks. An +/// unreadable or other template keeps the thinking assertion. +ThinkingSupport thinkingSupportForTemplate(String? template) { + final generationStart = template?.lastIndexOf('add_generation_prompt') ?? -1; + if (template == null || + generationStart < 0 || + template.contains('enable_thinking') || + !_closedThinkBlock.hasMatch(template.substring(generationStart))) { + return const ThinkingSupport.supported(); + } + return const ThinkingSupport.unsupported( + 'the bundle chat template never reads enable_thinking and its generation ' + 'prompt always closes an empty block, so it cannot ' + 'produce thinking', + ); +} + void _verifyResult({ required _ScenarioResult plain, required _ScenarioResult cutoff, required _ScenarioResult thinking, + required ThinkingSupport thinkingSupport, required _ScenarioResult textHistory, required _RequiredToolScenarioResult toolCall, required bool requiredUnsupportedExpected, @@ -365,11 +410,21 @@ void _verifyResult({ '${cutoff.finishReason}, not length.', ); } - if (thinking.thinking.trim().isEmpty) { - throw StateError('LiteRT-LM thinking scenario produced no thinking delta.'); - } - if (thinking.content.trim().isEmpty) { - throw StateError('LiteRT-LM thinking scenario produced no visible answer.'); + if (thinkingSupport.canProduceThinking) { + if (thinking.thinking.trim().isEmpty) { + throw StateError( + 'LiteRT-LM thinking scenario produced no thinking delta.', + ); + } + if (thinking.content.trim().isEmpty) { + throw StateError( + 'LiteRT-LM thinking scenario produced no visible answer.', + ); + } + } else { + stdout.writeln( + 'N/A litert_lm_chat_features thinking: ${thinkingSupport.reason}', + ); } if (!textHistory.content.contains('PINE') || textHistory.thinking.isNotEmpty || diff --git a/tool/testing/test_matrix.dart b/tool/testing/test_matrix.dart index 6cd9fce82..b97b42822 100644 --- a/tool/testing/test_matrix.dart +++ b/tool/testing/test_matrix.dart @@ -1285,7 +1285,8 @@ const List testMatrixRows = [ covers: 'real .litertlm chat, stop and maxTokens length finish reasons, an ' 'automatic tool loop cut off at maxTokens, thinking channel ' - 'reassembly, streaming tool calls, and optional byte-backed audio ' + 'reassembly (reported not_applicable when the bundle template cannot ' + 'think, as for Qwen3.5), streaming tool calls, and optional byte-backed audio ' 'question answering with an exact expected answer', command: 'dart run tool/testing/run_local_e2e.dart --scenario ' diff --git a/website/docs/getting-started/model-families.md b/website/docs/getting-started/model-families.md index 74e47e607..488a9dbb2 100644 --- a/website/docs/getting-started/model-families.md +++ b/website/docs/getting-started/model-families.md @@ -38,7 +38,7 @@ To see what a model will receive, render the prompt with | Family | Artifacts | Detected format | Evidence | Example repo | | --- | --- | --- | --- | --- | -| Qwen3.5 | GGUF, `.litertlm` | Qwen3-Coder XML (GGUF); Qwen3 template (`.litertlm`) | Validated: GGUF on CPU, Metal, CUDA, Vulkan and WebGPU; LiteRT-LM on CPU and GPU | `ggml-org/Qwen3.5-0.8B-GGUF`, `litert-community/Qwen3.5-0.8B` | +| Qwen3.5 | GGUF, `.litertlm` | Qwen3-Coder XML (GGUF); bundle's own template (`.litertlm`) | Validated: GGUF on CPU, Metal, CUDA, Vulkan and WebGPU; LiteRT-LM on CPU and GPU (no thinking, see below) | `ggml-org/Qwen3.5-0.8B-GGUF`, `litert-community/Qwen3.5-0.8B` | | Gemma 4 | GGUF, `.litertlm` | Gemma 4 | Validated: GGUF and LiteRT-LM | `unsloth/gemma-4-E2B-it-GGUF`, `litert-community/gemma-4-E2B-it-litert-lm` | | Qwen3 | GGUF, `.litertlm` | Hermes | Validated: LiteRT-LM. Template-tested: GGUF | `litert-community/Qwen3-0.6B` | | Gemma 3, 3n, 2 | GGUF, `.litertlm` | Gemma (generic JSON tool calls) | Validated: LiteRT-LM. Template-tested: GGUF | `litert-community/Gemma3-1B-IT` | @@ -76,6 +76,14 @@ thought channel or Mistral's `[THINK]`, the parser returns reasoning separately from the reply. See [Text generation and streaming](../guides/generation-and-streaming). +Qwen3.5 `.litertlm` bundles render their own embedded chat template +([#1134](https://github.com/leehack/llamadart/issues/1134)), so +`ModelParams.chatTemplate` is ignored for these files. The current +`litert-community/Qwen3.5-0.8B` bundles always close an empty `` block +in the generation prompt and never read `enable_thinking`, so +`enableThinking: true` produces no reasoning there. Use the GGUF for Qwen3.5 +thinking; Qwen3 and Gemma 4 `.litertlm` bundles support it. + ## Vision and audio GGUF vision and audio need a matching projector (`mmproj`) file. Gemma 4 E2B From e159c348158077819877fb45d90e262d378920ef Mon Sep 17 00:00:00 2001 From: Jhin Lee Date: Sun, 8 Nov 2026 19:30:49 -0500 Subject: [PATCH 2/3] Tighten Qwen3.5 thinking N/A docs, helper comment and checks Refs #1827 --- doc/testing_matrix.md | 3 ++- .../litert_lm_chat_features_smoke_test.dart | 11 +++++++++++ tool/litert_lm_chat_features_smoke.dart | 18 +++++++++++++++--- website/docs/getting-started/model-families.md | 13 ++++++++----- 4 files changed, 36 insertions(+), 9 deletions(-) diff --git a/doc/testing_matrix.md b/doc/testing_matrix.md index cfb65da64..8ae551cd6 100644 --- a/doc/testing_matrix.md +++ b/doc/testing_matrix.md @@ -765,7 +765,8 @@ it does not qualify audio, thinking, GPU or perceptual quality generally. The independent `litert-lm-chat-features-smoke` retains its thinking oracle. It records the thinking row as `not_applicable` (with the reason) when the loaded bundle's chat template never reads `enable_thinking` and always closes the -think block, as both Qwen3.5 `.litertlm` bundles do. +think block, as the Qwen3.5 `_int8`, `-VL_int8` and `_int8_gpu_composite_v0190` +bundles do. ### LiteRT-LM lifecycle regression diff --git a/test/unit/tooling/litert_lm_chat_features_smoke_test.dart b/test/unit/tooling/litert_lm_chat_features_smoke_test.dart index fd64ceda3..f1dec3cc6 100644 --- a/test/unit/tooling/litert_lm_chat_features_smoke_test.dart +++ b/test/unit/tooling/litert_lm_chat_features_smoke_test.dart @@ -220,6 +220,17 @@ void main() { ); }); + test('keeps the assertion when only history closes the think block', () { + const openInPrompt = + '{%- if m.role == "assistant" %}\n\n\n\n{% endif -%}' + '{%- if add_generation_prompt %}<|im_start|>assistant\n\n' + '{% endif -%}'; + expect( + smoke.thinkingSupportForTemplate(openInPrompt).canProduceThinking, + isTrue, + ); + }); + test('keeps the assertion without a readable or closed-think template', () { expect(smoke.thinkingSupportForTemplate(null).canProduceThinking, isTrue); expect( diff --git a/tool/litert_lm_chat_features_smoke.dart b/tool/litert_lm_chat_features_smoke.dart index fa6aeeb99..5a2f548f7 100644 --- a/tool/litert_lm_chat_features_smoke.dart +++ b/tool/litert_lm_chat_features_smoke.dart @@ -357,9 +357,10 @@ final RegExp _closedThinkBlock = RegExp(r'\s*'); /// Judges [template], the chat template embedded in a loaded bundle. /// -/// A template that never reads `enable_thinking` and closes the think block in -/// its generation prompt cannot produce thinking whatever the request asks. An -/// unreadable or other template keeps the thinking assertion. +/// A heuristic for the known always-closed shape: a template that never +/// mentions `enable_thinking` and closes an empty think block in its generation +/// prompt is treated as unable to think. Any other template, or an unreadable +/// one, keeps the thinking assertion. ThinkingSupport thinkingSupportForTemplate(String? template) { final generationStart = template?.lastIndexOf('add_generation_prompt') ?? -1; if (template == null || @@ -422,6 +423,17 @@ void _verifyResult({ ); } } else { + if (thinking.thinking.trim().isNotEmpty) { + throw StateError( + 'LiteRT-LM thinking scenario produced a thinking delta although its ' + 'template cannot think.', + ); + } + if (thinking.content.trim().isEmpty) { + throw StateError( + 'LiteRT-LM thinking scenario produced no visible answer.', + ); + } stdout.writeln( 'N/A litert_lm_chat_features thinking: ${thinkingSupport.reason}', ); diff --git a/website/docs/getting-started/model-families.md b/website/docs/getting-started/model-families.md index 488a9dbb2..3e6d1d1ad 100644 --- a/website/docs/getting-started/model-families.md +++ b/website/docs/getting-started/model-families.md @@ -78,11 +78,14 @@ separately from the reply. See Qwen3.5 `.litertlm` bundles render their own embedded chat template ([#1134](https://github.com/leehack/llamadart/issues/1134)), so -`ModelParams.chatTemplate` is ignored for these files. The current -`litert-community/Qwen3.5-0.8B` bundles always close an empty `` block -in the generation prompt and never read `enable_thinking`, so -`enableThinking: true` produces no reasoning there. Use the GGUF for Qwen3.5 -thinking; Qwen3 and Gemma 4 `.litertlm` bundles support it. +`ModelParams.chatTemplate` is ignored for these files. The +`Qwen3.5-0.8B_int8`, `Qwen3.5-0.8B-VL_int8` and +`Qwen3.5-0.8B_int8_gpu_composite_v0190` bundles (LiteRT-LM 0.19 and earlier) +close an empty `` block in the generation prompt and never read +`enable_thinking`, so `enableThinking: true` produces no reasoning there. The +`Qwen3.5-0.8B_dynamic_wi8_afp32` bundle reads `enable_thinking` but needs +LiteRT-LM 0.20.0 or later, so it fails on the pinned 0.19.1 runtime. Use the +GGUF for Qwen3.5 thinking; Qwen3 and Gemma 4 `.litertlm` bundles support it. ## Vision and audio From 97aea4a936d96260431c574194f348797439cd33 Mon Sep 17 00:00:00 2001 From: Jhin Lee Date: Sun, 8 Nov 2026 19:53:35 -0500 Subject: [PATCH 3/3] Pin the LiteRT-LM chat smoke thinking N/A row text Refs #1827 --- test/unit/tooling/test_matrix_test.dart | 14 ++++++++++++++ tool/testing/test_matrix.dart | 5 +++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/test/unit/tooling/test_matrix_test.dart b/test/unit/tooling/test_matrix_test.dart index e332cc5f5..926fc83c5 100644 --- a/test/unit/tooling/test_matrix_test.dart +++ b/test/unit/tooling/test_matrix_test.dart @@ -560,6 +560,20 @@ void main() { }, ); + test('documents the LiteRT-LM chat smoke thinking N/A', () { + final row = testMatrixRows.singleWhere( + (row) => row.id == 'litert-lm-chat-features-smoke', + ); + + expect( + row.covers.replaceAll(RegExp(r'\s+'), ' '), + contains( + 'not_applicable with a reason when the bundle template always ' + 'closes the think block, as for the Qwen3.5 int8 bundles', + ), + ); + }); + test('includes targeted local model smoke rows', () { final ids = testMatrixRows.map((row) => row.id).toSet(); diff --git a/tool/testing/test_matrix.dart b/tool/testing/test_matrix.dart index b97b42822..f35eff45c 100644 --- a/tool/testing/test_matrix.dart +++ b/tool/testing/test_matrix.dart @@ -1285,8 +1285,9 @@ const List testMatrixRows = [ covers: 'real .litertlm chat, stop and maxTokens length finish reasons, an ' 'automatic tool loop cut off at maxTokens, thinking channel ' - 'reassembly (reported not_applicable when the bundle template cannot ' - 'think, as for Qwen3.5), streaming tool calls, and optional byte-backed audio ' + 'reassembly (reported not_applicable with a reason when the bundle ' + 'template always closes the think block, as for the Qwen3.5 int8 ' + 'bundles), streaming tool calls, and optional byte-backed audio ' 'question answering with an exact expected answer', command: 'dart run tool/testing/run_local_e2e.dart --scenario '