The Langfuse Python SDK covers the full platform: observability/tracing (OpenTelemetry-based, with OpenAI and LangChain integrations), datasets & experiments (offline evaluation and regression testing of prompt/model changes, including CI via GitHub Actions), LLM-as-a-judge and custom evaluations/scores, prompt management, and a full REST API client.
Important
The SDK was rewritten in v4 and released in March 2026. Refer to the v4 migration guide for instructions on updating your code.
pip install langfuse
# env: LANGFUSE_PUBLIC_KEY, LANGFUSE_SECRET_KEY, LANGFUSE_BASE_URLfromlangfuseimportget_clientlangfuse=get_client()
# Create a span using a context managerwithlangfuse.start_as_current_observation(as_type="span", name="process-request") asspan:
# Your processing logic herespan.update(output="Processing complete")
# Create a nested generation for an LLM callwithlangfuse.start_as_current_observation(as_type="generation", name="llm-response", model="gpt-5.6") asgeneration:
# Your LLM call logic heregeneration.update(output="Generated response")
# All spans are automatically closed when exiting their context blocks# Flush events in short-lived applicationslangfuse.flush()- SDK guide: https://langfuse.com/docs/observability/sdk/overview
- Full documentation: https://langfuse.com/docs
- Machine-readable docs index (for AI agents): https://langfuse.com/llms.txt
- API reference of this package: https://python.reference.langfuse.com
