Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions packages/traceloop-sdk/tests/test_json_encoder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from pathlib import Path
import pytest
from pydantic import BaseModel

from traceloop.sdk.decorators import task
from opentelemetry.semconv_ai import SpanAttributes


def test_json_encoder_task(exporter, recwarn):

class TestValue(BaseModel):
value: int

@task(name="test_task")
def test_method(a: TestValue, b: TestValue):
return TestValue(value=a.value + b.value)

result = test_method(TestValue(value=2), TestValue(value=3))

assert result.value == 5

spans = exporter.get_finished_spans()
assert len(spans) == 1
span = spans[0]
assert span.attributes[SpanAttributes.TRACELOOP_ENTITY_INPUT] == r'{"args": ["{\"value\":2}", "{\"value\":3}"], "kwargs": {}}'
assert span.attributes[SpanAttributes.TRACELOOP_ENTITY_OUTPUT] == r'"{\"value\":5}"'

for warning in recwarn:
file = Path(warning.filename)
if file.name == "json_encoder.py" and "`json` method is deprecated" in str(warning.message):
pytest.fail(f"Deprecation warning found: {warning.message}")
3 changes: 3 additions & 0 deletions packages/traceloop-sdk/traceloop/sdk/utils/json_encoder.py
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have this method in other packages as well - can you update them as well? Also, can you bump pydantic so the test will actually work with the right version?

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ def default(self, o):
if hasattr(o, "to_json"):
return o.to_json()

if hasattr(o, "model_dump_json"):
return o.model_dump_json()

if hasattr(o, "json"):
return o.json()

Expand Down
Loading