Skip to content

Commit 0b19233

Browse files
Fix typing: add null checks and safe access in agentic multiturn tutorial
1 parent 606fc5e commit 0b19233

File tree

1 file changed

+9
-2
lines changed
  • examples/tutorials/10_agentic/00_base/010_multiturn/project

1 file changed

+9
-2
lines changed

examples/tutorials/10_agentic/00_base/010_multiturn/project/acp.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,19 @@ async def handle_event_send(params: SendEventParams):
9797
#########################################################
9898

9999
task_state = await adk.state.get_by_task_and_agent(task_id=params.task.id, agent_id=params.agent.id)
100+
if not task_state:
101+
raise ValueError("Task state not found - ensure task was properly initialized")
100102
state = StateModel.model_validate(task_state.state)
101103

102104
#########################################################
103105
# 6. (👋) Add the new user message to the message history
104106
#########################################################
105107

106-
state.messages.append(UserMessage(content=params.event.content.content))
108+
# Safely extract content from the event
109+
content_text = ""
110+
if hasattr(params.event.content, 'content') and isinstance(params.event.content.content, str):
111+
content_text = params.event.content.content
112+
state.messages.append(UserMessage(content=content_text))
107113

108114
#########################################################
109115
# 7. (👋) Call an LLM to respond to the user's message
@@ -114,7 +120,8 @@ async def handle_event_send(params: SendEventParams):
114120
llm_config=LLMConfig(model="gpt-4o-mini", messages=state.messages),
115121
trace_id=params.task.id,
116122
)
117-
state.messages.append(AssistantMessage(content=chat_completion.choices[0].message.content))
123+
response_content = chat_completion.choices[0].message.content or ""
124+
state.messages.append(AssistantMessage(content=response_content))
118125

119126
#########################################################
120127
# 8. (👋) Send agent response to client

0 commit comments

Comments
 (0)