Conversation
Stream delta and stop events address content blocks by their index in the
message. The accumulator looked them up in content_blocks, which skips block
types it does not format (server_tool_use, web_search_tool_result, ...). After
a web search, a client tool call's input_json_delta events pointed past the
end of the list (arguments captured as {}) or at a different tool block.
Keep a list that lines up with the stream index and use it for tool deltas
and finalization. content_blocks and the captured output are unchanged.
This branch has not been deployed
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.
💡 Motivation and Context
Anthropic stream events address content blocks by their
indexin the message._AnthropicStreamAccumulatorpassedcontent_blockstohandle_anthropic_tool_deltaandfinalize_anthropic_tool_input, which look the block up withcontent_blocks[event.index]. Butcontent_blocksonly holds the block types the wrapper formats (text, thinking, redacted_thinking, tool_use). Aserver_tool_useorweb_search_tool_resultblock still takes an index in the stream and is skipped in the list.So once a response uses a server tool (web search, web fetch, code execution) before a client tool call, the list and the stream index no longer line up:
input_json_deltaevents point past the end of the list, and$ai_output_choicesrecords the call witharguments: {};The fix keeps a second list that lines up with the stream index (an empty placeholder for block types that are not formatted) and uses it only for tool deltas and finalization.
content_blocksand the captured output are unchanged.💚 How did you test it?
test_streaming_tool_call_after_server_tool_blocksbuilds the stream from the realanthropic.typesevents:server_tool_use(0),web_search_tool_result(1), text (2),tool_usewith twoinput_json_deltachunks (3). It goes through the wrappedAnthropicclient.main(4a138e6) it fails: the tool call is captured with'arguments': {}.{'city': 'SF'}.pytest posthog4144 passed, 16 skipped.ruff format --check,ruff check, mypy throughmypy-baseline filterandpython -W error -c "import posthog"are clean.📝 Checklist
If releasing new changes
sampo addto generate a changeset file🤖 Agent context
Autonomy: Fully autonomous
mainbefore the fix.handle_anthropic_*helper signatures, so the non-streaming path and the formatted output stay as they are.Commits here are unsigned; this repo squash-merges, so GitHub signs the merged commit.