Skip to content

fix(tracing): catch ValueError and RecursionError in _safe_json_serialize#5419

Open
nileshpatil6 wants to merge 1 commit intogoogle:mainfrom
nileshpatil6:fix/safe-json-serialize-valueerror
Open

fix(tracing): catch ValueError and RecursionError in _safe_json_serialize#5419
nileshpatil6 wants to merge 1 commit intogoogle:mainfrom
nileshpatil6:fix/safe-json-serialize-valueerror

Conversation

@nileshpatil6
Copy link
Copy Markdown

Problem

_safe_json_serialize in telemetry/tracing.py only catches TypeError and OverflowError, but json.dumps can also raise:

  • ValueError — e.g. circular references (json.dumps raises ValueError: Circular reference detected)
  • RecursionError — e.g. deeply nested structures exceeding Python's recursion limit

Because this function is called inside finally blocks during tool execution telemetry, an uncaught exception here can interrupt normal execution flow and affect tool results.

Fixes #5411

Fix

Add ValueError and RecursionError to the existing except clause:

except (TypeError, OverflowError, ValueError, RecursionError):
    return '<not serializable>'

@nileshpatil6
Copy link
Copy Markdown
Author

I have read the Contributor License Agreement and I hereby agree to its terms.

@adk-bot adk-bot added the tracing [Component] This issue is related to OpenTelemetry tracing label Apr 20, 2026
@adk-bot
Copy link
Copy Markdown
Collaborator

adk-bot commented Apr 20, 2026

Response from ADK Triaging Agent

Hello @nileshpatil6, thank you for creating this PR!

Could you please include a testing plan section in your PR to describe how you tested your changes? This will help reviewers to review your PR more efficiently. Thanks!

@nileshpatil6
Copy link
Copy Markdown
Author

Testing Plan

Manual 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: _safe_json_serialize with a circular reference raises ValueError: Circular reference detected, which propagates out of the finally block and crashes tool execution.

After fix: Returns '<not serializable>' gracefully for all failure modes.

@rohityan rohityan self-assigned this Apr 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tracing [Component] This issue is related to OpenTelemetry tracing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

_safe_json_serialize in telemetry/tracing.py is not fully safe and can break tool execution when JSON serialization fails

3 participants