Skip to content

fix(nah_chat): replace (not append) tool call id/type in stream deltas - #30

Merged
linmx0130 merged 1 commit into
mainfrom
fix/nah_chat-tool-call-delta-id-type
Sep 15, 2026
Merged

linmx0130 merged 1 commit into
mainfrom
fix/nah_chat-tool-call-delta-id-type

Conversation

@linmx0130

Copy link
Copy Markdown
Owner

Problem

Streamed chat-completion deltas carry the tool call id and type as complete values, but some providers resend the full value in every chunk of the same tool call. ChatMessage::apply_model_response_chunk appended them, so the value grew unboundedly (e.g. "call_abc123call_abc123call_abc123"), which breaks matching a streamed tool call to its id.

Fix

Treat id / type as complete scalars rather than token streams — last non-empty value wins (replace):

  • a provider that resends the full value is now idempotent;
  • an empty string is treated as "absent", so a delta carrying "id": "" cannot erase the real value;
  • function.arguments / function.name keep accumulating, since those genuinely are token streams.

Tests

New regression tests in nah_chat/src/tests.rs:

  • repeated id/type across chunks are not duplicated;
  • a later id/type replaces the previous value;
  • empty-string id/type do not clobber the real value;
  • the decision is made per tool-call index.

All of these fail against the previous append behaviour (verified).

Verification

  • cargo test --workspace → nah_chat: 36 passed (was 32)
  • cargo fmt --all -- --check clean
  • cargo clippy — no new warnings

Note

A provider that streams id/type in fragments would now have the earlier pieces dropped (last fragment wins). That is the deliberate consequence of the replace model; per the chat-completions schema id/type are server-assigned complete values, so this is the correct semantics.

Streamed chat completion deltas carry the tool call `id` and `type` as
complete, server-assigned values, not token streams. Some providers resend
the full value in every chunk of the same tool call; appending made the
value grow unboundedly (e.g. "call_abccall_abc"), which breaks matching the
tool call to its id.

Merge `id` / `type` with last-non-empty-wins semantics instead: a later
non-empty value replaces the accumulated one, and an empty string is treated
as absent so a delta carrying `"id": ""` does not erase the real value.
`function.arguments` / `function.name` keep accumulating, as those are
genuine token streams.

Add regression tests for the resend, replace, empty-string and per-index
cases; they fail against the previous append behaviour.
@linmx0130
linmx0130 merged commit 9badc7f into main Sep 15, 2026
1 check passed
@linmx0130
linmx0130 deleted the fix/nah_chat-tool-call-delta-id-type branch September 15, 2026 05:15
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