fix: prevent UnboundLocalError in A2A telemetry and escape SSE error JSON - #181
Open
WangXukang-cypher wants to merge 1 commit into
Open
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
a2a_app.py:
_wrap_agent_executor_execute_funcreferencesresultin thefinallyblock, butresultis only assigned insidetry. Whenexecute_funcraises, the
finallyclause hitsUnboundLocalError, which suppresses theoriginal exception and makes debugging impossible. Fix: initialize
result = Nonebefore the
tryblock.agent_server_app.py: The
_invoke_compatSSE error path builds JSON viaf-string (
f'data: {{"error": "{str(e)}"}}'). If the exception message contains",\, or newlines, the resulting JSON is malformed and breaks client-sideparsing. The
/run_sseendpoint at line 475 already usesjson.dumps()correctly;this patch aligns
_invoke_compatto the same pattern.Test plan
pytest tests/apps/test_a2a_app.py— 20 passedpytest tests/apps/test_agent_server_invoke.py— 19 passedruff check— no new lint errors (count decreased from 13 → 12)ruff format --check— both files formatted