Skip to content

Conversation

@mpfaffenberger
Copy link
Contributor

Closes #3142

Add test to prevent tool-return stubs from being incorrectly merged into
subsequent user requests during message history cleaning.

- Guard against regression introduced in commit b26a687
- Verify tool-return parts remain in separate message entries
- Ensure cleaning preserves distinct message boundaries between tool results and user prompts
- Validate part types remain correct after history cleaning
- Add logic to identify "stub" ModelRequests containing only ToolReturnPart or RetryPromptPart
- Prevent merging non-stub requests into stub-only requests to maintain proper message structure
- Allow merging stub-into-stub or non-stub-into-non-stub requests when instructions match
- Refactor merge conditions for better clarity and correctness of message history handling
- Add deduplication logic to filter duplicate parts within messages
- Use hash-based tracking to identify and skip duplicate content
- Ensure backward compatibility with existing conversations in "bad state"
- Initialize parts_set to track previously seen message parts
- Append only unique parts to the final message list
This reverts commit 4746ece.
- Remove unnecessary hash-based deduplication logic that tracked parts across messages
- Simplify _clean_message_history to only merge consecutive messages of same type
- Remove unused import of pydantic_ai.messages module
- Fix import ordering in test file to follow conventions

The hash-based filtering was overly complex and potentially masking underlying issues with message construction. Message merging alone is sufficient for cleaning the history.
Add pyright ignore comment to _clean_message_history import to suppress
type checking warnings for this internal API usage in the test suite.
@DouweM DouweM self-assigned this Oct 13, 2025
@DouweM
Copy link
Collaborator

DouweM commented Oct 13, 2025

Pending discussion in #3142.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: edge case where all message parts are duplicated in each subsequent message

2 participants