fix(tracing): catch ValueError and RecursionError in _safe_json_serialize#5419
Open
nileshpatil6 wants to merge 1 commit intogoogle:mainfrom
Open
fix(tracing): catch ValueError and RecursionError in _safe_json_serialize#5419nileshpatil6 wants to merge 1 commit intogoogle:mainfrom
nileshpatil6 wants to merge 1 commit intogoogle:mainfrom
Conversation
Author
|
I have read the Contributor License Agreement and I hereby agree to its terms. |
Collaborator
|
Response from ADK Triaging Agent Hello @nileshpatil6, thank you for creating this PR! Could you please include a |
Author
Testing PlanManual verification: import json
from google.adk.telemetry.tracing import _safe_json_serialize
# Test 1: Circular reference (previously raised ValueError)
circular = {}
circular['self'] = circular
assert _safe_json_serialize(circular) == '<not serializable>'
# Test 2: Deeply nested structure (previously raised RecursionError)
deep = {}
node = deep
for _ in range(1000):
node['child'] = {}
node = node['child']
result = _safe_json_serialize(deep)
assert result == '<not serializable>'
# Test 3: Normal objects still serialize correctly
assert _safe_json_serialize({'key': 'value'}) == '{"key": "value"}'
print('All tests passed')Before fix: After fix: Returns |
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.
Problem
_safe_json_serializeintelemetry/tracing.pyonly catchesTypeErrorandOverflowError, butjson.dumpscan also raise:ValueError— e.g. circular references (json.dumpsraisesValueError: Circular reference detected)RecursionError— e.g. deeply nested structures exceeding Python's recursion limitBecause this function is called inside
finallyblocks during tool execution telemetry, an uncaught exception here can interrupt normal execution flow and affect tool results.Fixes #5411
Fix
Add
ValueErrorandRecursionErrorto the existing except clause: