This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
This is the Langfuse Python SDK, a client library for accessing the Langfuse observability platform. The SDK provides integration with OpenTelemetry (OTel) for tracing, automatic instrumentation for popular LLM frameworks (OpenAI, Langchain, etc.), and direct API access to Langfuse's features.
# Install the project and development dependencies
uv sync
# Setup pre-commit hooks
uv run pre-commit install# Run all tests with verbose output
uv run --env-file .env pytest -s -v --log-cli-level=INFO
# Run a specific test
uv run --env-file .env pytest -s -v --log-cli-level=INFO tests/test_core_sdk.py::test_flush
# Run tests in parallel (faster)
uv run --env-file .env pytest -s -v --log-cli-level=INFO -n auto# Format code with Ruff
uv run ruff format .
# Run linting (development config)
uv run ruff check .
# Run type checking
uv run mypy .
# Run pre-commit hooks manually
uv run pre-commit run --all-files# Build the package locally (for testing)
uv build --no-sources
# Generate documentation
uv run --group docs pdoc -o docs/ --docformat google --logo "https://langfuse.com/langfuse_logo.svg" langfuseReleases are automated via GitHub Actions. To release:
- Go to Actions > "Release Python SDK" workflow
- Click "Run workflow"
- Select version bump type (patch/minor/major/prepatch/preminor/premajor)
- For prereleases, select the type (alpha/beta/rc)
The workflow handles versioning, building, PyPI publishing (via OIDC), and GitHub release creation.
-
langfuse/_client/: Main SDK implementation built on OpenTelemetryclient.py: Core Langfuse client with OTel integrationspan.py: LangfuseSpan, LangfuseGeneration, LangfuseEvent classesobserve.py: Decorator for automatic instrumentationdatasets.py: Dataset management functionality
-
langfuse/api/: Auto-generated Fern API client- Contains all API resources and types
- Generated from OpenAPI spec - do not manually edit these files
-
langfuse/_task_manager/: Background processing- Media upload handling and queue management
- Score ingestion consumer
-
Integration modules:
langfuse/openai.py: OpenAI instrumentationlangfuse/langchain/: Langchain integration via CallbackHandler
The SDK is built on OpenTelemetry for observability, using:
- Spans for tracing LLM operations
- Attributes for metadata (see
LangfuseOtelSpanAttributes) - Resource management for efficient batching and flushing
The client follows an async-first design with automatic batching of events and background flushing to the Langfuse API.
Environment variables (defined in _client/environment_variables.py):
LANGFUSE_PUBLIC_KEY/LANGFUSE_SECRET_KEY: API credentialsLANGFUSE_HOST: API endpoint (defaults to https://cloud.langfuse.com)LANGFUSE_DEBUG: Enable debug loggingLANGFUSE_TRACING_ENABLED: Enable/disable tracingLANGFUSE_SAMPLE_RATE: Sampling rate for traces
- Create
.envfile based on.env.templatefor integration tests - E2E tests with external APIs (OpenAI, SERP) are typically skipped in CI
- Remove
@pytest.mark.skipdecorators in test files to run external API tests - Tests use
respxfor HTTP mocking andpytest-httpserverfor test servers
pyproject.toml: uv project metadata, dependencies, and tool settingsuv.lock: Locked dependency graph for local development and CI
The langfuse/api/ directory is auto-generated from the Langfuse OpenAPI specification using Fern. To update:
- Generate new SDK in main Langfuse repo
- Copy generated files from
generated/pythontolangfuse/api/ - Run
uv run ruff format .to format the generated code
- Don't remove functionality from existing unit tests just to make tests pass. Only change the test, if underlying code changes warrant a test change.
- Exception must not use an f-string literal, assign to variable first