Skip to content

Commit 18cc5f1

Browse files
committed
fix: handle ChatMessage objects in log_messages by converting at call 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
1 parent e585bff commit 18cc5f1

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/smolagents/memory.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,23 @@ def replay(self, logger: AgentLogger, detailed: bool = False):
261261
elif isinstance(step, ActionStep):
262262
logger.log_rule(f"Step {step.step_number}", level=LogLevel.ERROR)
263263
if detailed and step.model_input_messages is not None:
264-
logger.log_messages(step.model_input_messages, level=LogLevel.ERROR)
264+
# Convert ChatMessage objects to dicts for logging
265+
messages_as_dicts = [
266+
msg.dict() if hasattr(msg, 'dict') else dict(msg)
267+
for msg in step.model_input_messages
268+
]
269+
logger.log_messages(messages_as_dicts, level=LogLevel.ERROR)
265270
if step.model_output is not None:
266271
logger.log_markdown(title="Agent output:", content=step.model_output, level=LogLevel.ERROR)
267272
elif isinstance(step, PlanningStep):
268273
logger.log_rule("Planning step", level=LogLevel.ERROR)
269274
if detailed and step.model_input_messages is not None:
270-
logger.log_messages(step.model_input_messages, level=LogLevel.ERROR)
275+
# Convert ChatMessage objects to dicts for logging
276+
messages_as_dicts = [
277+
msg.dict() if hasattr(msg, 'dict') else dict(msg)
278+
for msg in step.model_input_messages
279+
]
280+
logger.log_messages(messages_as_dicts, level=LogLevel.ERROR)
271281
logger.log_markdown(title="Agent output:", content=step.plan, level=LogLevel.ERROR)
272282

273283
def return_full_code(self) -> str:

0 commit comments

Comments
 (0)