



# 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
```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)
```
If using agent factory function, use `@use_observed_agent` directly
```python
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
```
Raw data
{
"_id": null,
"home_page": null,
"name": "langfuse-pydantic-ai",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": null,
"keywords": "langfuse, pydantic-ai",
"author": null,
"author_email": "wh1isper <jizhongsheng957@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/84/07/5d6aadb114f6eb45348917a71b9fb81e15da91cf92f16de761e1256e3466/langfuse_pydantic_ai-0.1.0.tar.gz",
"platform": null,
"description": "\n\n\n\n\n# langfuse_pydantic_ai\n\n> This is a third-party package, not officially maintained by Langfuse. If Langfuse requires for this package, feel free to contact me.\n\nA simple wrapper, send trace to langfuse when using pydantic-ai\n\n## Install\n\n`pip install langfuse-pydantic-ai`\n\n## Usage\n\nTL;DR\n\n```python\nfrom langfuse_pydantic_ai import observed_agent\n\nagent = observed_agent(agent)\n```\n\nFull example:\n\n```python\nimport asyncio\n\nfrom pydantic_ai.agent import Agent\nfrom langfuse.decorators import observe\nfrom langfuse_pydantic_ai import observed_agent\n\n@observe # Add this decorator to span a trace\nasync def main():\n agent = Agent(\n \"google-gla:gemini-1.5-flash\",\n # Register a static system prompt using a keyword argument to the agent.\n # For more complex dynamically-generated system prompts, see the example below.\n system_prompt=\"Be concise, reply with one sentence.\",\n )\n agent = observed_agent(agent)\n result = await agent.run('Where does \"hello world\" come from?')\n print(result.data)\n\n\nif __name__ == \"__main__\":\n asyncio.run(main())\n```\n\nIf using custom model, use `observed_model` instead\n\n```python\nfrom pydantic_ai.agent import Agent\nfrom langfuse_pydantic_ai import observed_model\n\nmodel = observed_model(model)\nagent = Agent(model=model)\n```\n\nIf using agent factory function, use `@use_observed_agent` directly\n\n```python\nfrom pydantic_ai.agent import Agent\nfrom langfuse_pydantic_ai import use_observed_agent\n\n@use_observed_agent\ndef init_agent() -> Agent:\n return Agent(\n \"google-gla:gemini-1.5-flash\",\n # Register a static system prompt using a keyword argument to the agent.\n # For more complex dynamically-generated system prompts, see the example below.\n system_prompt=\"Be concise, reply with one sentence.\",\n )\n```\n\nConfiguration via environment variables:\n\n```bash\nLANGFUSE_HOST=<langfuse_host>\nLANGFUSE_PUBLIC_KEY=<langfuse_public_key>\nLANGFUSE_SECRET_KEY=<langfuse_secret_key>\n```\n\n## Develop\n\nInstall pre-commit before commit\n\n```\npip install pre-commit\npre-commit install\n```\n\nInstall package locally\n\n```\npip install -e .[test]\n```\n\nRun unit-test before PR\n\n```\npytest -v\n```\n",
"bugtrack_url": null,
"license": "BSD 3-Clause License",
"summary": "langfuse-pydantic-ai",
"version": "0.1.0",
"project_urls": {
"Source": "https://github.com/wh1isper/langfuse_pydantic_ai"
},
"split_keywords": [
"langfuse",
" pydantic-ai"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "88a46cb053eb8e8c3486f330b876522e5db82e438374c2d33c2035c94911ace9",
"md5": "8fee6c6bdbd8f5a0bc75d0e7a1bd4cf1",
"sha256": "122410f95e19a40f5247383537c4ac6814bf7a7fd3368efef9cbcadb5c244d21"
},
"downloads": -1,
"filename": "langfuse_pydantic_ai-0.1.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "8fee6c6bdbd8f5a0bc75d0e7a1bd4cf1",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 4422,
"upload_time": "2025-02-10T08:34:39",
"upload_time_iso_8601": "2025-02-10T08:34:39.979987Z",
"url": "https://files.pythonhosted.org/packages/88/a4/6cb053eb8e8c3486f330b876522e5db82e438374c2d33c2035c94911ace9/langfuse_pydantic_ai-0.1.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "84075d6aadb114f6eb45348917a71b9fb81e15da91cf92f16de761e1256e3466",
"md5": "23181ade014e3debf9475b7f2d35f671",
"sha256": "efe1702c5312bb0d9acfa72f6afa74a8ca74a8e929c92254de4c7212e77622cd"
},
"downloads": -1,
"filename": "langfuse_pydantic_ai-0.1.0.tar.gz",
"has_sig": false,
"md5_digest": "23181ade014e3debf9475b7f2d35f671",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 14513,
"upload_time": "2025-02-10T08:34:41",
"upload_time_iso_8601": "2025-02-10T08:34:41.645852Z",
"url": "https://files.pythonhosted.org/packages/84/07/5d6aadb114f6eb45348917a71b9fb81e15da91cf92f16de761e1256e3466/langfuse_pydantic_ai-0.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-02-10 08:34:41",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "wh1isper",
"github_project": "langfuse_pydantic_ai",
"github_not_found": true,
"lcname": "langfuse-pydantic-ai"
}