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

+12 -3
View File
@@ -2213,11 +2213,11 @@ int Engine::collect_step(const RadBatch* b, StepOut& so) {
* drive the KV rollback, the conv cursor and the emitted text) say how far the step
* really got. */
acc = accept_cut_at_eog(out, acc,
[&](int32_t t) { return vocab_ && vocab_->is_eog(t); });
[&](int32_t t) { return is_eog(t); });
so.accepted[i] = acc;
so.token[i] = out[acc];
so.eos[i] = vocab_ && vocab_->is_eog(so.token[i]) ? 1 : 0;
so.eos[i] = is_eog(so.token[i]) ? 1 : 0;
if (tree) {
if (so.path.empty()) so.path.assign(plan.e.size() * RAD_SCHED_MAX_SPEC, 0);
for (int32_t t = 0; t < acc; ++t) so.path[i * RAD_SCHED_MAX_SPEC + t] = path[t];
@@ -2254,7 +2254,7 @@ int Engine::collect_step(const RadBatch* b, StepOut& so) {
continue;
}
so.token[i] = so.rows[(size_t)so.first_row[i]];
so.eos[i] = vocab_ && vocab_->is_eog(so.token[i]) ? 1 : 0;
so.eos[i] = is_eog(so.token[i]) ? 1 : 0;
if (e.req && e.req->sp.n_probs >= 0)
RAD_TRY(row_logprobs(so, so.first_row[i], so.token[i]));
/* Advance the grammar on every rank: each builds the mask for its own vocabulary shard from
@@ -3392,6 +3392,15 @@ int Engine::run_server() {
const bool have_tpl = !gen_ && vocab_ && !vocab_->chat_template().empty() &&
chat_bridge.init(*vocab_, declared_format, plugins_.arch_plugin) >= 0 &&
chat_bridge.ready();
/* A token this format writes as one of its own call delimiters is a call boundary, not a
* turn's: a reply that closes a call with it must be able to write the next one, and text
* after it. A container that also declares it end-of-generation would cut every tool-calling
* turn there, so the id leaves the decoder's stop set. */
eog_demoted_ = chat_bridge.eog_call_markers();
for (int32_t id : eog_demoted_)
RAD_WARN("vocab: token %d \"%s\" is end-of-generation and one of this model's "
"tool-call delimiters; a turn no longer ends on it", id,
vocab_->text(id).c_str());
if (embedding_) {
/* No chat endpoint: the model's template, when it has one, renders a `messages` input. */
if (have_tpl) embed_info.tpl = &chat_bridge;
+9
View File
@@ -419,6 +419,15 @@ private:
/* The text frontend, loaded from the container. The sampler's grammars walk `vocab_view_`,
* which references `vocab_` and must not outlive it. */
std::shared_ptr<const Vocab> vocab_;
/* End-of-generation ids the chat format also writes as a call delimiter: a call boundary is
* not a turn's, so the decoder does not stop on them (run_server demotes them). */
std::vector<int32_t> eog_demoted_;
/* End-of-generation as the decoder sees it: the vocabulary's set, minus the demoted ids. */
bool is_eog(int32_t id) const {
if (!vocab_ || !vocab_->is_eog(id)) return false;
for (int32_t d : eog_demoted_) if (d == id) return false;
return true;
}
std::unique_ptr<VocabView> vocab_view_;
/* The container's second vocabulary (rad-convert --tokenizer2), which an image generator that
* declares `aux_tokens` reads its prompt through; null without one. */
+31 -6
View File
@@ -526,12 +526,7 @@ public:
* derive it from the template. A declaration the parser cannot run is refused at startup. */
int init(const Vocab& v, const RadChatFormat* declared = nullptr,
const std::string& plugin = std::string()) {
/* The spellings, resolved once. See ReplyOptions::terminators. */
terminators_.clear();
for (int32_t id : v.eog()) {
const std::string s = v.text(id);
if (!s.empty()) terminators_.push_back(s);
}
eog_call_markers_.clear();
const int st = t_.load_from_vocab(v);
if (st < 0) return st;
@@ -569,9 +564,38 @@ public:
RAD_INFO("chat: reply format %s", chat_format_describe(f).c_str());
if (t_.caps().tools && !tool_refusal_.empty())
RAD_WARN("chat: tool calls are refused for this model: %s", tool_refusal_.c_str());
/* A container can name a token end-of-generation that this format writes as one of its
* own call delimiters (a converted checkpoint that lists its tool-call close). A turn
* ending there loses a second call and anything after it, and a reply that quotes the tag
* is cut mid-sentence; the engine drops these ids from the decoder's set
* (Vocab::drop_eog). */
eog_call_markers_.clear();
if (f.has_tools())
for (const std::string& m : chat_format_markers(f))
for (int32_t id : v.eog())
if (v.text(id) == m &&
std::find(eog_call_markers_.begin(), eog_call_markers_.end(), id) ==
eog_call_markers_.end())
eog_call_markers_.push_back(id);
/* The spellings, resolved once. See ReplyOptions::terminators. A call delimiter the
* container also declares terminal is left out here too: the parser must read it as the
* call boundary it is, not as the end of the reply. */
terminators_.clear();
for (int32_t id : v.eog()) {
if (std::find(eog_call_markers_.begin(), eog_call_markers_.end(), id) !=
eog_call_markers_.end())
continue;
const std::string s = v.text(id);
if (!s.empty()) terminators_.push_back(s);
}
return RAD_OK;
}
/* End-of-generation ids whose spelling is one of the format's call delimiters: a call
* boundary the container also declares a turn boundary. Empty for a model without tools. */
const std::vector<int32_t>& eog_call_markers() const { return eog_call_markers_; }
bool ready() const { return t_.ready() && fmt_ != nullptr; }
bool supports_tools() const override { return t_.caps().tools; }
std::string tools_probe_error() const override { return t_.caps().probe_error; }
@@ -752,6 +776,7 @@ private:
ChatTemplate t_;
std::vector<std::string> terminators_;
std::vector<int32_t> eog_call_markers_;
std::shared_ptr<const ReplyFormat> fmt_;
bool declared_ = false;
std::string tool_refusal_;
+12 -8
View File
@@ -202,10 +202,13 @@ int chat_format_check(const ChatFormat& f, std::string* why) {
if (object && !f.section_open.empty())
return no("section-wrapped object arguments are not supported");
/* Without a suffix the name ends at what follows it: the object's `{`, or a tagged call's
* first argument or close. */
if (!f.name_suffix.empty()) {
if (trim_ws(f.name_suffix).empty())
return no("name_suffix must end the function name with something besides whitespace");
* first argument or close. A suffix of nothing but whitespace is that same ending, written
* out: the name stops at the whitespace (Ling 3.0's `<tool_call>NAME\n<arg_key>`), so the
* reader treats it as a suffixless name, while the grammar still writes the template's
* newline. The object family has no such terminator -- its `{` ends the name -- and keeps
* the refusal. */
const bool name_by_ws = !object && !f.name_suffix.empty() && trim_ws(f.name_suffix).empty();
if (!f.name_suffix.empty() && !name_by_ws) {
if (is_ws((unsigned char)f.name_suffix[0]))
return no("name_suffix " + show(f.name_suffix) + " may not begin with whitespace");
}
@@ -216,10 +219,11 @@ int chat_format_check(const ChatFormat& f, std::string* why) {
if (f.arg_name_suffix.empty() && f.arg_value_prefix.empty())
return no("nothing ends an argument name: arg_name_suffix and arg_value_prefix are "
"empty");
if (f.name_suffix.empty()) {
/* The name runs to the first arg_prefix or call_end. A wrapped format also reads
* name_prefix alone as a call, told from prose only by its closed name tag, and
* without a suffix there is none. */
if (f.name_suffix.empty() || name_by_ws) {
/* The name runs to the first arg_prefix or call_end, written either with no suffix
* or with the whitespace that ends it. A wrapped format also reads name_prefix
* alone as a call, told from prose only by its closed name tag, and without a
* suffix there is none. */
if (!f.call_open.empty())
return no("name_suffix is empty, which a tagged call allows only when name_prefix "
"opens it, and call_open is set");
+3 -1
View File
@@ -116,7 +116,9 @@ struct ChatFormat {
std::string call_open, call_close;
/* "<function=", ">\n". name_suffix may be empty: in the object family the name then ends at
* the object's `{`; in the tagged family, where name_prefix must open the call (no
* call_open), at the first arg_prefix or call_end, which stay part of the call's body. */
* call_open), at the first arg_prefix or call_end, which stay part of the call's body. A
* suffix of only whitespace is read as that second ending: the name stops at the whitespace
* (`<tool_call>NAME\n<arg_key>`, Ling 3.0), which the grammar still writes. */
std::string name_prefix, name_suffix;
std::string call_end; /* "</function>\n"; may be empty for an object */
+5 -3
View File
@@ -746,8 +746,8 @@ struct ReplyFormat::Impl {
/* Object family. `object`: a call's arguments are one object. `ns_object`: the object's own
* `{` ends the name (name_suffix empty), and ns_core is "{". */
bool object = false, ns_object = false;
/* Tagged family, name_suffix empty: a call's name ends where arg_prefix or end_core begins,
* and that tag is the first of the call's body. */
/* Tagged family, no core in name_suffix (empty, or whitespace alone): a call's name ends
* where arg_prefix or end_core begins, and that tag is the first of the call's body. */
bool name_open = false;
/* Where a key or value may begin, besides the object's punctuation: the dialect string's
* opener, and the call's close and end (written there by a reply that leaves its object
@@ -810,7 +810,9 @@ int ReplyFormat::compile(const ChatFormat& f, std::shared_ptr<const ReplyFormat>
m.name_end = f.arg_name_suffix + f.arg_value_prefix;
if (f.typed_tags()) m.name_end_json = f.arg_name_suffix + f.arg_json_value_prefix;
m.object = f.has_tools() && f.object_args();
m.name_open = f.has_tools() && !m.object && f.name_suffix.empty();
/* A suffix with no core of its own ends the name at whitespace, and the name then runs to
* the first argument tag or the call's end, as it does without a suffix (Ling 3.0). */
m.name_open = f.has_tools() && !m.object && m.ns_core.empty();
if (m.object && f.name_suffix.empty()) {
m.ns_object = true;
m.ns_core = "{";