Skip to content

fix: tool call arguments lost on OpenAI-compatible providers that stream id only in first SSE chunk#1522

Open
goingforstudying-ctrl wants to merge 1 commit into
sigoden:mainfrom
goingforstudying-ctrl:fix/streaming-tool-call-args
Open

fix: tool call arguments lost on OpenAI-compatible providers that stream id only in first SSE chunk#1522
goingforstudying-ctrl wants to merge 1 commit into
sigoden:mainfrom
goingforstudying-ctrl:fix/streaming-tool-call-args

Conversation

@goingforstudying-ctrl
Copy link
Copy Markdown

What

Fixes a bug where tool call arguments are lost (sent as {}) when using OpenAI-compatible providers that omit the id field on tool-call continuation SSE chunks (e.g. GLM-5 via OpenRouter).

Why

Per the OpenAI streaming spec, id is present only in the first delta chunk for a given tool call. Continuation chunks carry argument fragments with the same index but no id.

The current code computes the call-boundary key as:

let maybe_call_id = format!("{}/{}", id.unwrap_or_default(), index.unwrap_or_default());

When id is absent, unwrap_or_default() yields "", producing maybe_call_id = "/0". This does not match the stored call_id, so a spurious boundary is detected: function_arguments is flushed (empty at that point) and cleared. The subsequent argument fragments are appended to the now-empty buffer, but the tool is dispatched with the flushed empty string, normalised to {}.

How

Only advance the boundary when id is present in the chunk — i.e. only on the first chunk of a new call. Continuation chunks have no id and now pass through without touching the boundary logic.

Using {id}/{index} retains uniqueness for providers that reuse index: 0 across every call (the original intent of commit 7d33a2c).

Testing

  • Compiled successfully with cargo +nightly check --bin aichat
  • Verified the fix handles all three scenarios correctly:
    • Single-chunk call (Grok-4) — unchanged
    • Multi-chunk call, id absent in continuations (GLM-5) — now works
    • Parallel calls, provider reuses index: 0 — still works

Closes #1495

…eam id only in first SSE chunk

When using an OpenAI-compatible provider whose streaming responses omit
the id field on tool-call continuation chunks (e.g. GLM-5 via OpenRouter),
aichat dispatched the tool with arguments: '{}' instead of the actual
arguments.

Root cause: openai_chat_completions_streaming computed the call-boundary
key using id.unwrap_or_default(), which yields empty string when id is
absent in continuation chunks, producing maybe_call_id = '/0'. This does
not match the stored call_id and triggers a spurious flush.

Fix: Only advance the boundary when id is present in the chunk — i.e.
only on the first chunk of a new call. Continuation chunks have no id
and now pass through without touching the boundary logic.

Closes sigoden#1495
@goingforstudying-ctrl
Copy link
Copy Markdown
Author

Hi, just checking in on this PR. It has been 6 days since the last update. Please let me know if there is anything else needed from my side to move this forward. Thanks for your time.

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.

Tool call arguments lost (sent as {}) on OpenAI-compatible providers that stream id only in the first SSE chunk

1 participant