-
Notifications
You must be signed in to change notification settings - Fork 349
Closed
Labels
Description
Describe the bug
I followed the tutorial where it talks about handling events this way:
final_response_text = "Agent did not produce a final response." # Default
# Key Concept: run_async executes the agent logic and yields Events.
# We iterate through events to find the final answer.
async for event in runner.run_async(user_id=USER_ID, session_id=SESSION_ID, new_message=content):
# You can uncomment the line below to see *all* events during execution
# print(f" [Event] Author: {event.author}, Type: {type(event).__name__}, Final: {event.is_final_response()}, Content: {event.content}")
# Key Concept: is_final_response() marks the concluding message for the turn.
if event.is_final_response():
if event.content and event.content.parts:
# Assuming text response in the first part
final_response_text = event.content.parts[0].text
elif event.actions and event.actions.escalate: # Handle potential errors/escalations
final_response_text = f"Agent escalated: {event.error_message or 'No specific message.'}"
# Add more checks here if needed (e.g., specific error codes)
break # Stop processing events once the final response is found
print(f"<<< Agent Response: {final_response_text}")
This makes you think that all you need to do is look for the final event and print its text.
However, in my testing, I found out that before the final event, there are other events with text and the text of these events are NOT included in the final event. If you only look for the final event, then you'll miss some of the text.
Instead I handle events like this:
response_text = ""
for event in events:
if event.is_final_response():
if event.content and event.content.parts:
response_text += event.content.parts[0].text
elif event.actions and event.actions.escalate:
response_text = f"Agent escalated: {event.error_message or 'No specific message.'}"
break # Stop processing events once the final response is found
else:
if event.content and event.content.parts and event.content.parts[0].text:
response_text += event.content.parts[0].text
print(f"<<< Agent: {response_text}")
What I'm looking for?
Verification that my understanding is correct and the code to read text should be as I described.
To Reproduce
Steps to reproduce the behavior:
- Go through the tutorial
- Setup an agent that returns a big chunk of text
- Run it multiple times
- Sometimes, you'll see that the expected text is halfway cut
Expected behavior
The tutorial should show how to handle events properly with large text