fix: handle ValueError and RecursionError in _safe_json_serialize#5414
Open
voidborne-d wants to merge 1 commit intogoogle:mainfrom
Open
fix: handle ValueError and RecursionError in _safe_json_serialize#5414voidborne-d wants to merge 1 commit intogoogle:mainfrom
voidborne-d wants to merge 1 commit intogoogle:mainfrom
Conversation
Both _safe_json_serialize implementations only catch TypeError and OverflowError from json.dumps, but json.dumps can also raise: - ValueError: circular references (e.g. object referencing itself) - RecursionError: deeply nested structures exceeding recursion limit In telemetry/tracing.py, this is especially critical because _safe_json_serialize is called inside a finally block during trace_tool_call — an unhandled exception here can suppress the original tool execution result. In models/lite_llm.py, an unhandled exception during message serialization causes hard LLM request construction failures where a graceful fallback is expected. Fix: - Add ValueError and RecursionError to the exception tuple in both implementations. - In lite_llm.py, wrap the str() fallback in a secondary try/except RecursionError since str() can also fail on deeply recursive objects. 8 regression tests added across both modules. Fixes google#5411 Fixes google#5412
17dc740 to
87499ee
Compare
Collaborator
|
Hi @voidborne-d , Thank you for your contribution! We appreciate you taking the time to submit this pull request. Please fix formatting errors by running autoformat.sh |
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
Both
_safe_json_serializeimplementations only catchTypeErrorandOverflowErrorfromjson.dumps, butjson.dumpscan also raise:ValueError: circular references (e.g.obj.ref = obj)RecursionError: deeply nested structures exceeding Python's recursion limitImpact
telemetry/tracing.py(critical):_safe_json_serializeis called inside afinallyblock duringtrace_tool_call. An unhandled exception here can suppress the original tool execution result — telemetry failures should never affect runtime correctness.models/lite_llm.py: An unhandled exception during message serialization causes hard LLM request construction failures where a graceful fallback is expected.Root Cause
The
exceptclause catches(TypeError, OverflowError)but notValueErrororRecursionError. Both are documentedjson.dumpsfailure modes:ValueErrorfor circular references (per Python docs)RecursionErrorfor structures exceedingsys.getrecursionlimit()Note: in
tracing.py, thedefault=lambda o: ...callback handlesTypeErrorfor individual non-serializable objects, but circular references and deep recursion bypass thedefaultcallback entirely.Fix
ValueErrorandRecursionErrorto the exception tuple in both implementations.lite_llm.py, wrap thestr()fallback in a secondarytry/except RecursionErrorsincestr()→__repr__()can also fail on deeply recursive objects.Reproduction
Tests
8 regression tests added across both modules:
test_safe_json_serialize.py(telemetry): circular ref, deep nesting, normal dict, non-serializable objecttest_litellm_safe_serialize.py(models): circular ref, deep nesting, normal dict, non-serializable objectAll 8 tests pass.
Fixes #5411
Fixes #5412