Skip to content

Conversation

@zzstoatzz
Copy link
Collaborator

@zzstoatzz zzstoatzz commented Aug 20, 2025

Summary

Fixes #1207 - IndexError when processing pydantic events in streaming

Problem

When pydantic-ai streams incomplete tool calls (ToolCallPartDelta), the event.index refers to the position in the internal _parts list, but get_parts() filters out these incomplete parts. This causes an IndexError when trying to access get_parts()[index].

Solution

  1. Use the internal _parts list directly for snapshots, since that's what the event indices refer to
  2. Added defensive check for None snapshots when accessing tool_call_id attribute
  3. Updated type hint to reflect that _get_snapshot can return None (though this should never happen with valid events)

Test Plan

  • Added unit test that reproduces and verifies the fix
  • Existing tests pass
  • Verified the exact error is reproduced without the fix

🤖 Generated with Claude Code

When pydantic-ai streams incomplete tool calls (ToolCallPartDelta),
the event.index refers to the position in the internal _parts list,
but get_parts() filters out these incomplete parts. This causes an
IndexError when trying to access get_parts()[index].

The fix uses the internal _parts list directly for snapshots, since
that's what the event indices refer to.

Fixes #1207

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@github-actions github-actions bot added the tests label Aug 20, 2025
@zzstoatzz zzstoatzz marked this pull request as ready for review August 20, 2025 17:00
Copilot AI review requested due to automatic review settings August 20, 2025 17:00
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Fixes an IndexError that occurs when processing streaming tool calls in pydantic-ai events. The issue arises when ToolCallPartDelta objects are present but filtered out by get_parts(), causing a mismatch between event indices and accessible parts.

  • Modified _get_snapshot() to use the internal _parts list directly instead of the filtered get_parts() method
  • Added bounds checking to prevent accessing invalid indices
  • Added comprehensive unit test to verify the fix and prevent regression

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/marvin/engine/streaming.py Updated _get_snapshot() to access _parts directly with bounds checking
tests/basic/engine/test_streaming.py Added test case reproducing the IndexError scenario with incomplete tool calls

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@zzstoatzz zzstoatzz merged commit b8c12d7 into main Aug 20, 2025
4 checks passed
@zzstoatzz zzstoatzz deleted the fix-streaming-indexerror branch August 20, 2025 17:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

'list index out of range' error on processing pydantic events in marvin.engine.streaming

2 participants