Langfuse integration for pydantic-ai
Project description
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.
import os
from langfuse.openai import AsyncOpenAI
from pydantic_ai import Agent
from pydantic_ai_langfuse import LangfuseOpenAIModel
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,
)
result = weather_agent.run_sync(
"What the weather like in Medolago BG?",
model_settings={
"name": "weather_query",
"metadata": {"location": "medolago", "query_type": "weather"},
"session_id": "testoneditest",
"user_id": "user123",
"tags": ["weather", "italy", "query"],
},
)
print("Response:", result.data)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pydantic_ai_langfuse-0.1.1.tar.gz.
File metadata
- Download URL: pydantic_ai_langfuse-0.1.1.tar.gz
- Upload date:
- Size: 3.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.0 CPython/3.10.5 Linux/6.8.0-52-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f3f582e5f7657281c88c77ad10af8ab5be9330aa15165a4295bbe252e4804bbf
|
|
| MD5 |
91c0145dd67f75926e3fb4d5257bf257
|
|
| BLAKE2b-256 |
6715e56604cb2c6a417b5800871c228cf042527355972fb98e8c8b94395aeb6d
|
File details
Details for the file pydantic_ai_langfuse-0.1.1-py3-none-any.whl.
File metadata
- Download URL: pydantic_ai_langfuse-0.1.1-py3-none-any.whl
- Upload date:
- Size: 4.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.0 CPython/3.10.5 Linux/6.8.0-52-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8273a33fe840606fb7d8b50cf56c4cd0e6f5dd2be2966f3fe8f75c942919293b
|
|
| MD5 |
f7bab9939a42b9020a2e239a20c6fe0a
|
|
| BLAKE2b-256 |
ae2b64662edf6840904817017a7d7962bc87f0240efb0263cb5b9d7f4590c3f8
|