fix(tools): bind JSONDecodeError in ToolConnectionAnalyzer.analyze#5942
Open
sarathfrancis90 wants to merge 1 commit into
Open
fix(tools): bind JSONDecodeError in ToolConnectionAnalyzer.analyze#5942sarathfrancis90 wants to merge 1 commit into
sarathfrancis90 wants to merge 1 commit 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.
Author
|
Heads up on the red |
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.
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).