Skip to content

Commit 91231ea

Browse files
authored
fix(media): allow setting IO media via decorator update (#1005)
1 parent b3f465c commit 91231ea

2 files changed

Lines changed: 28 additions & 5 deletions

File tree

langfuse/decorators/langfuse_decorator.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -446,13 +446,18 @@ def _handle_call_result(
446446
)
447447

448448
end_time = observation_params["end_time"] or _get_timestamp()
449-
raw_output = observation_params["output"] or (
450-
result if result and capture_output else None
449+
450+
output = observation_params["output"] or (
451+
# Serialize and deserialize to ensure proper JSON serialization.
452+
# Objects are later serialized again so deserialization is necessary here to avoid unnecessary escaping of quotes.
453+
json.loads(
454+
json.dumps(
455+
result if result and capture_output else None,
456+
cls=EventSerializer,
457+
)
458+
)
451459
)
452460

453-
# Serialize and deserialize to ensure proper JSON serialization.
454-
# Objects are later serialized again so deserialization is necessary here to avoid unnecessary escaping of quotes.
455-
output = json.loads(json.dumps(raw_output, cls=EventSerializer))
456461
observation_params.update(end_time=end_time, output=output)
457462

458463
if isinstance(observation, (StatefulSpanClient, StatefulGenerationClient)):

tests/test_decorators.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,6 +1473,16 @@ def test_media():
14731473
@observe()
14741474
def main():
14751475
langfuse_context.update_current_trace(
1476+
input={
1477+
"context": {
1478+
"nested": media,
1479+
},
1480+
},
1481+
output={
1482+
"context": {
1483+
"nested": media,
1484+
},
1485+
},
14761486
metadata={
14771487
"context": {
14781488
"nested": media,
@@ -1486,6 +1496,14 @@ def main():
14861496

14871497
trace_data = get_api().trace.get(mock_trace_id)
14881498

1499+
assert (
1500+
"@@@langfuseMedia:type=application/pdf|id="
1501+
in trace_data.input["context"]["nested"]
1502+
)
1503+
assert (
1504+
"@@@langfuseMedia:type=application/pdf|id="
1505+
in trace_data.output["context"]["nested"]
1506+
)
14891507
assert (
14901508
"@@@langfuseMedia:type=application/pdf|id="
14911509
in trace_data.metadata["context"]["nested"]

0 commit comments

Comments
 (0)