fix(stream): preserve non-negative tool call indexes#247
Open
bokmann wants to merge 1 commit into
Open
Conversation
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.
Summary
contentBlockIndexvalues to zero-based tool-call ordinals instead of subtracting 1Problem
Streaming tool-call chunks were being emitted with
tool_calls[].index = -1for the first tool call. Strict OpenAI-compatible clients reject that shape and fail during streamed tool use.I reproduced this against a live deployment with a request like:
{ "model": "us.anthropic.claude-sonnet-4-6", "stream": true, "stream_options": {"include_usage": true}, "messages": [{"role": "user", "content": "Use a tool call. Return weather for Boston."}], "tools": [{ "type": "function", "function": { "name": "get_weather", "description": "Get weather", "parameters": { "type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"] } } }] }Before this patch, the stream contained chunks like:
{"delta":{"tool_calls":[{"index":-1,"id":"tooluse_...","type":"function","function":{"name":"get_weather","arguments":""}}]}}That matches the existing negative-index bug report in #203.
Fix
The converter was using
contentBlockIndex - 1as the OpenAItool_calls[].index. That is not safe, becausecontentBlockIndexis a content-block position, not a tool-call ordinal.This patch keeps a per-stream mapping from Bedrock
contentBlockIndexto the next zero-based tool-call ordinal and reuses that mapping for later deltas from the same tool call.Validation
Ran the focused regression test inside the project runtime image:
Fixes #203.