diff --git a/examples/memory/basic.py b/examples/memory/basic.py index fd0a9334..b3b1b336 100644 --- a/examples/memory/basic.py +++ b/examples/memory/basic.py @@ -335,7 +335,6 @@ def conversation_loop(): raise # Process all messages from the runner - assistant_content: list[BetaContentBlockParam] = [] for message in runner: spinner.stop() @@ -350,7 +349,8 @@ def conversation_loop(): for edit in message.context_management.applied_edits: print(f"\n🧹 [Context Management: {edit.type} applied]") - # Process content blocks + # Process content blocks - create assistant_content for THIS iteration + assistant_content: list[BetaContentBlockParam] = [] for content in message.content: if content.type == "text": print(content.text, end="", flush=True) @@ -371,7 +371,11 @@ def conversation_loop(): } ) - # Generate tool response automatically + # Append assistant message FIRST (before tool result) + if assistant_content: + messages.append({"role": "assistant", "content": assistant_content}) + + # THEN generate and append tool response tool_response = runner.generate_tool_call_response() if tool_response and tool_response["content"]: # Add tool results to messages @@ -381,10 +385,6 @@ def conversation_loop(): if isinstance(result, dict) and result.get("type") == "tool_result": print(f"[Tool result processed]") - # Store assistant message - if assistant_content: - messages.append({"role": "assistant", "content": assistant_content}) - print()