Repository navigation
Recognise tool calls a Chat upstream writes into its answer text - #313
Merged
Merged
Conversation
Self-hosted and relayed Chat-format models (DeepSeek, GLM, Qwen, Llama
and friends) sometimes do not answer with `tool_calls` and instead write
the call into `content` in the template they were trained on. The client
then sees a tagged piece of prose and executes nothing, and the tool-call
guard never sees the call either.
`chat::text_calls` is a pure parser for four text forms:
- `<tool_call>{"name": …, "arguments": {…}}</tool_call>` (`parameters`
accepted for the arguments, and a string holding JSON text)
- DeepSeek's `<|tool▁calls▁begin|>…` group, with or without the
`function` word and the code fence, closing markers optional at the end
- GLM's `<tool_call>NAME\n<arg_key>…</arg_key><arg_value>…</arg_value>`,
values typed when they parse as JSON numbers, booleans, objects, arrays
- `<function=NAME><parameter name="k">v</parameter>…</function>`, closing
tags optional at the end
Only names the request defined are converted; anything else, and any bare
JSON object in prose, stays text. The ids use the same `call_` scheme as
the other synthesised calls.
The conversion session now keeps the request's tool names. For a Chat
upstream, a whole answer is rewritten before encoding, and a streamed
answer goes through a stage between the parser and the writer that buffers
a text block from the first opening marker to its close (or the end of the
block) and then emits ordinary ToolCall blocks, so the tool-call guard
inspects them like any structured call. Text before the marker streams as
it arrives. A `stop` finish becomes `tool_use` when a call was recovered.
Once a structured call has been seen the stage leaves later text alone.
Same-format passthrough (Chat client on a Chat upstream) is unchanged, as
the gateway never rewrites bytes there.
Also boxes the Responses variant of the stream `Writer` enum in
`convert.rs`: the current stable clippy (1.98) flags it as
`large_enum_variant` (456 bytes against 232 for the next one), which
would fail `-D warnings` as soon as CI's toolchain catches up.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Chat-format upstreams (self-hosted or relayed DeepSeek, GLM, Qwen, Llama …) sometimes write a tool call into the answer text instead of
tool_calls. The gateway now recognises four text forms and turns each into an ordinary structured tool call before the answer is converted for the client, removing the matched text:<tool_call>{"name": "...", "arguments": {...}}</tool_call>(also"parameters", and arguments given as a JSON string)<|tool▁calls▁begin|><|tool▁call▁begin|>function<|tool▁sep|>NAME\n```json\n{...}\n```<|tool▁call▁end|>…<|tool▁calls▁end|>— closing markers optional at the end; the newerNAME<|tool▁sep|>{...}spelling without the fence is read too<tool_call>NAME\n<arg_key>k</arg_key>\n<arg_value>v</arg_value>…</tool_call>— values stay strings unless they parse as JSON numbers / booleans / objects / arrays<function=NAME><parameter name="k">v</parameter>…</function>— closing tags optional at the endRules
tool_calls.new_id("call_").stopfinish reason is reported astool_usewhen a call was recovered.Where
crates/tw-dialect/src/chat/text_calls.rs: puresplit()parser, whole-responserewrite(), streamingScannerand the eventStage, with unit tests for every form, truncated endings, a non-matching name, bare JSON, marker split across deltas and the stage's event sequence.crates/tw-dialect/src/convert.rs:Sessionkeeps the request's tool names;normalize()callsrewrite()for Chat upstreams;Normalizerruns theStagefirst for Chat upstreams.chat/response.rs,chat/stream.rsandir.rsare untouched.crates/tw-gateway/tests/conversion.rs: four end-to-end tests with a fake Chat upstream and an Anthropic client — whole answer, streamed answer (marker split across chunks), a dangerous text-written call cut by the tool guard, and a non-matching name left as text.Also
The
Responsesvariant of the streamWriterenum inconvert.rsis now boxed: clippy on the current stable (1.98) raiseslarge_enum_varianton it (456 bytes vs 232), which fails-D warningslocally and will on CI once its toolchain updates. Behaviour unchanged.Verified
cargo fmt --all -- --checkcargo clippy --workspace --all-targets -- -D warningsenv -u HTTP_PROXY -u HTTPS_PROXY -u http_proxy -u https_proxy cargo test -p tw-dialectenv -u HTTP_PROXY -u HTTPS_PROXY -u http_proxy -u https_proxy cargo test -p tw-gatewayNew messages
None. No protocol change.
🤖 Generated with Claude Code