Skip to content

Commit dfa93fc

Browse files
committed
fix(serializer): lazy-load langchain Serializable
1 parent c7d5d44 commit dfa93fc

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

langfuse/_utils/serializer.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,33 @@
77
from collections.abc import Sequence
88
from dataclasses import asdict, is_dataclass
99
from datetime import date, datetime
10+
from functools import lru_cache
1011
from json import JSONEncoder
1112
from logging import getLogger
1213
from pathlib import Path
13-
from typing import Any
14+
from typing import Any, Optional, Type
1415
from uuid import UUID
1516

1617
from pydantic import BaseModel
1718

1819
from langfuse.media import LangfuseMedia
1920

20-
# Attempt to import Serializable
21-
try:
22-
from langchain_core.load.serializable import Serializable
23-
except ImportError:
24-
# If Serializable is not available, set it to a placeholder type
25-
class Serializable: # type: ignore
26-
pass
21+
22+
@lru_cache(maxsize=1)
23+
def _get_langchain_serializable_type() -> Optional[Type[Any]]:
24+
"""Best-effort lookup of LangChain's Serializable base class.
25+
26+
Import lazily to avoid import-time side effects from optional langchain
27+
dependencies, including Python 3.14 warning-as-error failures triggered by
28+
transitive `pydantic.v1` imports.
29+
"""
30+
31+
try:
32+
from langchain_core.load.serializable import Serializable
33+
34+
return Serializable
35+
except Exception:
36+
return None
2737

2838

2939
# Attempt to import numpy
@@ -109,8 +119,8 @@ def default(self, obj: Any) -> Any:
109119
if isinstance(obj, Path):
110120
return str(obj)
111121

112-
# if langchain is not available, the Serializable type is NoneType
113-
if Serializable is not type(None) and isinstance(obj, Serializable): # type: ignore
122+
serializable_type = _get_langchain_serializable_type()
123+
if serializable_type is not None and isinstance(obj, serializable_type):
114124
return obj.to_json()
115125

116126
# 64-bit integers might overflow the JavaScript safe integer range.

0 commit comments

Comments
 (0)