You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`_handle_span_input` and `_handle_span_output` in `traceloop/sdk/decorators/base.py` only caught `TypeError` around `json.dumps(..., cls=JSONEncoder)`. `JSONEncoder.default` calls an object's own `to_json()`, `model_dump()`, `dict()` or `json()`, and any of these can raise any exception. For example, `httpx.Response(204).json()` raises `JSONDecodeError`. `json.dumps` also raises `ValueError` on a circular reference. These errors escaped from the two handlers.
- **Input side:** the handler runs after `_setup_span` has attached the workflow/agent name, span and entity-path tokens, but outside the cleanup try/finally. So the decorated function never ran, the span was never ended, and the name and path leaked to later spans.
- **Output side, sync:** the result was lost and the span leaked.
- **Output side, async:** the instrumentation's own failure was recorded on the span as an ERROR and the user's result was lost.
Change
`_handle_span_input` and `_handle_span_output` now catch `Exception` instead of only `TypeError`. On an encoder error only the entity input or output attribute is left out, as already happened for a `TypeError`. The function runs normally, its span is ended and the name and path tokens are detached. All four wrappers (sync, async, generator, async generator) share these handlers.
I added `tests/test_decorator_serialization_errors.py` with one test per contract row (r1–r7). Each test runs with two inputs: an object whose `.json()` raises `JSONDecodeError` (like `httpx.Response(204)`) and a circular list. The tests check that:
- the function returns or yields normally;
- span status is UNSET;
- the failed attribute is absent and the other one is still recorded;
- a later sibling `@task` has no leaked workflow name or entity path;
- the user's own exception still propagates, with one exception event (r3);
- the async output case has no exception event.
All 14 cases failed before the fix with the errors from the issue. After it they pass, with the nearby decorator tests (69 passed). Ruff and mypy report no issues.
Behaviour changes: decorated functions no longer raise when their arguments or return value fail to serialize for reasons other than `TypeError`; the attribute is left out instead. Async tasks whose return value fails to serialize are no longer marked ERROR.
Not addressed: none.
Scope
slice 1 of 1
covers rows: 7 of 7 in rows
Out of scope:
Changing JSONEncoder.default to fall back to the class name when .json()/to_json()/dict() raise: This would change the attribute contents that are recorded. The issue asks only that the function runs and the attribute is omitted, as for TypeError today.
Unguarded json.dumps in guardrail/guardrail.py and guardrail/guards.py: These are separate code paths not named in the issue. A follow-up issue is needed if they can raise on real data.
Logging or debug-reporting the swallowed serialization exception: The existing TypeError path is a silent pass. Adding logging is a separate design change.
Abandoned async generator without aclose leaking the name: This is a documented known limitation in async_gen_wrap and is unrelated to serialization.
Regression evidence
Each test below failed on the base commit and passes with this change, run offline in the heal harness:
2 of 2 cases: agen.workflow, sibling.child, sibling.task recorded after the fix (absent before)
108 more rows not shown
Review
`_handle_span_input` and `_handle_span_output` in decorators/base.py now catch `Exception`, not only `TypeError`. Encoder failures (JSONDecodeError from `.json()`, ValueError on cycles, errors from a user's `to_json`/`dict`) now leave out only the input/output attribute. Decorated functions run and return normally, spans end and context tokens are detached. Seven tests cover the sync, async, generator and async-generator wrappers, each with two inputs. They check span status, attributes, exception events, user-exception propagation and that no context leaks to a sibling span.
Before merging
CI green: Lint, Build Packages, Test Packages (3.10, 3.11, 3.12)
Serialization errors encountered while recording span inputs or outputs no longer interrupt task and workflow execution. This includes errors from response-like objects and circular data, across synchronous and asynchronous tasks, workflows, and generators.
Functions and generators continue to return or yield their results, and user-raised exceptions continue to behave as expected.
No actionable comments were generated in the recent review.
Walkthrough
The span input and output handlers now suppress exceptions raised during serialization or span-attribute recording. New tests cover synchronous, asynchronous, and generator decorators with serialization failures.
The input and output handlers catch exceptions during serialization and span-attribute recording. Tests check function results, propagated user errors, span data, and sibling-span attributes across synchronous, asynchronous, and generator calls.
traceloop/openllmetry#4687: It changes the same handlers to catch serialization exceptions and adds tests for decorated call types.
Suggested reviewers:nirga
Merge Risk: Minimal · up to 7df54
Decorated functions will no longer fail when their inputs or outputs cannot be serialized for tracing; the affected attribute is omitted instead. The change is small and reversible, and no merge-blocking risk was found.
Pre-merge checks: 4
Check name
Status
Explanation
Description Check
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
The title names the main change: decorator input and output serialization errors no longer crash decorated functions. The raw summary says _handle_span_input and _handle_span_output now suppress any e…
Linked Issues check
[#4686] requires that non-TypeError encoder failures in @task/@workflow input and output handling not escape into the decorated function. At the reviewed head, _handle_span_input and `_handle_span_o…
Out of Scope Changes check
The PR changes two areas: the exception handling in _handle_span_input and _handle_span_output in packages/traceloop-sdk/traceloop/sdk/decorators/base.py, and the new test file `packages/tracelo…
Full details: Title check
Explanation
The title names the main change: decorator input and output serialization errors no longer crash decorated functions. The raw summary says _handle_span_input and _handle_span_output now suppress any exception during serialization or span recording, not only TypeError. The objectives describe the same behavior for sync, async, generator, and async-generator wrappers. The title uses the fix(sdk) convention and stays within 77 characters, so it is specific and concise.
Full details: Linked Issues check
Explanation
[#4686] requires that non-TypeError encoder failures in @task/@workflow input and output handling not escape into the decorated function. At the reviewed head, _handle_span_input and _handle_span_output in packages/traceloop-sdk/traceloop/sdk/decorators/base.py wrap the json.dumps(..., cls=JSONEncoder) call in except Exception: pass. Both handlers are called from all four wrappers: sync_wrap, async_wrap, async_gen_wrap, and the sync generator path. The input handler runs after _setup_span and before the try/finally that ends the span, so the catch-all keeps the function running. The output handler runs before _cleanup_span, so the span ends and the tokens detach. For async output, the catch-all also stops the failure from being recorded as ERROR. The new file packages/traceloop-sdk/tests/test_decorator_serialization_errors.py covers the issue triggers: an object whose .json() raises JSONDecodeError and a circular list. It runs them through sync, async, generator, and async-generator wrappers. It checks that the function returns or yields normally, span status is UNSET, the failed attribute is omitted, and a later sibling span has no leaked workflow name or entity path. It also checks that a user exception still propagates. The issue has no non-coding requirements. Limitation: the diff tool could not read the changed blobs, so the base-side behavior is inferred from the PR description. The head source and test file were read directly. The pass/fail results and lint claims in the PR description were not re-run.
Full details: Out of Scope Changes check
Explanation
The PR changes two areas: the exception handling in _handle_span_input and _handle_span_output in packages/traceloop-sdk/traceloop/sdk/decorators/base.py, and the new test file packages/traceloop-sdk/tests/test_decorator_serialization_errors.py. Both connect directly to the issue's requirement. The new tests support that requirement. The PR description lists excluded items, including JSONEncoder.default fallback changes, guardrail json.dumps calls, logging, and the async-generator abandonment limitation. None of these appear in the reviewed head file for base.py, so no unrelated change is shown. The telemetry tables in the PR description are evidence text, not code changes. Limitation: the full diff could not be read, so unrelated edits elsewhere in the PR cannot be fully excluded. The files listed in the PR summary are consistent with the stated scope.
Finishing Touches
Generate unit tests (beta)
Commit to this branch
Create a new PR
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 3 remain after this review.
Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.
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
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.
Fixes #4686
Root cause
`_handle_span_input` and `_handle_span_output` in `traceloop/sdk/decorators/base.py` only caught `TypeError` around `json.dumps(..., cls=JSONEncoder)`. `JSONEncoder.default` calls an object's own `to_json()`, `model_dump()`, `dict()` or `json()`, and any of these can raise any exception. For example, `httpx.Response(204).json()` raises `JSONDecodeError`. `json.dumps` also raises `ValueError` on a circular reference. These errors escaped from the two handlers.
- **Input side:** the handler runs after `_setup_span` has attached the workflow/agent name, span and entity-path tokens, but outside the cleanup try/finally. So the decorated function never ran, the span was never ended, and the name and path leaked to later spans.
- **Output side, sync:** the result was lost and the span leaked.
- **Output side, async:** the instrumentation's own failure was recorded on the span as an ERROR and the user's result was lost.
Change
`_handle_span_input` and `_handle_span_output` now catch `Exception` instead of only `TypeError`. On an encoder error only the entity input or output attribute is left out, as already happened for a `TypeError`. The function runs normally, its span is ended and the name and path tokens are detached. All four wrappers (sync, async, generator, async generator) share these handlers.
I added `tests/test_decorator_serialization_errors.py` with one test per contract row (r1–r7). Each test runs with two inputs: an object whose `.json()` raises `JSONDecodeError` (like `httpx.Response(204)`) and a circular list. The tests check that:
- the function returns or yields normally;
- span status is UNSET;
- the failed attribute is absent and the other one is still recorded;
- a later sibling `
@task` has no leaked workflow name or entity path;- the user's own exception still propagates, with one exception event (r3);
- the async output case has no exception event.
All 14 cases failed before the fix with the errors from the issue. After it they pass, with the nearby decorator tests (69 passed). Ruff and mypy report no issues.
Behaviour changes: decorated functions no longer raise when their arguments or return value fail to serialize for reasons other than `TypeError`; the attribute is left out instead. Async tasks whose return value fails to serialize are no longer marked ERROR.
Not addressed: none.
Scope
inrowsOut of scope:
Regression evidence
Each test below failed on the base commit and passes with this change, run offline in the heal harness:
traceloop-sdk:tests/test_decorator_serialization_errors.py::test_async_generator_unserializable_input_yields(failed before: exception)traceloop-sdk:tests/test_decorator_serialization_errors.py::test_async_unserializable_input_runs_function(failed before: exception)traceloop-sdk:tests/test_decorator_serialization_errors.py::test_async_unserializable_output_returns_result(failed before: exception)traceloop-sdk:tests/test_decorator_serialization_errors.py::test_sync_generator_unserializable_input_yields(failed before: exception)traceloop-sdk:tests/test_decorator_serialization_errors.py::test_sync_unserializable_input_runs_function(failed before: exception)traceloop-sdk:tests/test_decorator_serialization_errors.py::test_sync_unserializable_input_user_exception_propagates(failed before: exception)traceloop-sdk:tests/test_decorator_serialization_errors.py::test_sync_unserializable_output_returns_result(failed before: exception)Package suites: no new failures against the offline baseline.
Ruff: clean.
Mypy: clean.
Telemetry evidence
tests/test_decorator_serialization_errors.py::test_sync_unserializable_input_runs_function2 of 2 cases: wf.workflow, sibling.child, sibling.task recorded after the fix (absent before)
tests/test_decorator_serialization_errors.py::test_sync_unserializable_output_returns_result2 of 2 cases: produce.task, sibling.child, sibling.task recorded after the fix (absent before)
tests/test_decorator_serialization_errors.py::test_sync_unserializable_input_user_exception_propagates2 of 2 cases: fails.task recorded after the fix (absent before)
tests/test_decorator_serialization_errors.py::test_async_unserializable_input_runs_function2 of 2 cases: wf.workflow, sibling.child, sibling.task recorded after the fix (absent before)
tests/test_decorator_serialization_errors.py::test_async_unserializable_output_returns_result2 of 2 cases: sibling.child, sibling.task recorded after the fix (absent before); 3 span fields changed
6 attributes unchanged
tests/test_decorator_serialization_errors.py::test_sync_generator_unserializable_input_yields2 of 2 cases: gen.task, sibling.child, sibling.task recorded after the fix (absent before)
tests/test_decorator_serialization_errors.py::test_async_generator_unserializable_input_yields2 of 2 cases: agen.workflow, sibling.child, sibling.task recorded after the fix (absent before)
108 more rows not shown
Review
`_handle_span_input` and `_handle_span_output` in decorators/base.py now catch `Exception`, not only `TypeError`. Encoder failures (JSONDecodeError from `.json()`, ValueError on cycles, errors from a user's `to_json`/`dict`) now leave out only the input/output attribute. Decorated functions run and return normally, spans end and context tokens are detached. Seven tests cover the sync, async, generator and async-generator wrappers, each with two inputs. They check span status, attributes, exception events, user-exception propagation and that no context leaks to a sibling span.
Before merging
🤖 Generated with Claude Code
Summary by CodeRabbit