Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion langfuse/_utils/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

# Attempt to import Serializable
try:
from langchain.load.serializable import Serializable
from langchain_core.load.serializable import Serializable
except ImportError:
# If Serializable is not available, set it to a placeholder type
class Serializable: # type: ignore
Expand Down
85 changes: 54 additions & 31 deletions langfuse/langchain/CallbackHandler.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import typing
from contextvars import Token
from typing import (
Any,
Dict,
List,
Literal,
Optional,
Sequence,
Set,
Type,
Union,
cast,
)
from uuid import UUID

import pydantic
from opentelemetry import context, trace
Expand All @@ -16,41 +28,52 @@
LangfuseSpan,
LangfuseTool,
)
from langfuse._utils import _get_timestamp
from langfuse.langchain.utils import _extract_model_name
from langfuse.logger import langfuse_logger

try:
import langchain # noqa

except ImportError as e:
langfuse_logger.error(
f"Could not import langchain. The langchain integration will not work. {e}"
)
import langchain

from typing import Any, Dict, List, Literal, Optional, Sequence, Set, Type, Union, cast
from uuid import UUID
if langchain.__version__.startswith("1"):
# Langchain v1
from langchain_core.agents import AgentAction, AgentFinish
from langchain_core.callbacks import (
BaseCallbackHandler as LangchainBaseCallbackHandler,
)
from langchain_core.documents import Document
from langchain_core.messages import (
AIMessage,
BaseMessage,
ChatMessage,
FunctionMessage,
HumanMessage,
SystemMessage,
ToolMessage,
)
from langchain_core.outputs import ChatGeneration, LLMResult

from langfuse._utils import _get_timestamp
from langfuse.langchain.utils import _extract_model_name
else:
# Langchain v0
from langchain.callbacks.base import ( # type: ignore
BaseCallbackHandler as LangchainBaseCallbackHandler,
)
from langchain.schema.agent import AgentAction, AgentFinish # type: ignore
from langchain.schema.document import Document # type: ignore
from langchain_core.messages import (
AIMessage,
BaseMessage,
ChatMessage,
FunctionMessage,
HumanMessage,
SystemMessage,
ToolMessage,
)
from langchain_core.outputs import (
ChatGeneration,
LLMResult,
)

try:
from langchain.callbacks.base import (
BaseCallbackHandler as LangchainBaseCallbackHandler,
)
from langchain.schema.agent import AgentAction, AgentFinish
from langchain.schema.document import Document
from langchain_core.messages import (
AIMessage,
BaseMessage,
ChatMessage,
FunctionMessage,
HumanMessage,
SystemMessage,
ToolMessage,
)
from langchain_core.outputs import (
ChatGeneration,
LLMResult,
)
except ImportError:
raise ModuleNotFoundError(
"Please install langchain to use the Langfuse langchain integration: 'pip install langchain'"
Expand Down Expand Up @@ -1011,7 +1034,7 @@ def _flatten_comprehension(matrix: Any) -> Any:
return [item for row in matrix for item in row]


def _parse_usage_model(usage: typing.Union[pydantic.BaseModel, dict]) -> Any:
def _parse_usage_model(usage: Union[pydantic.BaseModel, dict]) -> Any:
# maintains a list of key translations. For each key, the usage model is checked
# and a new object will be created with the new key if the key exists in the usage model
# All non matched keys will remain on the object.
Expand Down
2,574 changes: 1,240 additions & 1,334 deletions poetry.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ license = "MIT"
readme = "README.md"

[tool.poetry.dependencies]
python = ">=3.9,<4.0"
python = ">=3.10,<4.0"
httpx = ">=0.15.4,<1.0"
pydantic = ">=1.10.7, <3.0"
backoff = ">=1.10.0"
openai = { version = ">=0.27.8", optional = true }
wrapt = "^1.14"
langchain = { version = ">=0.0.309", optional = true }
packaging = ">=23.2,<26.0"
requests = "^2"
opentelemetry-api = "^1.33.1"
Expand All @@ -31,7 +30,8 @@ pytest-httpserver = "^1.0.8"
ruff = ">=0.1.8,<0.13.0"
mypy = "^1.0.0"
langchain-openai = ">=0.0.5,<0.4"
langgraph = ">=0.2.62,<0.7.0"
langchain = ">=1"
langgraph = ">=1"
autoevals = "^0.0.130"

[tool.poetry.group.docs.dependencies]
Expand Down
2 changes: 1 addition & 1 deletion tests/test_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from concurrent.futures import ThreadPoolExecutor
from typing import Sequence

from langchain import PromptTemplate
from langchain_core.prompts import PromptTemplate
from langchain_openai import OpenAI

from langfuse import Langfuse, observe
Expand Down
2 changes: 1 addition & 1 deletion tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from typing import Optional

import pytest
from langchain.prompts import ChatPromptTemplate
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from opentelemetry import trace

Expand Down
2 changes: 1 addition & 1 deletion tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from unittest.mock import patch

import pytest
from langchain.schema.messages import HumanMessage
from langchain.messages import HumanMessage
from pydantic import BaseModel

import langfuse
Expand Down
Loading
Loading