Skip to main content

langfuse-pydantic-ai

Project description

langfuse_pydantic_ai

This is a third-party package, not officially maintained by Langfuse. If Langfuse requires for this package, feel free to contact me.

A simple wrapper, send trace to langfuse when using pydantic-ai

Install

pip install langfuse-pydantic-ai

Usage

TL;DR

from langfuse_pydantic_ai import observed_agent

agent = observed_agent(agent)

Full example:

import asyncio

from pydantic_ai.agent import Agent
from langfuse.decorators import observe
from langfuse_pydantic_ai import observed_agent

@observe # Add this decorator to span a trace
async def main():
    agent = Agent(
        "google-gla:gemini-1.5-flash",
        # Register a static system prompt using a keyword argument to the agent.
        # For more complex dynamically-generated system prompts, see the example below.
        system_prompt="Be concise, reply with one sentence.",
    )
    agent = observed_agent(agent)
    result = await agent.run('Where does "hello world" come from?')
    print(result.output)


if __name__ == "__main__":
    asyncio.run(main())

If using custom model, use observed_model instead

from pydantic_ai.agent import Agent
from langfuse_pydantic_ai import observed_model

model = observed_model(model)
agent = Agent(model=model)

If using agent factory function, use @use_observed_agent directly

from pydantic_ai.agent import Agent
from langfuse_pydantic_ai import use_observed_agent

@use_observed_agent
def init_agent() -> Agent:
    return Agent(
        "google-gla:gemini-1.5-flash",
        # Register a static system prompt using a keyword argument to the agent.
        # For more complex dynamically-generated system prompts, see the example below.
        system_prompt="Be concise, reply with one sentence.",
    )

Configuration via environment variables:

LANGFUSE_HOST=<langfuse_host>
LANGFUSE_PUBLIC_KEY=<langfuse_public_key>
LANGFUSE_SECRET_KEY=<langfuse_secret_key>

Develop

Install pre-commit before commit

pip install pre-commit
pre-commit install

Install package locally

pip install -e .[test]

Run unit-test before PR

pytest -v

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

langfuse_pydantic_ai-0.2.1.tar.gz (14.6 kB view details)

Uploaded Source

Built Distribution

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

langfuse_pydantic_ai-0.2.1-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file langfuse_pydantic_ai-0.2.1.tar.gz.

File metadata

  • Download URL: langfuse_pydantic_ai-0.2.1.tar.gz
  • Upload date:
  • Size: 14.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for langfuse_pydantic_ai-0.2.1.tar.gz
Algorithm Hash digest
SHA256 ba7374a42f5864a0d3f893793af8842836db16d7db3fbe5f412aac284f9c27fe
MD5 e85d64c678a2c52ba1d863dd825bc6eb
BLAKE2b-256 1f9e8d530251d719dc69715001a9bda966b5476909fc5abaa05c76e8c3c94f85

See more details on using hashes here.

File details

Details for the file langfuse_pydantic_ai-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for langfuse_pydantic_ai-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 80d7a3ccc8f99405a4f979332700d4163b124540c0f557b55f7dc65ee466cc09
MD5 5b0dc05fa1a86df5596bdb90dc789105
BLAKE2b-256 91850082faeb5292e6dadb52b530989b87bd28831abc6eca0b42991dcfd30236

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