chat: read a function name that ends at whitespace (Ling 3.0)

Ling-3.0-flash's template writes a call as the opener, the function name, a
newline, and only then the argument tags:

    <tool_call>read
    <arg_key>path</arg_key>
    <arg_value>/a</arg_value>
    </tool_call>

The differential analysis names the function between the opener and the first
argument tag, so its end is the bare newline and name_suffix comes out as
whitespace alone. chat_format_check refused that, so every request carrying
tools answered 501 with "this model's tool calls cannot be read back:
name_suffix must end the function name with something besides whitespace" --
the whole tool-calling surface, for every Ling-3.0 checkpoint.

A suffix with no core of its own is the suffixless ending written out: the
name stops at the whitespace. The reader now takes it that way (name_open keys
on the trimmed core, so the name runs to the first arg_prefix or call_end,
whitespace trimmed), while the grammar still writes the template's newline.
The object family has no such terminator -- its brace ends the name -- and
keeps the refusal, as does the wrapped format whose bare name_prefix would
otherwise read as a call.

Adds the real template as a fixture and a regression test that derives the
format, builds the grammar, and reads a two-call reply built from the derived
fields; a second test pins the object family's refusal.
This commit is contained in:
Victor Ionescu authored and StillDeadcode committed 2026-11-16 18:03:16 +01:00
1 parent bd3fa646bc
commit b988378e70
8 files changed
+275 -21

No files matched your search

