Add `ModelResponsePartsManager.handle_tool_call_delta_events` and forward every stream's tool call deltas through it
This pull request was posted by Claude Code using claude-opus-5-5 on behalf of David.
Why we make these changes
ModelResponsePartsManager.handle_tool_call_delta returns event | None, so every stream iterator wraps it in an if maybe_event is not None guard (plus a # pragma: no branch). handle_text_delta and handle_thinking_delta yield their events instead. This adds an iterable variant and moves all 25 in-repo call sites, across 8 stream modules, onto it, as proposed in open question 2 of #10983. handle_tool_call_delta keeps its signature and is not deprecated, since custom models call it.
New public surface
ModelResponsePartsManager.handle_tool_call_delta_events: same keyword-only arguments ashandle_tool_call_delta, returnsIterator[ModelResponseStreamEvent], and yields nothing where the original returnsNone.
User-visible behavior
None. For a custom model's _get_event_iterator:
-maybe_event = self._parts_manager.handle_tool_call_delta(vendor_part_id=i, tool_name=name, args=args)
-if maybe_event is not None:
- yield maybe_event
+for event in self._parts_manager.handle_tool_call_delta_events(vendor_part_id=i, tool_name=name, args=args):
+ yield event
Verification
test_handle_tool_call_delta_eventsintests/test_parts_manager.pycovers the variant, including theNonecase.- Reach: CI's 100% coverage gate fails if any of the 25 moved call sites goes unexecuted.
- Events: an event capture, a pytest plugin that records every streamed event and final response per test node, ran on base and head. Base (f62e8d627) and head e012a7e17 are equal on 20 nodes chosen so every call site runs in at least one: 108 streams, 0 differing. A probe that records the line calling
handle_tool_call_delta_eventsfound all 25 call sites called in the head run, and no other caller. The node list and method are below. The capture tool lands in its own follow-up PR. - Moved branches that aren't call sites, Bedrock's Nova code interpreter start and Google's web search and file search responses, pass
test_bedrock_model_code_execution_tool_stream,test_google_model_web_search_tool_streamandtest_google_model_file_search_tool_stream. - Every call site has such a node, so none is left to coverage and snapshots alone.
Cognitive complexity
At three call sites the old code took no loop: Bedrock's tool-use start, Google's media-processing ID update, and OpenAI Responses' finished function call. The variant costs a loop there, since an async generator can't yield from. To keep each iterator at or below main's score:
- Bedrock handles a non-server tool use first, so the loop takes an existing branch: 92 to 92.
GeminiStreamedResponse._handle_tool_responsetakes the tool response branch: 169 to 153, plus 11 in the method.OpenAIResponsesStreamedResponse._handle_tool_search_call_donetakes the finished tool search call: 296 to 290, plus 2 in the method.
What changes for existing users
Nothing.
Event capture: nodes, result, normalized fields
Base was captured first, then head, in one serial pytest process each:
tests/models/anthropic/test_output.py::test_mixed_tools_no_output
tests/models/test_anthropic.py::test_anthropic_mcp_servers_stream
tests/models/test_bedrock.py::test_bedrock_model_iter_stream
tests/models/test_model_function.py::test_stream_structure
tests/models/test_google.py::test_google_model_iter_stream
tests/models/google/test_agentic_video.py::test_media_processing_pairing_preserves_explicit_ids (2 nodes)
tests/models/test_groq.py::test_tool_use_failed_error_streaming
tests/models/test_streamed_tool_call_deltas.py::test_tool_call_deltas_accumulate_into_one_part (openai, groq, huggingface)
tests/models/test_openai.py::test_stream_tool_call_with_empty_text
tests/models/test_openai_responses.py::test_openai_responses_stream_parallel_function_call_args
tests/models/test_openai_responses.py::test_openai_responses_stream
tests/models/test_openai_responses.py::test_openai_responses_code_execution_return_image_stream
tests/models/test_openai_responses.py::test_openai_responses_model_mcp_server_tool_stream
tests/models/test_openai_responses.py::test_openai_responses_model_web_search_tool_stream
tests/models/test_openai_responses.py::test_openai_responses_model_file_search_tool_stream
tests/models/test_openai_responses_stream_parity.py::test_recorded_streams_match_the_complete_response
tests/models/test_xai.py::test_xai_stream_structured
nodes: base 20, head 20
streams: base 108, head 108
events: head 4303
nodes per stream module, head:
pydantic_ai.models.anthropic._streaming: 2
pydantic_ai.models.bedrock: 1
pydantic_ai.models.function: 1
pydantic_ai.models.google: 3
pydantic_ai.models.groq: 2
pydantic_ai.models.huggingface: 1
pydantic_ai.models.openai: 9
pydantic_ai.models.xai: 1
only in base: 0, only in head: 0, differing: 0
A node that calls each call site in the head run, from the probe:
| Module | Call site (what it handles) | Node |
|---|---|---|
anthropic/_streaming.py | BetaToolUseBlock start | test_mixed_tools_no_output |
BetaMCPToolUseBlock start, BetaMCPToolUseBlock stop | test_anthropic_mcp_servers_stream | |
BetaInputJSONDelta | both of the above | |
bedrock.py | toolUse start, toolUse delta | test_bedrock_model_iter_stream |
function.py | tool call delta | test_stream_structure |
google.py | function_call part | test_google_model_iter_stream |
| media-processing tool response | test_media_processing_pairing_preserves_explicit_ids | |
groq.py | tool call delta | test_tool_use_failed_error_streaming, [groq] |
huggingface.py | tool call delta | [huggingface] |
openai.py (Chat) | tool call delta | test_stream_tool_call_with_empty_text, [openai] |
openai.py (Responses) | finalize_function_call | test_openai_responses_stream_parallel_function_call_args |
ResponseFunctionCallArgumentsDeltaEvent | test_openai_responses_stream | |
| code interpreter item, code delta, code done | test_openai_responses_code_execution_return_image_stream | |
McpCall item, MCP arguments delta, MCP arguments done | test_openai_responses_model_mcp_server_tool_stream | |
ResponseFunctionWebSearch | test_openai_responses_model_web_search_tool_stream | |
ResponseFileSearchToolCall | test_openai_responses_model_file_search_tool_stream | |
ResponseToolSearchCall, client and server | test_recorded_streams_match_the_complete_response | |
xai.py | client tool call | test_xai_stream_structured |
[provider] is test_tool_call_deltas_accumulate_into_one_part[provider].
Two runs of the same code differed in three fields, so the capture normalizes them, along with the generated ids and timestamps it already normalized:
ModelResponse.history_digest: it hashes the serialized history, timestamps included.durationof a fallback's failed attempts.- Local test server ports, as in
provider_url.
test_bedrock_thread_cancellation.py::test_request_timeout_ends_a_stream_read_blocked_on_a_real_connection starts its stream before the timeout in some runs and not in others, so it is left out of the selection. It yields no events either way.
Method: the plugin wraps StreamedResponse.__aiter__, so each stream records the events its _get_event_iterator yields and the response get() builds once it is exhausted, one JSON line per stream. Both captures loaded the plugin from a copy outside the tree, so the base commit could run it too. A comparator then diffs the two captures node by node. On the head run, a second plugin wrapped handle_tool_call_delta_events and recorded the file and line that called it, per node.
PR decisions
2026-11-01 · variant name: handle_tool_call_delta_events · iter -
- Decision: Name the iterable variant
handle_tool_call_delta_events, same keyword-only signature,Iterator[ModelResponseStreamEvent]return - Why: Keeps the
handle_*_deltafamily prefix;_eventssuffix followsrun_stream->run_stream_eventsfor the event-yielding variant of a method - Source: https://github.com/pydantic/pydantic-ai/tree/parts-manager-iterable-tool-call-delta
- Session: ee8d4048-dbc1-4c0e-b850-ab4f183ba77f
2026-11-01 · Google media-processing assert dropped · iter -
- Decision:
GeminiStreamedResponse'sassert event is not Nonegoes with the move to the variant - Why: Unreachable: the uuid vendor id always maps to the
NativeToolCallParthandle_partregistered, sohandle_tool_call_deltareturns aPartDeltaEvent;_stop_tracking_vendor_idonly touches embedded-thinking text ids - Source: https://github.com/pydantic/pydantic-ai/tree/parts-manager-iterable-tool-call-delta
- Session: ee8d4048-dbc1-4c0e-b850-ab4f183ba77f
2026-11-01 · finalize_function_call becomes a generator · iter -
- Decision: OpenAI Responses' nested
finalize_function_callyields from the variant, then records the call as finalized - Why: Same call order as before; the state it updates is local to the iterator, so updating it after the yield is unobservable
- Source: https://github.com/pydantic/pydantic-ai/tree/parts-manager-iterable-tool-call-delta
- Session: ee8d4048-dbc1-4c0e-b850-ab4f183ba77f
2026-11-01 · event capture location · iter -
- Decision:
scripts/stream_event_capture.py: pytest plugin plus comparator, outside the ruff/pyright/coverage include lists - Why: Developer proof tooling not run in CI, like
scripts/list_over_budget_tests.py; ruff and pyright run clean on it explicitly - Source: https://github.com/pydantic/pydantic-ai/tree/parts-manager-iterable-tool-call-delta
- Session: ee8d4048-dbc1-4c0e-b850-ab4f183ba77f
2026-11-01 · implement directly, no plan-only PR · iter -
- Decision: initialize-worktree complexity call: simple, implement
- Why: Manager dispatch for D3 fixed scope and shape; the change is mechanical and behavior-neutral
- Source: https://github.com/pydantic/pydantic-ai/issues/10983
- Session: ee8d4048-dbc1-4c0e-b850-ab4f183ba77f
2026-11-01 · pre-push review 1 of 3 at defc90280f9258aee415e6f66500a80ef50bf36f · iter -
- Decision: reserved; dispatched (haiku, light tier, first push)
- Why: tree 19e9d96bebe8f5aff43114233e99b61557d940e1; scope d9dbeb5bc..defc90280 full branch
- Source: https://github.com/pydantic/pydantic-ai/tree/parts-manager-iterable-tool-call-delta
- Session: ee8d4048-dbc1-4c0e-b850-ab4f183ba77f
2026-11-01 · pre-push review 1 of 3 at defc90280f9258aee415e6f66500a80ef50bf36f · iter -
- Decision: remediated: capture docstring runs the plugin from an out-of-tree copy and states the continuation-composite limit; used capture dir refused. Kept: variant name (delegated, run_stream_events precedent); not recording the unchanged continuation composite (its segment streams are recorded)
- Why: tree 19e9d96bebe8f5aff43114233e99b61557d940e1; scope d9dbeb5bc..defc90280 full branch; remediation commit f23559f076942154ef567d673b7c6a7bcfe73b69
- Source: https://github.com/pydantic/pydantic-ai/tree/parts-manager-iterable-tool-call-delta
- Session: ee8d4048-dbc1-4c0e-b850-ab4f183ba77f
2026-11-01 · pre-push review 2 of 3 at f23559f076942154ef567d673b7c6a7bcfe73b69 · iter -
- Decision: reserved; dispatched (haiku, light tier)
- Why: tree 81db8b68330be1babedfe8a6f5e24aaaf5e6203b; scope defc90280..f23559f07 follow-up plus prior findings
- Source: https://github.com/pydantic/pydantic-ai/tree/parts-manager-iterable-tool-call-delta
- Session: ee8d4048-dbc1-4c0e-b850-ab4f183ba77f
2026-11-01 · pre-push review 2 of 3 at f23559f07 · iter -
- Decision: remediated: capture creates a missing capture directory (217b9a557); prior findings 3 and 5 verified fixed, 1 and 2 dispositions not re-raised
- Why: tree 81db8b68330be1babedfe8a6f5e24aaaf5e6203b; scope defc90280..f23559f07 follow-up plus prior findings
- Source: https://github.com/pydantic/pydantic-ai/tree/parts-manager-iterable-tool-call-delta
- Session: ee8d4048-dbc1-4c0e-b850-ab4f183ba77f
2026-11-02 · event capture normalizes run-varying fields · iter -
- Decision: The capture normalizes
history_digest, failed-attemptdurationand local server ports, next to the existing id and timestamp normalization - Why: Two head runs of the same code differed on 180 nodes in exactly these three fields; a Bedrock timeout test also starts its stream only sometimes, so the selection leaves it out
- Source: https://github.com/pydantic/pydantic-ai/tree/parts-manager-iterable-tool-call-delta
- Session: ee8d4048-dbc1-4c0e-b850-ab4f183ba77f
2026-11-02 · proof E: small serial capture, reach from CI coverage · iter -
- Decision: Capture base then head, serially, on 20 nodes (17 test functions) chosen so each of the 25 call sites yields an event in at least one; CI's 100% coverage gate proves reach, with no local coverage pass
- Why: David set limits after the 82-file captures with coverage overloaded the machine: about 40 nodes at most, one pytest process, no coverage, no subagent-launched runs
- Source: https://github.com/pydantic/pydantic-ai/tree/parts-manager-iterable-tool-call-delta
- Session: ee8d4048-dbc1-4c0e-b850-ab4f183ba77f
2026-11-02 · pre-push review 3 of 3 at c6ac55f16e2da9ebe703d5885d39d6a29437ad49 · iter -
- Decision: reserved; dispatched (haiku, light tier)
- Why: tree 37acc075523245932b4b7ce3aff69fad90c53c82; scope f23559f07..c6ac55f16 follow-up (217b9a557, merge 35d256eb6 of upstream f62e8d627, c143d17f7, c6ac55f16) plus prior findings
- Source: https://github.com/pydantic/pydantic-ai/tree/parts-manager-iterable-tool-call-delta
- Session: ee8d4048-dbc1-4c0e-b850-ab4f183ba77f
2026-11-02 · pre-push review 3 of 3 at c6ac55f16e2da9ebe703d5885d39d6a29437ad49 · iter -
- Decision: 3 findings. Contract-file change: invalid, it is #12107 (17aa9ee83) carried in by the merge and the branch diff touches only stream_event_capture.py under scripts/. Digest masking: skipped, the record keeps the events and response parts the digest hashes, and its request history is test input. Per-site proof: remediated in the PR body with a call site to node map. No code change; budget exhausted
- Why: tree 37acc075523245932b4b7ce3aff69fad90c53c82; scope f23559f07..c6ac55f16 follow-up plus prior findings; no change since
- Source: https://github.com/pydantic/pydantic-ai/tree/parts-manager-iterable-tool-call-delta
- Session: ee8d4048-dbc1-4c0e-b850-ab4f183ba77f
2026-11-03 · douwebot run 41554856783 finding 2: docstring pointer · iter -
- Decision: Applied in 5c05b22cd: handle_tool_call_delta's docstring points at handle_tool_call_delta_events. Exempt delta (comments or docstrings only), chained from reviewed SHA c6ac55f16; review budget exhausted (3 of 3)
- Why: Cheap, correct; custom-model authors learn the parts manager from these docstrings
- Source: https://github.com/pydantic/pydantic-ai/pull/12302#pullrequestreview-5745500067
- Session: ee8d4048-dbc1-4c0e-b850-ab4f183ba77f
2026-11-03 · douwebot run 41554856783 finding 1: drop capture script — escalated · iter -
- Decision: Not acted on: douwebot asks to drop scripts/stream_event_capture.py (outside pyright/tests, patches StreamedResponse private internals, copy-out-of-tree step) and keep method/results in the PR body, or land it in its own PR as an env-gated tests/ plugin with typing and a test. Conflicts with David's D3 dispatch to commit the script; asked David
- Why: A settled maintainer decision outranks a bot finding; douwebot APPROVE is the M merge bar, so the verdict stays active until David decides
- Source: https://github.com/pydantic/pydantic-ai/pull/12302#pullrequestreview-5745500067
- Session: ee8d4048-dbc1-4c0e-b850-ab4f183ba77f
2026-11-03 · cognitive complexity gate (#11117) fails 5c05b22cd — escalated · iter -
- Decision: Required gate (via Check) fails: Bedrock 92->97, Google 169->175, OpenAI Responses 296->301. Cause: three sites were flat before (Bedrock elif-walrus at toolUse start, Google media-processing assert, OpenAI output_item.done elif-walrus on finalize_function_call); the variant needs a for loop in an async generator. Verified with complexipy 8.3.0: Bedrock reorder (non-server branch first, nova as elif) is score-neutral; keeping Google media and OpenAI finalize on handle_tool_call_delta restores both base scores (23 of 25 moved). Asked David: that, offset restructures, or hold until R10
- Why: Moving all 25 conflicts with a required gate main added after the merge base; the brief puts complexity restructuring out of scope
- Source: https://github.com/pydantic/pydantic-ai/actions/runs/41561795116/job/125673929272
- Session: ee8d4048-dbc1-4c0e-b850-ab4f183ba77f
2026-11-03 · douwebot run 41554856783 finding 1: capture script split out · iter -
- Decision: David: remove scripts/stream_event_capture.py from #12302; the PR body keeps the capture method and results and says the tool lands in its own follow-up PR (env-gated tests/ plugin with typing and a test), dispatched separately, not built in this worktree. Copy of the script kept at local-notes/follow-up-stream-event-capture/stream_event_capture.py
- Why: douwebot: the script sits outside every gate and patches private StreamedResponse internals; reuse across the #10983 refactors deserves a deliberate addition
- Source: https://github.com/pydantic/pydantic-ai/pull/12302#pullrequestreview-5745500067
- Session: ee8d4048-dbc1-4c0e-b850-ab4f183ba77f
- Supersedes: douwebot run 41554856783 finding 1: drop capture script — escalated
2026-11-03 · push gate at 6a074431f: no fourth review · iter -
- Decision: Delta from reviewed SHA c6ac55f16: 5c05b22cd docstring only (exempt) and 6a074431f deleting scripts/stream_event_capture.py (not in the exempt table). Budget exhausted (3 of 3), so no fourth review; the push goes to CI and a fresh douwebot review
- Why: Pushing skill: after the third review, remediate, do not dispatch a fourth, continue with push, CI and hosted review
- Source: https://github.com/pydantic/pydantic-ai/pull/12302
- Session: ee8d4048-dbc1-4c0e-b850-ab4f183ba77f
2026-11-03 · complexity gate: move all 25 and offset · iter -
- Decision: David: keep all 25 sites moved; Bedrock gets the score-neutral reorder; Google's and OpenAI Responses' iterators each extract one block into a private method next to the code (single-use extraction for complexity, never a nested def) so each scores at or below main; scripts/check_complexity.py must pass; behavior unchanged; rerun the 20-node capture serially on the new head against base
- Why: The variant costs a loop at the three sites that were flat; the required complexity gate allows no growth on functions over the limit
- Source: https://github.com/pydantic/pydantic-ai/actions/runs/41561795116/job/125673929272
- Session: ee8d4048-dbc1-4c0e-b850-ab4f183ba77f
- Supersedes: cognitive complexity gate (#11117) fails 5c05b22cd — escalated
2026-11-03 · push gate at e012a7e17: no fourth review · iter -
- Decision: Delta from reviewed SHA c6ac55f16: docstring (5c05b22cd), script removal (6a074431f), and e012a7e17: Bedrock tool-use start reorder plus GeminiStreamedResponse._handle_tool_response and OpenAIResponsesStreamedResponse._handle_tool_search_call_done. Runtime change, not exempt; budget exhausted (3 of 3), so CI and a fresh douwebot review cover it. Local: complexity gate logic from main passes vs f62e8d627 (Bedrock 92->92, Google 169->153 + 11, OpenAI Responses 296->290 + 2); pyright clean; ruff clean; 25 variant calls, 0 old-style
- Why: Pushing skill: after the third review, continue with push, CI and hosted review
- Source: https://github.com/pydantic/pydantic-ai/pull/12302
- Session: ee8d4048-dbc1-4c0e-b850-ab4f183ba77f
2026-11-03 · capture rerun via test-runner; douwebot review left in place · iter -
- Decision: David: the test-runner subagent runs exactly two serial runs (the 20-node capture with the reach probe, then the 3 extra tests), no coverage, no -n; his earlier limit only barred subagents that fan out runs. The addressed douwebot CHANGES_REQUESTED on c6ac55f16 stays undismissed
- Why: review_gates.py::douwebot_approval on main only counts github-actions[bot] reviews whose commit_id is the PR head, and a fresh APPROVE replaces the old verdict in GitHub's review decision (David checked)
- Source: https://github.com/pydantic/pydantic-ai/pull/12302#pullrequestreview-5745500067
- Session: ee8d4048-dbc1-4c0e-b850-ab4f183ba77f
2026-11-03 · proof E rerun on e012a7e17 · iter -
- Decision: Equal to base f62e8d627 on the 20 nodes (108 streams, 0 differing); reach probe: all 25 call sites called, no other caller; the 3 moved-branch tests pass. Outputs in local-notes/capture/sel-head2, reach-head2.txt, sel-compare2.txt
- Why: Two serial test-runner runs, no coverage, per David's answer
- Source: https://github.com/pydantic/pydantic-ai/pull/12302
- Session: ee8d4048-dbc1-4c0e-b850-ab4f183ba77f
2026-11-03 · additional review surface: none · iter -
- Decision: David chose none (D) after douwebot approved e012a7e17; final metadata check next, keeping one category label: refactor
- Why: Merge bar met on e012a7e17: CI green, clean douwebot APPROVE (review 5746589753, run 41567195760)
- Source: https://github.com/pydantic/pydantic-ai/pull/12302#pullrequestreview-5746589753
- Session: ee8d4048-dbc1-4c0e-b850-ab4f183ba77f
2026-11-03 · final metadata check at e012a7e17 · iter -
- Decision: Light-tier check from policy base a56a6d6f1. Kept: remove chore (one category label, refactor); sync the PR decisions mirror; drop the template's entries marker from the body. Not kept: provider topic labels (precedent #7548, a cross-provider streaming refactor labeled streaming only); un-collapsing the event-capture details (the cited rules cover playgrounds and per-goal sections, and David asked for method and results in the body)
- Why: Pushing skill, before handing the PR back: verify each correction against the rule it cites
- Source: https://github.com/pydantic/pydantic-ai/pull/12302
- Session: ee8d4048-dbc1-4c0e-b850-ab4f183ba77f
Checklist
- No breaking changes in accordance with the version policy.
- Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it.
- PR title is fit for the release changelog.
Summary by cubic
Adds ModelResponsePartsManager.handle_tool_call_delta_events, an iterable variant of handle_tool_call_delta that yields stream events instead of returning event | None, and moves all 25 in-repo call sites across 8 stream modules onto it.
- Stream iterators no longer need
if maybe_event is not Noneguards; the variant yields nothing where the original returnsNone. handle_tool_call_deltakeeps its signature and is not deprecated, since custom models call it; its docstring now points at the variant.- Three model iterators were refactored to stay within main's cognitive complexity limit: Bedrock handles non-server tool starts first, and Google and OpenAI Responses move tool response handling into helper generators.
- Adds a unit test for the variant, including the
Nonecase.
User-visible behavior
None.