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.

⚠️ This package is experimental and not fully tested. Use at your own risk.

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.data)


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:

```bash
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.0.3.tar.gz (13.3 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.0.3-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for langfuse_pydantic_ai-0.0.3.tar.gz
Algorithm Hash digest
SHA256 c70717a5fc77e1e28e4491078645309665ff5bcdca7b56ac149169f6a4f4f258
MD5 45b9e6c83018ab1c4fd2826eb0e9097d
BLAKE2b-256 3bcab2620ec33f3eb1eeda7bcba03e36adeb3b524afa4c9561b0fa513ef51588

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for langfuse_pydantic_ai-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 ce44c697f87b00513a138b5ebb82867ac65711a812e178e9144128033ea3a5cf
MD5 502691c9ccc9579265f1ee35b16d165e
BLAKE2b-256 9640e31fbcf0185370c762ec0a636680a4dd974557d652b15712b6e24166bb44

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