fix(tools): bind JSONDecodeError in ToolConnectionAnalyzer.analyze#5942
fix(tools): bind JSONDecodeError in ToolConnectionAnalyzer.analyze#5942sarathfrancis90 wants to merge 4 commits into
Conversation
When the LLM returns a response that is not valid JSON, ToolConnectionAnalyzer.analyze caught json.JSONDecodeError but the except clause did not bind the exception (`except json.JSONDecodeError:`), while the warning log inside the handler referenced `e`. That raised `NameError: name 'e' is not defined`, masking the real parse error and crashing analyze() instead of degrading gracefully to an empty ToolConnectionMap. Bind the exception with `as e` so the handler logs the parse error and returns an empty connection map as intended. Add regression tests for the malformed-JSON path (previously uncovered), plus valid-JSON and fenced-JSON parsing.
|
Heads up on the red |
|
/adk-pr-analyze |
|
Hi @sarathfrancis90 , Thank you for your contribution! We appreciate you taking the time to submit this pull request. Your PR has been received by the team and is currently under review. We will provide feedback as soon as we have an update to share. |
|
Hi @GWeale , can you please review this. |
Please ensure you have read the contribution guide before creating a pull request.
Link to Issue or Description of Change
2. Or, if no issue exists, describe the change:
Problem:
ToolConnectionAnalyzer.analyze()insrc/google/adk/tools/environment_simulation/tool_connection_analyzer.pycrashes whenever the analyzed LLM returns a response that is not valid JSON.
The JSON parse is wrapped in a
try/except, but theexceptclause does notbind the exception:
The warning log references
e, but theexceptclause binds nothing. So themoment a non-JSON response is parsed, the handler itself raises
NameError: name 'e' is not defined. This masks the real parse error andcrashes
analyze()instead of degrading gracefully to an emptyToolConnectionMapas the surrounding code clearly intends.Reproduced traceback (LLM mocked to return
"this is not json at all"):Solution:
Bind the caught exception with
as eso the handler can log the real parseerror and return an empty
ToolConnectionMapas designed:This is a one-character fix; the surrounding logging and fallback behaviour are
unchanged. After the fix, the same input logs the underlying parse error and
returns
ToolConnectionMap(stateful_parameters=[])without raising.Testing Plan
Unit Tests:
This branch of
analyze()previously had zero coverage, so I addedtests/unittests/tools/environment_simulation/test_tool_connection_analyzer.pycovering:
NameErroron thepre-fix code, passes after the fix),
pytestsummary (low parallelism, as run locally):Verified the regression test fails on the unfixed code with the exact
NameError: name 'e' is not definedand passes after the fix.isortandpyinkreport no changes on the modified files.Manual End-to-End (E2E) Tests:
Reproduced directly against the analyzer with a mocked LLM:
NameError: name 'e' is not defined.Failed to parse tool connection analysis from LLM... Error: Expecting value: line 1 column 1 (char 0)and returns
ToolConnectionMap(stateful_parameters=[]).Checklist
Additional context
The affected module is marked
@experimental(FeatureName.ENVIRONMENT_SIMULATION).