Skip to content

chat: add dedicated Ling 3.0 (Bailing V3) parser - #28682

Merged
aldehir merged 2 commits into
ggml-org:masterfrom
aetherbird:fix-autoparser-reasoning-tool-termination
Sep 19, 2026
Merged

aldehir merged 2 commits into
ggml-org:masterfrom
aetherbird:fix-autoparser-reasoning-tool-termination

Conversation

@aetherbird

@aetherbird aetherbird commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Overview

Ling 3.0 templates pre-open in the generation prompt, so the model never emits an opening tag and tool calls can appear before </think>. The generated parser classified that output entirely as reasoning_content and clients received content="" with no tool_calls.

This adds a dedicated Ling parser (modeled on the hand-written Qwen3-Coder and Kimi parsers) that terminates reasoning at </think> or a tool-call marker, prefers the close tag when both are present, defers the streaming split to the final parse so delivered deltas are never reclassified, and keeps trailing prose after the last tool call as content. Adds the Ling 3.0 Flash chat template (byte-identical to the GGUF-embedded template) and tests covering the unclosed-think tool call (full and streaming), closed-think paths, trailing prose, parallel calls, marker-like strings in argument values, and reasoning_format=none.

Additional information

This builds upon the initial work that added BailingMoE 3 support #26608. Ling 3.0 also emits stray byte-level tokenizer pieces that decode to invalid UTF-8, which hard-fails the parse layer with a 500. That is fixed separately in #28724. Please note that both changes are needed for reliable usage of Ling 3.0 Flash in llama.cpp.

Validated against Ling 3.0 Flash GGUF on llama-server with --jinja --reasoning-format deepseek. With this parser and #28724 applied, a 29-task agentic benchmark went from 5 reasoning-only tool-call stops and 1 parse 500 to 0 of both.

Requirements

  • I have read and agree with the contributing guidelines.
  • AI usage disclosure: Yes, I used AI to help write and check the code. I personally, thoroughly reviewed the CONTRIBUTING.md file.

@aetherbird
aetherbird requested review from a team and ggerganov as code owners September 10, 2026 04:35
@github-actions github-actions Bot added the testing Everything test related label Sep 10, 2026
@pwilkin

pwilkin commented Sep 10, 2026

Copy link
Copy Markdown
Member

Models doing tool calls inside reasoning is generally a model bug. We don't want to adjust the parser behavior to support model bugs because fundamentally it usually results in reduced quality anyway. The only exceptions are when the behavior is so common (and present in high quantization versions) that it can be justifiably explained as actual trained model behavior, but we certainly don't want this to become the standard.

@aetherbird

Copy link
Copy Markdown
Contributor Author

Models doing tool calls inside reasoning is generally a model bug. We don't want to adjust the parser behavior to support model bugs because fundamentally it usually results in reduced quality anyway. The only exceptions are when the behavior is so common (and present in high quantization versions) that it can be justifiably explained as actual trained model behavior, but we certainly don't want this to become the standard.

A tool call emitted before </think> is not model misbehavior in Ling 3.0 Flash: the template's generation prompt pre-opens <think> (the model never emits an opening tag), so tool calls in an unclosed think block are the template's defined output format. InclusionAI's reference stack parses the same shape (vLLM/SGLang, --tool-call-parser ling3).

Master's hand-written parsers already terminate reasoning at non-think boundaries: common/parsers/qwen3-coder.cpp and common/parsers/deepseek.cpp at tool-call markers, common/parsers/kimi-k3.cpp at a response boundary. The change puts the generated parsers on the same behavior.

The close tag in this change is preferred. A <tool_call> appearing inside reasoning text does not end the reasoning block when a real </think> follows (Reka-Edge template test). Streaming defers the split to the final parse, so delivered deltas are never reclassified.

The UTF-8 tolerance commit changes no parsing rules. Invalid bytes are skipped and rendered as U+FFFD. Everything around them parses by the unchanged rules. This can be split into its own PR if needed.

@aldehir

aldehir commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

the template's generation prompt pre-opens (the model never emits an opening tag), so tool calls in an unclosed think block are the template's defined output format.

So do many models, and they properly emit a closing think tag. Yes, there is prior work, but this is a weak argument.

The UTF-8 tolerance commit changes no parsing rules. Invalid bytes are skipped and rendered as U+FFFD. Everything around them parses by the unchanged rules. This can be split into its own PR if needed.

This needs to be removed entirely. It will invalidate the prompt cache on subsequent requests and I'm not convinced it won't in-context learn as a result. It requires a better solution, and definitely not one that touches all models.

I'm not opposed to a dedicated Ling parser to address the tool call issue. But I'll leave it to @pwilkin's discretion.

