Skip to content

Conversation

ketan0
Copy link

@ketan0 ketan0 commented Aug 30, 2025

Fix agent.replay(detailed=True) crashing with TypeError

The AgentLogger.log_messages method expects list[dict] but receives ChatMessage objects when replay() is called with detailed=True, causing:

TypeError: 'ChatMessage' object is not iterable

This PR fixes the issue by converting ChatMessage objects to dicts at the call sites in memory.py before passing them to log_messages, maintaining a clean interface.

Changes:

  • Convert ChatMessage objects to dicts in ActionStep and PlanningStep replay methods
  • Keep log_messages interface unchanged (expects list[dict])

Now users can debug the full LLM input/output flow with agent.replay(detailed=True) without crashes.

@ketan0 ketan0 force-pushed the fix/log-messages-chatmessage-objects branch from ad1a68e to 18cc5f1 Compare August 30, 2025 21:43
logger.log_messages(step.model_input_messages, level=LogLevel.ERROR)
# Convert ChatMessage objects to dicts for logging
messages_as_dicts = [
msg.dict() if hasattr(msg, 'dict') else dict(msg)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why this if/else?

Copy link
Author

Choose a reason for hiding this comment

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

good point, not needed! just fixed to do .dict() directly

… sites

- Modified memory.py to convert ChatMessage objects to dicts before calling log_messages
- Kept AgentLogger.log_messages interface clean - only accepts list[dict] as documented
- Fixes TypeError when calling agent.replay(detailed=True)
- Conversion happens at the 2 call sites in ActionStep and PlanningStep replay
- Maintains type safety and clear separation of concerns

This cleaner approach:
- Makes the log_messages interface predictable and type-safe
- Explicitly converts ChatMessage objects where they're used
- Avoids mixing domain objects with logging utilities
- Follows 'be conservative in what you send' principle

Resolves issue where replay(detailed=True) would crash with:
TypeError: 'ChatMessage' object is not iterable
@ketan0 ketan0 force-pushed the fix/log-messages-chatmessage-objects branch from 18cc5f1 to bbb5fca Compare August 30, 2025 22:18
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.

2 participants