From 125b135298346235980cfc5c6d7942a5b97c2982 Mon Sep 17 00:00:00 2001 From: Sh0rck_Wang Date: Fri, 4 Sep 2026 13:39:48 +0800 Subject: [PATCH] fix: prevent UnboundLocalError in A2A telemetry and escape SSE error JSON 1. a2a_app.py: initialize `result = None` before the try block so the finally clause can always reference it when execute_func raises. Previously an UnboundLocalError would suppress the original exception. 2. agent_server_app.py: use json.dumps() instead of an f-string to build the SSE error payload, matching the /run_sse path. Exception messages containing quotes or backslashes previously produced malformed JSON. --- agentkit/apps/a2a_app/a2a_app.py | 1 + agentkit/apps/agent_server_app/agent_server_app.py | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/agentkit/apps/a2a_app/a2a_app.py b/agentkit/apps/a2a_app/a2a_app.py index 37e9c711..1d079322 100644 --- a/agentkit/apps/a2a_app/a2a_app.py +++ b/agentkit/apps/a2a_app/a2a_app.py @@ -46,6 +46,7 @@ async def wrapper(*args, **kwargs): with telemetry.tracer.start_as_current_span(name="a2a_invocation") as span: exception = None + result = None try: result = await execute_func( executor_instance, context=context, event_queue=event_queue diff --git a/agentkit/apps/agent_server_app/agent_server_app.py b/agentkit/apps/agent_server_app/agent_server_app.py index 3988e427..6872c9a7 100644 --- a/agentkit/apps/agent_server_app/agent_server_app.py +++ b/agentkit/apps/agent_server_app/agent_server_app.py @@ -618,7 +618,9 @@ async def event_generator(): telemetry.trace_agent_server_finish( path="/invoke", func_result="", exception=e ) - error_payload = f'data: {{"error": "{str(e)}"}}\n\n' + error_payload = ( + "data: " + json.dumps({"error": str(e)}) + "\n\n" + ) if error_payload is not None: # The identity path never suspends while retaining the # original exception or its credential-bearing traceback.