fix(streaming): emit input_json events for server_tool_use blocks#1740
Open
sean-kim05 wants to merge 1 commit into
Open
fix(streaming): emit input_json events for server_tool_use blocks#1740sean-kim05 wants to merge 1 commit into
sean-kim05 wants to merge 1 commit into
Conversation
`build_events` only fired an `InputJsonEvent` when the streamed block was a client `tool_use` (and, in the beta path, `mcp_tool_use`). But `accumulate_event` tracks input for every type in `TRACKS_TOOL_INPUT`, which also includes `server_tool_use`. So when a server-side tool (e.g. `web_search`) streams its input, the deltas are accumulated into the message snapshot but no `input_json` event is ever emitted — `on_input_json` handlers and the iterated event stream silently miss server-tool input entirely. Emit `input_json` for exactly the block types whose input we accumulate, by reusing `TRACKS_TOOL_INPUT` (`isinstance`) instead of hard-coding a subset of `.type` values. This keeps emission and accumulation in lockstep so the two can't drift apart again.
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.
What
When streaming a message that contains a server-side tool call (
server_tool_use, e.g.web_search), the SDK accumulates the tool's streamed input into the message snapshot but never emits aninput_jsonstream event for it.on_input_jsoncallbacks and code iterating the event stream therefore never see the input of server tools — only clienttool_use(and, in the beta path,mcp_tool_use) fireinput_json.Root cause
accumulate_eventdecides what to accumulate withTRACKS_TOOL_INPUT:But
build_eventsdecides what to emit with a narrower, hard-coded check:The beta path (
_beta_messages.py) tracks(BetaToolUseBlock, BetaServerToolUseBlock, BetaMCPToolUseBlock)but only emits fortool_use/mcp_tool_use— again droppingserver_tool_use. So the emitter and the accumulator disagree about which blocks carry input.Fix
Emit
input_jsonfor exactly the block types whose input is accumulated, by reusingTRACKS_TOOL_INPUTviaisinstanceinbuild_events(both non-beta and beta). Emission and accumulation now share one source of truth and can't drift apart.Tests
tests/lib/streaming/fixtures/server_tool_use_response.txt— a stream with aweb_searchserver_tool_useblock whose input streams in as{"query": "weather in Paris"}.test_server_tool_use_input_json(sync + async) in bothtest_messages.pyandtest_beta_messages.py, assertinginput_jsonevents fire and the final snapshot equals the accumulated input.input_json_events == []for theserver_tool_useblock in all four tests; with it, they fire.tests/lib/streaming/suite (44 tests) passes;./scripts/lint(ruff + pyright + mypy) clean.