@pwilkin

pwilkin commented Sep 10, 2026

Copy link
Copy Markdown
Member

Yeah, I was about to say - I'm fine with doing a dedicated Ling parser, I'm even fine with doing a superclass for "tool-call-in-reasoning" type parser that encompasses them all. But I'm wary of assuming all models that do reasoning will also do tool calls in-reasoning.

@aetherbird aetherbird changed the title Fix autoparser reasoning tool termination chat: add dedicated Ling 3.0 (Bailing V3) parser Sep 10, 2026
@aetherbird
aetherbird force-pushed the fix-autoparser-reasoning-tool-termination branch from 31ba154 to 02578ad Compare September 10, 2026 22:34
@aetherbird

aetherbird commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor Author

Per review from @pwilkin and @aldehir, this PR has been reworked to add a dedicated Ling parser (modeled on the hand-written Qwen3-Coder/Kimi parsers) instead of changing the generated-parser rules. The parse-layer UTF-8 change has been removed. The UTF-8 issue is a separate server-side fix, and has been opened as #28724.

Please note that Ling 3.0 Flash and Ling 3.0 Tiny need both of these changes, especially for any sort of agentic use. This builds upon the initial work that added BailingMoE 3 support #26608.

@aldehir

aldehir commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

/bot review

@ggml-gh-bot

ggml-gh-bot Bot commented Sep 11, 2026 •

Copy link
Copy Markdown

❌ Code review failed.

Error: 429 "Too Many Requests"

Comment thread common/parsers/parsers.h Outdated
Comment on lines +66 to +68
common_chat_params common_chat_params_init_ling3(const common_chat_template & tmpl, const autoparser::generation_params & inputs);

common_chat_params common_chat_params_init_ling3(const common_chat_template & tmpl, const autoparser::generation_params & inputs);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This seems wrong.

Comment thread tests/test-chat-auto-parser.cpp Resolved
@pwilkin

pwilkin commented Sep 11, 2026

Copy link
Copy Markdown
Member

/bot review

@ggml-gh-bot

ggml-gh-bot Bot commented Sep 11, 2026 •

Copy link
Copy Markdown
Automated code review

Review of the reworked diff (dedicated Ling 3.0 parser)

The rework looks good overall: the UTF-8 tolerance change was removed as requested, and a dedicated parser modeled on the existing hand-written parsers (kimi-k3, qwen3-coder) is the approach the maintainers explicitly approved. The detection strings (<role>ASSISTANT</role> + <arg_key>) are unique to this family among the bundled tagged-arg templates. No security concerns: nothing here sizes buffers from untrusted input.

Correctness issues that should be fixed before merge:

(point 1) Thinking-off path misclassifies content as reasoning - common/parsers/ling3.cpp:53-56. When the template is rendered with thinking off (enable_thinking=false, or a thinking_option: 'off' chat_template_kwargs), the generation prompt ends with the pre-closed <think/><think_end>, so the model emits only content. But think_body is built as p.reasoning(...) whenever reasoning_format != NONE, independent of whether the think block is actually open, so the entire response is classified as reasoning_content and the client gets content="". Gate the reasoning variant on the generation prompt actually pre-opening the think block, e.g. build think_body as reasoning only when data.generation_prompt ends with THINK_START (robust against both the enable_thinking input and a direct thinking_option kwarg). Please add a test for the thinking-off path; the current tests only exercise thinking-on.

(point 2) Missing continuation handling - common/parsers/ling3.cpp has no inputs.has_continuation() block, while every other specialized parser does (all 14 in common/parsers/), and the generic path handles it too (common/chat-auto-parser-generator.cpp:54). When continue_final_message is set, common/chat_templates_apply removes the final partial assistant message from params.messages and sets add_generation_prompt=false, expecting the specialized parser to splice it into data.generation_prompt (see the pattern in common/parsers/kimi-k3.cpp:58-67). Without it, the partial assistant turn is silently dropped from the prompt for Ling/Bailing templates, which regresses the generic path these templates used before this PR. Follow the kimi-k3 pattern: generation prompt becomes "<role>ASSISTANT></role>" + THINK_START + msg.reasoning_content, plus THINK_END + msg.render_content() for content continuation.

Will slow the review:

(point 3) Duplicate declaration - common/parsers/parsers.h: common_chat_params_init_ling3 is declared twice. Identical redeclarations are legal C++ so it compiles, but it is clearly accidental - remove one copy.

