Metadata-Version: 2.4
Name: langfuse-pydantic-ai
Version: 0.0.2
Summary: langfuse-pydantic-ai
Project-URL: Source, https://github.com/wh1isper/langfuse_pydantic_ai
Author-email: wh1isper <jizhongsheng957@gmail.com>
License: BSD 3-Clause License
License-File: LICENSE
Keywords: langfuse,pydantic-ai
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Requires-Python: >=3.9
Requires-Dist: langfuse
Requires-Dist: pydantic-ai>=0.0.23
Provides-Extra: docs
Requires-Dist: autodoc-pydantic; extra == 'docs'
Requires-Dist: pydata-sphinx-theme; extra == 'docs'
Requires-Dist: sphinx; extra == 'docs'
Requires-Dist: sphinx-click; extra == 'docs'
Provides-Extra: test
Requires-Dist: dirty-equals; extra == 'test'
Requires-Dist: pytest; extra == 'test'
Requires-Dist: pytest-asyncio; extra == 'test'
Requires-Dist: pytest-cov; extra == 'test'
Description-Content-Type: text/markdown

![](https://img.shields.io/github/license/wh1isper/langfuse-pydantic-ai)
![](https://img.shields.io/github/v/release/wh1isper/langfuse-pydantic-ai)
![](https://img.shields.io/pypi/dm/langfuse-pydantic-ai)
![](https://img.shields.io/github/last-commit/wh1isper/langfuse-pydantic-ai)
![](https://img.shields.io/pypi/pyversions/langfuse-pydantic-ai)

# 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

```python
from langfuse_pydantic_ai import observed_agent

agent = observed_agent(agent)
```

Full example:

```python
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

```python
from pydantic_ai.agent import Agent
from langfuse_pydantic_ai import observed_model

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

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
```
