Skip to content

Recognise tool calls a Chat upstream writes into its answer text - #313

Merged
fylorn merged 1 commit into
mainfrom
feat/text-tool-calls
Oct 10, 2026
Merged

fylorn merged 1 commit into
mainfrom
feat/text-tool-calls

Conversation

@fylorn

@fylorn fylorn commented Oct 10, 2026

Copy link
Copy Markdown
Contributor

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:

  1. <tool_call>{"name": "...", "arguments": {...}}</tool_call> (also "parameters", and arguments given as a JSON string)
  2. DeepSeek: <|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 newer NAME<|tool▁sep|>{...} spelling without the fence is read too
  3. GLM: <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
  4. <function=NAME><parameter name="k">v</parameter>…</function> — closing tags optional at the end

Rules

  • Only for Chat-dialect upstreams, only when the request defined tools, and only when the reply has no structured tool_calls.
  • Only names that match a defined tool are converted; everything else stays text. Bare JSON objects in prose are never parsed.
  • Recovered calls get ids from the crate's usual new_id("call_").
  • A stop finish reason is reported as tool_use when a call was recovered.
  • Streaming: text before the marker streams normally; from an opening marker on, the text block is buffered until the matching close (or the end of the block) and then emitted as ToolCall block(s). The tool-call guard therefore inspects these calls exactly like structured ones (test included). Once a structured call has been seen in the stream, later text is left alone.
  • Same-format passthrough (Chat client → Chat upstream) is unchanged: the gateway does not rewrite bytes there.

Where

  • New crates/tw-dialect/src/chat/text_calls.rs: pure split() parser, whole-response rewrite(), streaming Scanner and the event Stage, 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: Session keeps the request's tool names; normalize() calls rewrite() for Chat upstreams; Normalizer runs the Stage first for Chat upstreams. chat/response.rs, chat/stream.rs and ir.rs are 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 Responses variant of the stream Writer enum in convert.rs is now boxed: clippy on the current stable (1.98) raises large_enum_variant on it (456 bytes vs 232), which fails -D warnings locally and will on CI once its toolchain updates. Behaviour unchanged.

Verified

  • cargo fmt --all -- --check
  • cargo clippy --workspace --all-targets -- -D warnings
  • env -u HTTP_PROXY -u HTTPS_PROXY -u http_proxy -u https_proxy cargo test -p tw-dialect
  • env -u HTTP_PROXY -u HTTPS_PROXY -u http_proxy -u https_proxy cargo test -p tw-gateway

New messages

None. No protocol change.

🤖 Generated with Claude Code

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>
@fylorn
fylorn merged commit d2e606a into main Oct 10, 2026
5 checks passed
@fylorn
fylorn deleted the feat/text-tool-calls branch October 10, 2026 10:43
@fylorn fylorn mentioned this pull request Oct 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant