Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 3 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.12"
python-version: "3.13"
- name: Install poetry
uses: abatilo/actions-poetry@v2
- name: Setup a local virtual environment
Expand Down Expand Up @@ -69,9 +69,10 @@ jobs:
fail-fast: false
matrix:
python-version:
- "3.9"
- "3.10"
- "3.11"
- "3.12"
- "3.13"

name: Test on Python version ${{ matrix.python-version }}
steps:
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ repos:
types_or: [python, pyi, jupyter]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.8.0
rev: v1.18.2
hooks:
- id: mypy
additional_dependencies:
Expand Down
4 changes: 1 addition & 3 deletions langfuse/_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,7 @@ def __init__(
additional_headers: Optional[Dict[str, str]] = None,
tracer_provider: Optional[TracerProvider] = None,
):
self._host = host or cast(
str, os.environ.get(LANGFUSE_HOST, "https://cloud.langfuse.com")
)
self._host = host or os.environ.get(LANGFUSE_HOST, "https://cloud.langfuse.com")
self._environment = environment or cast(
str, os.environ.get(LANGFUSE_TRACING_ENVIRONMENT)
)
Expand Down
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
Loading