(point 4) Required arguments forced into schema order - common/parsers/ling3.cpp:108-110. Required args must appear in schema order, then optionals. The sibling parsers deliberately avoid this: qwen3-coder uses p.permute "as Qwen does not always adhere to the order provided" (common/parsers/qwen3-coder.cpp:121-122), and kimi-k3 accepts any arg in any order any number of times. Note Ling's own template renders historical tool calls in argument-dict order, not schema order, so the model has no training signal for schema order; and since the lazy grammar triggers at <tool_call_start>, it will actively force schema order onto the model mid-generation. Consider p.permute for the required args (mind the COMMON_CHAT_MAX_PERMUTE limit) or the kimi-style any-order form.

(point 5) Whitespace strictness regression vs. the autoparser path - common/parsers/ling3.cpp:25,120. ARG_VAL = "\n<arg_value>" is a hard literal, and tool_open requires a literal "\n" after the function name. The generated-parser path this replaces had a dedicated Bailing V3 patch setting tolerate_intertag_whitespace = true (common/chat-diff-analyzer.cpp:198-201), i.e. real outputs of this family are known to have inconsistent whitespace between tags. The rendered historical calls in the template also have no "\n" after the name. Consider tolerating optional whitespace at these seams (e.g. p.space() before ARG_VAL, or the p.optspace helper), or justify the strictness with observed Ling 3.0 Flash output.

Nits:

(point 6) tests/test-chat-auto-parser.cpp, load_ling_templates(): silently returns an empty template when the file cannot be opened (tests then fail far away with confusing errors). Reuse the existing load_template(t, ...) helper, which asserts the source is non-empty, and wrap it via common_chat_templates_init.

(point 7) tests/test-chat-auto-parser.cpp: five stray blank lines after ling_parse().

(point 8) models/templates/inclusionai-ling-3.0-flash.jinja has no trailing newline; every other bundled template ends with one. If the GGUF-embedded template really lacks it, byte-identity is a valid reason to keep it - otherwise add the newline.

(point 9) common/parsers/ling3.cpp:79-105: the manual required/optional split and is_string check reimplement foreach_parameter (which yields is_required directly) and common_schema_info::resolves_to_string, both used by qwen3-coder. Prefer the shared helpers so edge cases (non-array required, enum-typed strings) behave the same across parsers.

Everything else in the parser matches the established patterns (preserved tokens, message delimiters, grammar trigger on the call-start word, whitespace-only reasoning discarded). The tests cover the important paths well, including the streaming monotonicity case.

This review was generated automatically by pi coding agent using zai-org/GLM-5.3. It may contain mistakes. Maintainers make the final call.

@aetherbird
aetherbird force-pushed the fix-autoparser-reasoning-tool-termination branch from 02578ad to 9d800ba Compare September 11, 2026 14:45
@aetherbird
aetherbird requested a review from pwilkin as a code owner September 11, 2026 14:45
@aetherbird

Copy link
Copy Markdown
Contributor Author

9d800ba8d addresses issues surfaced from @aldehir's review:

This seems wrong. (parsers.h)

Duplicate declaration removed

Move tests to tests/test-chat.cpp and follow the conventions used by other models.

Ling tests now use the peg_tester conventions in tests/test-chat.cpp. Added coverage for the thinking-off path, continuations, tool calls with whitespace variations, and required arguments.

@aetherbird
aetherbird force-pushed the fix-autoparser-reasoning-tool-termination branch from 9d800ba to 72ddff5 Compare September 11, 2026 23:51
@aetherbird

Copy link
Copy Markdown
Contributor Author

72ddff5c0 adds handling for Ling's end-of-turn token.

<|role_end|> is the registered EOS for this tokenizer. The model sometimes spells it out as text tokens instead of emitting the control token, which does not stop generation, so it leaks into content. This is the same failure class the autoparser handles for Laguna's </assistant> with a literal stop string.

This registers <|role_end|> as an additional stop and consumes it in the parser if it still reaches the text. Tests cover both paths.

@aetherbird
aetherbird requested a review from aldehir September 12, 2026 21:07
Comment thread common/parsers/ling3.cpp Outdated
Comment on lines +111 to +112
auto schema_info = common_schema_info();
schema_info.resolve_refs(parameters);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This has changed, needs to adopt the newer schema types. See the qwen3-coder source as an example.

Comment thread tests/test-chat-auto-parser.cpp Outdated
@@ -99,6 +99,7 @@ static void test_normalize_quotes_with_embedded_quotes(testing & t);
static void test_tagged_args_with_embedded_quotes(testing & t);
static void test_bailing_v3_tool_format(testing & t);


Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

??

