Skip to content

Commit f11776e

Browse files
committed
revert decorator io media
1 parent 4b269a0 commit f11776e

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

langfuse/decorators/langfuse_decorator.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from functools import wraps
66
import httpx
77
import inspect
8+
import json
89
import logging
910
from typing import (
1011
Any,
@@ -37,6 +38,7 @@
3738
ScoreDataType,
3839
StateType,
3940
)
41+
from langfuse.serializer import EventSerializer
4042
from langfuse.types import ObservationParams, SpanLevel
4143
from langfuse.utils import _get_timestamp
4244
from langfuse.utils.langfuse_singleton import LangfuseSingleton
@@ -388,11 +390,15 @@ def _get_input_from_func_args(
388390
) -> Any:
389391
# Remove implicitly passed "self" or "cls" argument for instance or class methods
390392
logged_args = func_args[1:] if is_method else func_args
391-
return {
393+
raw_input = {
392394
"args": logged_args,
393395
"kwargs": func_kwargs,
394396
}
395397

398+
# Serialize and deserialize to ensure proper JSON serialization.
399+
# Objects are later serialized again so deserialization is necessary here to avoid unnecessary escaping of quotes.
400+
return json.loads(json.dumps(raw_input, cls=EventSerializer))
401+
396402
def _finalize_call(
397403
self,
398404
observation: Optional[
@@ -440,10 +446,13 @@ def _handle_call_result(
440446
)
441447

442448
end_time = observation_params["end_time"] or _get_timestamp()
443-
output = observation_params["output"] or (
449+
raw_output = observation_params["output"] or (
444450
result if result and capture_output else None
445451
)
446452

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))
447456
observation_params.update(end_time=end_time, output=output)
448457

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

tests/test_decorators.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,19 +1462,21 @@ def main():
14621462
assert len(child_observations) == 2
14631463

14641464

1465-
def test_pdf_in_metadata():
1465+
def test_media():
14661466
mock_trace_id = create_uuid()
14671467

14681468
with open("static/bitcoin.pdf", "rb") as pdf_file:
14691469
pdf_bytes = pdf_file.read()
14701470

1471+
media = LangfuseMedia(content_bytes=pdf_bytes, content_type="application/pdf")
1472+
14711473
@observe()
14721474
def main():
14731475
langfuse_context.update_current_trace(
14741476
metadata={
1475-
"context": LangfuseMedia(
1476-
content_bytes=pdf_bytes, content_type="application/pdf"
1477-
)
1477+
"context": {
1478+
"nested": media,
1479+
},
14781480
},
14791481
)
14801482

@@ -1484,4 +1486,13 @@ def main():
14841486

14851487
trace_data = get_api().trace.get(mock_trace_id)
14861488

1487-
assert "@@@langfuseMedia:type=application/pdf|id=" in trace_data.metadata["context"]
1489+
assert (
1490+
"@@@langfuseMedia:type=application/pdf|id="
1491+
in trace_data.metadata["context"]["nested"]
1492+
)
1493+
parsed_reference_string = LangfuseMedia.parse_reference_string(
1494+
trace_data.metadata["context"]["nested"]
1495+
)
1496+
assert parsed_reference_string["content_type"] == "application/pdf"
1497+
assert parsed_reference_string["media_id"] is not None
1498+
assert parsed_reference_string["source"] == "bytes"

0 commit comments

Comments
 (0)