Skip to main content

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.

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)

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

pydantic_ai_langfuse-0.2.2.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydantic_ai_langfuse-0.2.2-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_ai_langfuse-0.2.2.tar.gz.

File metadata

  • Download URL: pydantic_ai_langfuse-0.2.2.tar.gz
  • Upload date:
  • Size: 3.9 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

Hashes for pydantic_ai_langfuse-0.2.2.tar.gz
Algorithm Hash digest
SHA256 20edfeb93ca2eff35f8336c16b8e23bc08154316a366784c0bf5a94687e933ac
MD5 91ebce0994a7875c02bc709edeb71231
BLAKE2b-256 4b3f4efdbe9fbd7a657863feaa3dd1e59ece15c701902b9708c6a15a921e8790

See more details on using hashes here.

File details

Details for the file pydantic_ai_langfuse-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: pydantic_ai_langfuse-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 4.5 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

Hashes for pydantic_ai_langfuse-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7e07a9c48de5d830cc12f3f41c39fe87de6eb80fbf051d7dc37f242e085c419c
MD5 2acfadf01e774a48e24e9544d8868968
BLAKE2b-256 c6ca68bdde592c63e9808690f7bc84ead1da55ad835d36a14e668cd876e94e49

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page