+73
View File
@@ -34,6 +34,7 @@
#include <arch/rad_museglimmer_chat.h>
#include <arch/rad_deepseek41_chat.h>
#include <arch/rad_gemma4_chat.h>
#include "util/json.h"
@@ -2380,6 +2381,78 @@ TEST(glm53_derives_a_name_the_opener_opens) {
CHECK(why.find("name_suffix") != std::string::npos);
}
/* Ling 3.0's template ends the function name with a newline and only then writes the argument
* tags, so the analysis derives a name_suffix of whitespace alone. The reader treats that as the
* suffixless ending -- the name stops at the first argument tag, whitespace trimmed -- while the
* grammar keeps the newline the template writes. Before it was allowed, every request carrying
* tools was refused: "name_suffix must end the function name with something besides
* whitespace". */
TEST(ling3_reads_a_name_that_ends_at_whitespace) {
ChatFormat f;
std::string why, refused;
CHECK_OK(chat_format_from_template(fixture("ling3"), "", "<|role_end|>", &f, &refused, &why));
CHECK(refused.empty());
/* The spellings of this shape are pinned by glm53_derives_a_name_the_opener_opens; Ling's
* difference is the newline between the name and the first argument tag. */
CHECK(f.call_open.empty() && f.call_close.empty());
CHECK(!f.name_prefix.empty() && f.name_prefix[0] == '<');
CHECK(!f.name_suffix.empty() &&
f.name_suffix.find_first_not_of(" \t\r\n") == std::string::npos);
CHECK(!f.call_end.empty());
CHECK(!f.arg_prefix.empty() && !f.arg_name_suffix.empty() && !f.arg_value_prefix.empty() &&
!f.arg_value_suffix.empty());
CHECK_OK(chat_format_check(f, &why));
ChatGrammarRequest gq;
gq.tools = &agent_tools();
ChatGrammar g;
CHECK_OK(chat_format_grammar(f, gq, &g, &why));
CHECK(g.lazy);
CHECK(!g.trigger_words.empty());
/* A reply built the way the template writes it reads back as the calls it renders, newline
* after the name and no whitespace between one argument's close and the next argument's
* tag. */
std::string reply = "Look.";
auto arg = [&](const std::string& k, const std::string& v) {
return f.arg_prefix + k + f.arg_name_suffix + f.arg_value_prefix + v + f.arg_value_suffix;
};
reply += f.name_prefix + "read" + f.name_suffix + arg("path", "/a") + arg("limit", "3") +
f.call_end;
reply += f.name_prefix + "ping" + f.name_suffix + f.call_end;
ReplyOptions ro;
ro.tools = &agent_tools();
ro.terminators = { "<|role_end|>" };
const std::shared_ptr<const ReplyParser> p = parser_of(f, ro);
REQUIRE(p != nullptr);
bool strict = false;
const ReplyMessage m = reply_parse(*p, reply, &strict);
CHECK_EQ(m.content, std::string("Look."));
REQUIRE(m.calls.size() == 2);
CHECK_EQ(m.calls[0].name, std::string("read"));
CHECK_EQ(m.calls[0].arguments, std::string("{\"path\":\"/a\",\"limit\":3}"));
CHECK(m.calls[0].closed);
CHECK_EQ(m.calls[1].name, std::string("ping"));
CHECK(m.calls[1].closed);
CHECK(strict);
const std::string stream_error = stream_all(*p, reply);
if (!stream_error.empty()) ::radtest::fail(__FILE__, __LINE__, stream_error);
}
/* The relaxation is for the tagged family only: an object family's name ends at its `{`, so a
* whitespace suffix there still describes a format nothing reads. */
TEST(a_whitespace_name_suffix_is_refused_for_object_arguments) {
ChatFormat f;
std::string why;
CHECK_OK(chat_format_from_abi(rad::arch::gemma4_chat_format(), &f, &why));
CHECK(f.object_args());
CHECK_OK(chat_format_check(f, &why));
f.name_suffix = "\n";
CHECK(chat_format_check(f, &why) < 0);
CHECK(why.find("whitespace") != std::string::npos);
}
TEST(glm53_replies_read_back_as_the_messages_they_render) {
const Glm* g = glm();
REQUIRE(g != nullptr);
+130
View File
@@ -0,0 +1,130 @@
{#- Bailing V3 chat template -#}
{#- Supports: thinking option, tool calling -#}
{#- ==================== thinking option normalization ==================== -#}
{%- if enable_thinking is defined %}
{%- if enable_thinking %}
{%- set thinking_option = 'on' %}
{%- else %}
{%- set thinking_option = 'off' %}
{%- endif %}
{%- elif thinking_option is not defined %}
{%- set thinking_option = 'on' %}
{%- endif %}
{#- ==================== preserved thinking ==================== -#}
{% set preserved_thinking = true %}
{#- ==================== system message ==================== -#}
{{- '<role>SYSTEM</role>' }}
{%- if tools %}
{%- if messages[0].role == 'system' %}
{{- messages[0].content + '\n' }}
{%- endif %}
{{- "# Tools\n\nYou may call one or more functions to assist with the user query.\n\nYou are provided with function signatures within <tools></tools> XML tags:\n<tools>" }}
{%- for tool in tools %}
{{- "\n" }}
{{- tool | tojson }}
{%- endfor %}
{{- "\n</tools>\n\nIf none of the functions can be used, point it out. If the given question lacks the parameters required by the function, also point it out.\nIf you need to use a function, for each function call, output the function name and arguments within the following XML format:\n<tool_call>{function-name}\n<arg_key>{arg-key-1}</arg_key>\n<arg_value>{arg-value-1}</arg_value>\n<arg_key>{arg-key-2}</arg_key>\n<arg_value>{arg-value-2}</arg_value>\n...\n</tool_call>\n" }}
{%- if messages[0].role == 'system' and messages[0].content is string and ('detailed thinking on' in messages[0].content or 'detailed thinking off' in messages[0].content) %}
{{- '<|role_end|>' }}
{%- else %}
{{- 'detailed thinking ' + thinking_option + '<|role_end|>' }}
{%- endif %}
{%- else %}
{%- if messages[0].role == 'system' %}
{%- if 'detailed thinking on' in messages[0].content or 'detailed thinking off' in messages[0].content %}
{{- messages[0].content + '<|role_end|>' }}
{%- else %}
{{- messages[0].content + '\n' }}
{{- 'detailed thinking ' + thinking_option + '<|role_end|>' }}
{%- endif %}
{% else %}
{{- 'detailed thinking ' + thinking_option + '<|role_end|>' }}
{%- endif %}
{%- endif %}
{%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
{%- for message in messages[::-1] %}
{%- set index = (messages|length - 1) - loop.index0 %}
{%- if ns.multi_step_tool and message.role == "user" and message.content is string and not(message.content.startswith('<tool_response>') and message.content.endswith('</tool_response>')) %}
{%- set ns.multi_step_tool = false %}
{%- set ns.last_query_index = index %}
{%- endif %}
{%- endfor %}
{%- for message in messages %}
{%- if message.content is string %}
{%- set content = message.content %}
{%- else %}
{%- set content = '' %}
{%- endif %}
{%- if message.role == "user" %}
{{- '<role>HUMAN</role>' + message.content + '<|role_end|>' }}
{%- elif message.role == "system" and not loop.first %}
{{- '<role>SYSTEM</role>' + message.content + '<|role_end|>' }}
{%- elif message.role == "assistant" %}
{%- set reasoning_content = '' %}
{%- if message.reasoning_content is string and message.reasoning_content != '' %}
{%- set reasoning_content = message.reasoning_content %}
{%- else %}
{%- if '</think>' in content %}
{%- set reasoning_content = content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}
{%- set content = content.split('</think>')[-1].lstrip('\n') %}
{%- endif %}
{%- endif %}
{%- if preserved_thinking or loop.index0 > ns.last_query_index %}
{%- if reasoning_content != '' %}
{{- '<role>ASSISTANT</role>' + '\n<think>' + reasoning_content.strip('\n') + '</think>' + content.lstrip('\n') }}
{%- else %}
{{- '<role>ASSISTANT</role>\n<think></think>' + content }}
{%- endif %}
{%- else %}
{{- '<role>ASSISTANT</role>\n<think></think>' + content }}
{%- endif %}
{%- if message.tool_calls %}
{%- for tool_call in message.tool_calls %}
{%- if (loop.first and content) or (not loop.first) %}
{{- '\n' }}
{%- endif %}
{%- set tc = tool_call %}
{%- if tool_call.function %}
{%- set tc = tool_call.function %}
{%- endif %}
{{- '<tool_call>' + tc.name }}
{% set _args = tc.arguments %}
{%- for k, v in _args.items() %}
{{- '<arg_key>' + k + '</arg_key>' }}
{{- '\n<arg_value>' }}
{%- if v is string %}
{{- v }}
{%- else %}
{{- v | tojson(ensure_ascii=False) }}
{%- endif %}
{{- '</arg_value>' }}
{%- endfor %}
{{- '\n</tool_call>' }}
{%- endfor %}
{%- endif %}
{{- '<|role_end|>' }}
{%- elif message.role == "tool" %}
{%- if loop.first or (messages[loop.index0 - 1].role != "tool") %}
{{- '<role>OBSERVATION</role>' }}
{%- endif %}
{{- '\n<tool_response>\n' }}
{{- content }}
{{- '\n</tool_response>' }}
{%- if loop.last or (messages[loop.index0 + 1].role != "tool") %}
{{- '<|role_end|>' }}
{%- endif %}
{%- endif %}
{%- endfor %}
{#- ==================== generation prompt ==================== -#}
{%- if add_generation_prompt %}
{{- '<role>ASSISTANT</role>' }}
{%- if thinking_option == 'on' %}
{{- '\n<think>' }}
{%- elif thinking_option == 'off' %}
{{- '\n<think></think>' }}
{%- endif %}
{%- endif %}