Skip to content

Commit a898520

Browse files
Fix additional typing issues in agentic tutorials
- Add null checks for task_state access - Fix safe content extraction patterns - Add null checks for span.output assignments - Addresses reportOptionalMemberAccess errors in tutorials
1 parent 8f5d4fc commit a898520

File tree

3 files changed

+23
-5
lines changed
  • examples/tutorials/10_agentic/00_base

3 files changed

+23
-5
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ async def handle_event_send(params: SendEventParams):
119119

120120
# Safely extract content from the task message
121121
response_text = ""
122-
if hasattr(task_message.content, 'content'):
122+
if task_message.content and hasattr(task_message.content, 'content'):
123123
content_val = getattr(task_message.content, 'content', '')
124124
if isinstance(content_val, str):
125125
response_text = content_val

examples/tutorials/10_agentic/00_base/030_tracing/project/acp.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,19 @@ async def handle_event_send(params: SendEventParams):
6262
#########################################################
6363

6464
task_state = await adk.state.get_by_task_and_agent(task_id=params.task.id, agent_id=params.agent.id)
65+
if not task_state:
66+
raise ValueError("Task state not found - ensure task was properly initialized")
6567
state = StateModel.model_validate(task_state.state)
6668
state.turn_number += 1
6769

6870
# Add the new user message to the message history
69-
state.messages.append(UserMessage(content=params.event.content.content))
71+
# Safely extract content from the event
72+
content_text = ""
73+
if hasattr(params.event.content, 'content'):
74+
content_val = getattr(params.event.content, 'content', '')
75+
if isinstance(content_val, str):
76+
content_text = content_val
77+
state.messages.append(UserMessage(content=content_text))
7078

7179
#########################################################
7280
# 4. (👋) Create a tracing span.
@@ -122,7 +130,13 @@ async def handle_event_send(params: SendEventParams):
122130
parent_span_id=span.id if span else None,
123131
)
124132

125-
state.messages.append(AssistantMessage(content=task_message.content.content))
133+
# Safely extract content from the task message
134+
response_text = ""
135+
if task_message.content and hasattr(task_message.content, 'content'):
136+
content_val = getattr(task_message.content, 'content', '')
137+
if isinstance(content_val, str):
138+
response_text = content_val
139+
state.messages.append(AssistantMessage(content=response_text))
126140

127141
#########################################################
128142
# 8. Store the messages in the task state for the next turn
@@ -144,7 +158,8 @@ async def handle_event_send(params: SendEventParams):
144158

145159
# (👋) You can store an arbitrary pydantic model or dictionary in the span output. The idea of a span is that it easily allows you to compare the input and output of a span to see what the wrapped function did.
146160
# In this case, the state is comprehensive and expressive, so we just store the change in state that occured.
147-
span.output = state
161+
if span:
162+
span.output = state
148163

149164
@acp.on_task_cancel
150165
async def handle_task_cancel(params: CancelTaskParams):

examples/tutorials/10_agentic/00_base/040_other_sdks/project/acp.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ async def handle_event_send(params: SendEventParams):
8686

8787
# Retrieve the task state. Each event is handled as a new turn, so we need to get the state for the current turn.
8888
task_state = await adk.state.get_by_task_and_agent(task_id=params.task.id, agent_id=params.agent.id)
89+
if not task_state:
90+
raise ValueError("Task state not found - ensure task was properly initialized")
8991
state = StateModel.model_validate(task_state.state)
9092
state.turn_number += 1
9193

@@ -149,7 +151,8 @@ async def handle_event_send(params: SendEventParams):
149151
)
150152

151153
# Set the span output to the state for the next turn
152-
span.output = state
154+
if span:
155+
span.output = state
153156

154157
@acp.on_task_cancel
155158
async def handle_task_cancel(params: CancelTaskParams):

0 commit comments

Comments
 (0)