Skip to content

Commit 5e74015

Browse files
Fix typing: add null checks and safe access in agentic streaming tutorial
1 parent 0b19233 commit 5e74015

File tree

1 file changed

+12
-2
lines changed
  • examples/tutorials/10_agentic/00_base/020_streaming/project

1 file changed

+12
-2
lines changed

examples/tutorials/10_agentic/00_base/020_streaming/project/acp.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,19 @@ async def handle_event_send(params: SendEventParams):
8282
#########################################################
8383

8484
task_state = await adk.state.get_by_task_and_agent(task_id=params.task.id, agent_id=params.agent.id)
85+
if not task_state:
86+
raise ValueError("Task state not found - ensure task was properly initialized")
8587
state = StateModel.model_validate(task_state.state)
8688

8789
#########################################################
8890
# 6. Add the new user message to the message history
8991
#########################################################
9092

91-
state.messages.append(UserMessage(content=params.event.content.content))
93+
# Safely extract content from the event
94+
content_text = ""
95+
if hasattr(params.event.content, 'content') and isinstance(params.event.content.content, str):
96+
content_text = params.event.content.content
97+
state.messages.append(UserMessage(content=content_text))
9298

9399
#########################################################
94100
# 7. (👋) Call an LLM to respond to the user's message
@@ -109,7 +115,11 @@ async def handle_event_send(params: SendEventParams):
109115
trace_id=params.task.id,
110116
)
111117

112-
state.messages.append(AssistantMessage(content=task_message.content.content))
118+
# Safely extract content from the task message
119+
response_text = ""
120+
if hasattr(task_message.content, 'content') and isinstance(task_message.content.content, str):
121+
response_text = task_message.content.content
122+
state.messages.append(AssistantMessage(content=response_text))
113123

114124
#########################################################
115125
# 8. Store the messages in the task state for the next turn

0 commit comments

Comments
 (0)