pydantic-ai-langfuse
pydantic-ai-langfuse extends pydantic-ai-slim to integrate Langfuse tracking into your OpenAI model interactions. By incorporating our Langfuse OpenAI model settings, you can easily label, track, and filter your generations using enriched metadata.
Installation
Ensure you have the required dependencies installed:
pip install pydantic-ai-langfuse
Environment Setup
Before running your model, you need to set up the following environment variables:
OPENAI_API_KEY: Your OpenAI API key.LANGFUSE_PUBLIC_KEY: Your Langfuse public key.LANGFUSE_SECRET_KEY: Your Langfuse secret key.LANGFUSE_HOST: Your Langfuse host endpoint.
Quickstart
Below is a complete Python example showing how to set up and use the LangfuseOpenAIModel with extra model settings. This example uses the synchronous run_sync method with basic error handling and retries built in.
if __name__ == "__main__":
import os
from langfuse.openai import AsyncOpenAI
from pydantic_ai import Agent
from pydantic_ai_langfuse import LangfuseOpenAIModel, LangfuseOpenAIModelSettings
for var in [
"OPENAI_API_KEY",
"LANGFUSE_PUBLIC_KEY",
"LANGFUSE_SECRET_KEY",
"LANGFUSE_HOST",
]:
if var not in os.environ:
raise OSError(f"Missing env variable: {var}")
weather_agent = Agent(
model=LangfuseOpenAIModel("gpt-4o", openai_client=AsyncOpenAI()),
system_prompt="Be concise: reply with one sentence.",
retries=2,
)
model_settings: LangfuseOpenAIModelSettings = {
"name": "weather_query",
"metadata": {"location": "medolago", "query_type": "weather"},
"session_id": "testoneditest",
"user_id": "user123",
"tags": ["weather", "italy", "query"],
}
result = weather_agent.run_sync(
user_prompt="What the weather like in Medolago BG?",
model_settings=model_settings,
)
print("Response:", result.data)
Release files for pydantic-ai-langfuse 0.2.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pydantic_ai_langfuse-0.2.3.tar.gz | 3.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pydantic_ai_langfuse-0.2.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 8.1 kB
Release files / pydantic_ai_langfuse-0.2.3.tar.gz
| Download URL | pydantic_ai_langfuse-0.2.3.tar.gz |
|---|---|
| Size | 3.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e2462dfcf4fd29880c7e572db290a4ab3a882936693577df28b0eb9de2f0fdcf
|
|
BLAKE2b-256 checksum How to use checksums |
025d2c1c9467c7b95a23d29122237cea634f1c9d6abebf2878caa8f1b9d8e527
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.0 CPython/3.10.5 Linux/6.8.0-52-generic
|
Release files / pydantic_ai_langfuse-0.2.3-py3-none-any.whl
| Download URL | pydantic_ai_langfuse-0.2.3-py3-none-any.whl |
|---|---|
| Size | 4.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ccd24632337e85b426fab7cd817243b295e45cd3dca7bf1bf35bd07a169e66da
|
|
BLAKE2b-256 checksum How to use checksums |
caf49fd67f43f47568adff9bcba27e4582c6f84a0e2589cbee15e08d2e9997db
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.0 CPython/3.10.5 Linux/6.8.0-52-generic
|