Skip to content

Commit

Permalink
fix(sdk): fail gracefully in case input/output serialization failure
Browse files Browse the repository at this point in the history
  • Loading branch information
nirga committed Feb 22, 2024
1 parent 478e833 commit b64a03c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 18 deletions.
16 changes: 16 additions & 0 deletions packages/traceloop-sdk/tests/test_workflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,19 @@ def joke_workflow():
}

assert json.loads(task_span.attributes.get("traceloop.entity.output")) == joke


@pytest.mark.vcr
def test_unserializable_workflow(exporter):
@task(name="unserializable_task")
def unserializable_task(obj: object):
return object()

@workflow(name="unserializable_workflow")
def unserializable_workflow(obj: object):
return unserializable_task(obj)

unserializable_task(object())

spans = exporter.get_finished_spans()
assert [span.name for span in spans] == ["unserializable_task.task"]
48 changes: 30 additions & 18 deletions packages/traceloop-sdk/traceloop/sdk/decorators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,24 @@ def wrap(*args, **kwargs):
)
span.set_attribute(SpanAttributes.TRACELOOP_ENTITY_NAME, name)

if _should_send_prompts():
span.set_attribute(
SpanAttributes.TRACELOOP_ENTITY_INPUT,
json.dumps({"args": args, "kwargs": kwargs}),
)
try:
if _should_send_prompts():
span.set_attribute(
SpanAttributes.TRACELOOP_ENTITY_INPUT,
json.dumps({"args": args, "kwargs": kwargs}),
)
except TypeError:
pass # Some args might not be serializable

res = fn(*args, **kwargs)

if _should_send_prompts():
span.set_attribute(
SpanAttributes.TRACELOOP_ENTITY_OUTPUT, json.dumps(res)
)
try:
if _should_send_prompts():
span.set_attribute(
SpanAttributes.TRACELOOP_ENTITY_OUTPUT, json.dumps(res)
)
except TypeError:
pass # Some outputs might not be serializable

return res

Expand Down Expand Up @@ -116,18 +122,24 @@ def wrap(*args, **kwargs):
)
span.set_attribute(SpanAttributes.TRACELOOP_ENTITY_NAME, name)

if _should_send_prompts():
span.set_attribute(
SpanAttributes.TRACELOOP_ENTITY_INPUT,
json.dumps({"args": args, "kwargs": kwargs}),
)
try:
if _should_send_prompts():
span.set_attribute(
SpanAttributes.TRACELOOP_ENTITY_INPUT,
json.dumps({"args": args, "kwargs": kwargs}),
)
except TypeError:
pass # Some args might not be serializable

res = fn(*args, **kwargs)

if _should_send_prompts():
span.set_attribute(
SpanAttributes.TRACELOOP_ENTITY_OUTPUT, json.dumps(res)
)
try:
if _should_send_prompts():
span.set_attribute(
SpanAttributes.TRACELOOP_ENTITY_OUTPUT, json.dumps(res)
)
except TypeError:
pass # Some outputs might not be serializable

return res

Expand Down

0 comments on commit b64a03c

Please sign in to comment.