Skip to content

Commit 0af28ed

Browse files
pdettoriclaude
andcommitted
fix: handle LLM and graph errors in weather agent executor
The execute() method had no error handling around get_graph() and graph.astream(). When the LLM call fails (bad API key, model unavailable, network error), the unhandled exception left the A2A task stuck in "submitted" state with no artifacts and no error message — making failures invisible to both tests and users. Now graph initialization and LLM execution errors are caught and reported as failed tasks with the actual error message, matching the existing pattern for MCP connection failures. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Signed-off-by: Paolo Dettori <dettori@us.ibm.com>
1 parent 7249809 commit 0af28ed

1 file changed

Lines changed: 23 additions & 11 deletions

File tree

  • a2a/weather_service/src/weather_service

a2a/weather_service/src/weather_service/agent.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,30 @@ async def execute(self, context: RequestContext, event_queue: EventQueue):
130130
await event_emitter.emit_event(f"Error: Cannot connect to MCP weather service at {os.getenv('MCP_URL', 'http://localhost:8000/sse')}. Please ensure the weather MCP server is running. Error: {tool_error}", failed=True)
131131
return
132132

133-
graph = await get_graph(mcpclient)
134-
async for event in graph.astream(input, stream_mode="updates"):
135-
await event_emitter.emit_event(
136-
"\n".join(
137-
f"🚶‍♂️{key}: {str(value)[:256] + '...' if len(str(value)) > 256 else str(value)}"
138-
for key, value in event.items()
133+
try:
134+
graph = await get_graph(mcpclient)
135+
except Exception as graph_error:
136+
logger.error(f'Failed to create LLM graph: {graph_error}')
137+
await event_emitter.emit_event(f"Error: Failed to initialize LLM graph: {graph_error}", failed=True)
138+
return
139+
140+
try:
141+
async for event in graph.astream(input, stream_mode="updates"):
142+
await event_emitter.emit_event(
143+
"\n".join(
144+
f"🚶‍♂️{key}: {str(value)[:256] + '...' if len(str(value)) > 256 else str(value)}"
145+
for key, value in event.items()
146+
)
147+
+ "\n"
139148
)
140-
+ "\n"
141-
)
142-
output = event
143-
logger.info(f'event: {event}')
144-
output = output.get("assistant", {}).get("final_answer")
149+
output = event
150+
logger.info(f'event: {event}')
151+
except Exception as llm_error:
152+
logger.error(f'LLM execution failed: {llm_error}')
153+
await event_emitter.emit_event(f"Error: LLM execution failed: {llm_error}", failed=True)
154+
return
155+
156+
output = output.get("assistant", {}).get("final_answer") if output else None
145157

146158
# Set span output BEFORE emitting final event (for streaming response capture)
147159
# This populates mlflow.spanOutputs, output.value, gen_ai.completion

0 commit comments

Comments
 (0)