Comment thread tests/test-chat-auto-parser.cpp Outdated
Comment on lines +2592 to +2601
// ============================================================================
// Ling 3.0 Flash tests
//
// The template pre-opens the think block in the generation prompt
// ('<role>ASSISTANT</role>\n<think>'), so the model never emits an opening
// <think>. Reasoning must terminate at </think> OR at a tool-call start,
// otherwise a tool call emitted before the think block closes is swallowed
// into reasoning_content and the client sees a reasoning-only turn.
// ============================================================================

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

???

Comment thread tests/test-chat.cpp Outdated
@@ -2092,9 +2092,171 @@ static void test_lfm2_parser(const std::string & template_path, bool detailed_de

}


// Ling 3.0 Flash / Bailing V3 dedicated parser
static void test_ling_3_0_flash(bool detailed_debug) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Conform to the existing format used by all other models.

@aetherbird
aetherbird force-pushed the fix-autoparser-reasoning-tool-termination branch from 72ddff5 to 965c048 Compare September 14, 2026 21:47
@aetherbird
aetherbird requested a review from aldehir September 14, 2026 22:05
@aldehir

aldehir commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

Thanks for addressing my comments. The tests still don't match the existing patterns in this file, though. We try to avoid adding style variations without a clear reason, so could you remove the extra tool definitions, reuse the existing ones, and nest the test case under the function like the other tests? I'll approve once that's done.

@aetherbird
aetherbird force-pushed the fix-autoparser-reasoning-tool-termination branch from 965c048 to 5fb83b9 Compare September 17, 2026 22:49
Ling 3.0 Flash templates pre-open the think block in the generation
prompt, so the model never emits an opening <think>, and a tool call can
arrive before any </think>. The generated autoparser terminated reasoning
only at the close tag, which classified such tool calls entirely as
reasoning_content: clients received content="" with no tool_calls and
agent loops died as reasoning-only turns.

Adds a specialized parser that terminates reasoning at the think close
tag or at a <tool_call> start, mirroring the hand-written Qwen3-Coder and
Kimi K3 parsers and the reference vLLM/SGLang Ling3 parser (which treats
<tool_call> as an implicit reasoning terminator). Detection is gated on
the <role>...</role> section markers, unique to this family among the
tagged-argument templates.

Adds the Ling 3.0 Flash chat template and tests covering the
unclosed-think tool call (full parse and streaming), healthy closed-think
paths, trailing prose, parallel calls, marker-like strings in argument
values, string-union and non-string argument types, and
reasoning_format=none.

Assisted-by: Kimi Code
@aetherbird
aetherbird force-pushed the fix-autoparser-reasoning-tool-termination branch from 5fb83b9 to 1fc5c42 Compare September 17, 2026 23:13
@aetherbird

Copy link
Copy Markdown
Contributor Author

Thanks for addressing my comments. The tests still don't match the existing patterns in this file, though. We try to avoid adding style variations without a clear reason, so could you remove the extra tool definitions, reuse the existing ones, and nest the test case under the function like the other tests? I'll approve once that's done.

Addressed in 5fb83b9.

@aldehir
aldehir merged commit 59657a6 into ggml-org:master Sep 19, 2026
23 of 26 checks passed
@linuxid10t

Copy link
Copy Markdown

Congrats on the merge, excited for Ling 3.0 Flash VL, but happy to see Ling 3.0 Tiny working right now.

@aetherbird
aetherbird deleted the fix-autoparser-reasoning-tool-termination branch September 26, 2026 21:13
LadislavSopko pushed a commit to 0ics-srls/llama.cpp that referenced this pull request Oct 5, 2026
* chat: add dedicated Ling 3.0 (Bailing V3) parser

Ling 3.0 Flash templates pre-open the think block in the generation
prompt, so the model never emits an opening <think>, and a tool call can
arrive before any </think>. The generated autoparser terminated reasoning
only at the close tag, which classified such tool calls entirely as
reasoning_content: clients received content="" with no tool_calls and
agent loops died as reasoning-only turns.

Adds a specialized parser that terminates reasoning at the think close
tag or at a <tool_call> start, mirroring the hand-written Qwen3-Coder and
Kimi K3 parsers and the reference vLLM/SGLang Ling3 parser (which treats
<tool_call> as an implicit reasoning terminator). Detection is gated on
the <role>...</role> section markers, unique to this family among the
tagged-argument templates.

Adds the Ling 3.0 Flash chat template and tests covering the
unclosed-think tool call (full parse and streaming), healthy closed-think
paths, trailing prose, parallel calls, marker-like strings in argument
values, string-union and non-string argument types, and
reasoning_format=none.

Assisted-by: Kimi Code

* tests : move Ling 3.0 test

---------

Co-authored-by: aetherbird <[email protected]>
Co-authored-by: Alde Rojas <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants