|
5 | 5 | from functools import wraps |
6 | 6 | import httpx |
7 | 7 | import inspect |
| 8 | +import json |
8 | 9 | import logging |
9 | 10 | from typing import ( |
10 | 11 | Any, |
|
37 | 38 | ScoreDataType, |
38 | 39 | StateType, |
39 | 40 | ) |
| 41 | +from langfuse.serializer import EventSerializer |
40 | 42 | from langfuse.types import ObservationParams, SpanLevel |
41 | 43 | from langfuse.utils import _get_timestamp |
42 | 44 | from langfuse.utils.langfuse_singleton import LangfuseSingleton |
@@ -388,11 +390,15 @@ def _get_input_from_func_args( |
388 | 390 | ) -> Any: |
389 | 391 | # Remove implicitly passed "self" or "cls" argument for instance or class methods |
390 | 392 | logged_args = func_args[1:] if is_method else func_args |
391 | | - return { |
| 393 | + raw_input = { |
392 | 394 | "args": logged_args, |
393 | 395 | "kwargs": func_kwargs, |
394 | 396 | } |
395 | 397 |
|
| 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 | + |
396 | 402 | def _finalize_call( |
397 | 403 | self, |
398 | 404 | observation: Optional[ |
@@ -440,10 +446,13 @@ def _handle_call_result( |
440 | 446 | ) |
441 | 447 |
|
442 | 448 | end_time = observation_params["end_time"] or _get_timestamp() |
443 | | - output = observation_params["output"] or ( |
| 449 | + raw_output = observation_params["output"] or ( |
444 | 450 | result if result and capture_output else None |
445 | 451 | ) |
446 | 452 |
|
| 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)) |
447 | 456 | observation_params.update(end_time=end_time, output=output) |
448 | 457 |
|
449 | 458 | if isinstance(observation, (StatefulSpanClient, StatefulGenerationClient)): |
|
0 commit comments