Skip to content

Commit c1b4169

Browse files
committed
feat(langchain): add v1 support
1 parent 1b61c36 commit c1b4169

10 files changed

Lines changed: 1308 additions & 1816 deletions

langfuse/_utils/serializer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
# Attempt to import Serializable
2121
try:
22-
from langchain.load.serializable import Serializable
22+
from langchain_core.load.serializable import Serializable
2323
except ImportError:
2424
# If Serializable is not available, set it to a placeholder type
2525
class Serializable: # type: ignore

langfuse/langchain/CallbackHandler.py

Lines changed: 54 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
1-
import typing
21
from contextvars import Token
2+
from typing import (
3+
Any,
4+
Dict,
5+
List,
6+
Literal,
7+
Optional,
8+
Sequence,
9+
Set,
10+
Type,
11+
Union,
12+
cast,
13+
)
14+
from uuid import UUID
315

416
import pydantic
517
from opentelemetry import context, trace
@@ -16,41 +28,52 @@
1628
LangfuseSpan,
1729
LangfuseTool,
1830
)
31+
from langfuse._utils import _get_timestamp
32+
from langfuse.langchain.utils import _extract_model_name
1933
from langfuse.logger import langfuse_logger
2034

2135
try:
22-
import langchain # noqa
23-
24-
except ImportError as e:
25-
langfuse_logger.error(
26-
f"Could not import langchain. The langchain integration will not work. {e}"
27-
)
36+
import langchain
2837

29-
from typing import Any, Dict, List, Literal, Optional, Sequence, Set, Type, Union, cast
30-
from uuid import UUID
38+
if langchain.__version__.startswith("1"):
39+
# Langchain v1
40+
from langchain_core.agents import AgentAction, AgentFinish
41+
from langchain_core.callbacks import (
42+
BaseCallbackHandler as LangchainBaseCallbackHandler,
43+
)
44+
from langchain_core.documents import Document
45+
from langchain_core.messages import (
46+
AIMessage,
47+
BaseMessage,
48+
ChatMessage,
49+
FunctionMessage,
50+
HumanMessage,
51+
SystemMessage,
52+
ToolMessage,
53+
)
54+
from langchain_core.outputs import ChatGeneration, LLMResult
3155

32-
from langfuse._utils import _get_timestamp
33-
from langfuse.langchain.utils import _extract_model_name
56+
else:
57+
# Langchain v0
58+
from langchain.callbacks.base import ( # type: ignore
59+
BaseCallbackHandler as LangchainBaseCallbackHandler,
60+
)
61+
from langchain.schema.agent import AgentAction, AgentFinish # type: ignore
62+
from langchain.schema.document import Document # type: ignore
63+
from langchain_core.messages import (
64+
AIMessage,
65+
BaseMessage,
66+
ChatMessage,
67+
FunctionMessage,
68+
HumanMessage,
69+
SystemMessage,
70+
ToolMessage,
71+
)
72+
from langchain_core.outputs import (
73+
ChatGeneration,
74+
LLMResult,
75+
)
3476

35-
try:
36-
from langchain.callbacks.base import (
37-
BaseCallbackHandler as LangchainBaseCallbackHandler,
38-
)
39-
from langchain.schema.agent import AgentAction, AgentFinish
40-
from langchain.schema.document import Document
41-
from langchain_core.messages import (
42-
AIMessage,
43-
BaseMessage,
44-
ChatMessage,
45-
FunctionMessage,
46-
HumanMessage,
47-
SystemMessage,
48-
ToolMessage,
49-
)
50-
from langchain_core.outputs import (
51-
ChatGeneration,
52-
LLMResult,
53-
)
5477
except ImportError:
5578
raise ModuleNotFoundError(
5679
"Please install langchain to use the Langfuse langchain integration: 'pip install langchain'"
@@ -1011,7 +1034,7 @@ def _flatten_comprehension(matrix: Any) -> Any:
10111034
return [item for row in matrix for item in row]
10121035

10131036

1014-
def _parse_usage_model(usage: typing.Union[pydantic.BaseModel, dict]) -> Any:
1037+
def _parse_usage_model(usage: Union[pydantic.BaseModel, dict]) -> Any:
10151038
# maintains a list of key translations. For each key, the usage model is checked
10161039
# and a new object will be created with the new key if the key exists in the usage model
10171040
# All non matched keys will remain on the object.

poetry.lock

Lines changed: 1240 additions & 1334 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@ license = "MIT"
88
readme = "README.md"
99

1010
[tool.poetry.dependencies]
11-
python = ">=3.9,<4.0"
11+
python = ">=3.10,<4.0"
1212
httpx = ">=0.15.4,<1.0"
1313
pydantic = ">=1.10.7, <3.0"
1414
backoff = ">=1.10.0"
1515
openai = { version = ">=0.27.8", optional = true }
1616
wrapt = "^1.14"
17-
langchain = { version = ">=0.0.309", optional = true }
1817
packaging = ">=23.2,<26.0"
1918
requests = "^2"
2019
opentelemetry-api = "^1.33.1"
@@ -31,7 +30,8 @@ pytest-httpserver = "^1.0.8"
3130
ruff = ">=0.1.8,<0.13.0"
3231
mypy = "^1.0.0"
3332
langchain-openai = ">=0.0.5,<0.4"
34-
langgraph = ">=0.2.62,<0.7.0"
33+
langchain = ">=1"
34+
langgraph = ">=1"
3535
autoevals = "^0.0.130"
3636

3737
[tool.poetry.group.docs.dependencies]

tests/test_datasets.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from concurrent.futures import ThreadPoolExecutor
44
from typing import Sequence
55

6-
from langchain import PromptTemplate
6+
from langchain_core.prompts import PromptTemplate
77
from langchain_openai import OpenAI
88

99
from langfuse import Langfuse, observe

tests/test_decorators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from typing import Optional
88

99
import pytest
10-
from langchain.prompts import ChatPromptTemplate
10+
from langchain_core.prompts import ChatPromptTemplate
1111
from langchain_openai import ChatOpenAI
1212
from opentelemetry import trace
1313

tests/test_json.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from unittest.mock import patch
88

99
import pytest
10-
from langchain.schema.messages import HumanMessage
10+
from langchain.messages import HumanMessage
1111
from pydantic import BaseModel
1212

1313
import langfuse

0 commit comments

Comments
 (0)