Repository navigation
fix(nah_chat): replace (not append) tool call id/type in stream deltas - #30
Merged
Merged
Conversation
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.
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.
Problem
Streamed chat-completion deltas carry the tool call
idandtypeas complete values, but some providers resend the full value in every chunk of the same tool call.ChatMessage::apply_model_response_chunkappended 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/typeas complete scalars rather than token streams — last non-empty value wins (replace):"id": ""cannot erase the real value;function.arguments/function.namekeep accumulating, since those genuinely are token streams.Tests
New regression tests in
nah_chat/src/tests.rs:id/typeacross chunks are not duplicated;id/typereplaces the previous value;id/typedo not clobber the real value;index.All of these fail against the previous append behaviour (verified).
Verification
cargo test --workspace→nah_chat: 36 passed (was 32)cargo fmt --all -- --checkcleancargo clippy— no new warningsNote
A provider that streams
id/typein fragments would now have the earlier pieces dropped (last fragment wins). That is the deliberate consequence of the replace model; per the chat-completions schemaid/typeare server-assigned complete values, so this is the correct semantics.