Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions examples/memory/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,6 @@ def conversation_loop():
raise

# Process all messages from the runner
assistant_content: list[BetaContentBlockParam] = []
for message in runner:
spinner.stop()

Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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()


Expand Down