Metadata-Version: 2.5
Name: runtype-pydantic-ai
Version: 0.2.1
Summary: Send Pydantic AI agent runs to Runtype at full fidelity: one call, composes with Logfire and any OpenTelemetry setup.
Project-URL: Homepage, https://runtype.com
Project-URL: Documentation, https://docs.runtype.com
Author-email: Runtype Labs <dev@runtype.com>
License-Expression: MIT
Keywords: gen-ai,observability,opentelemetry,otel,pydantic-ai,runtype
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: System :: Monitoring
Classifier: Typing :: Typed
Requires-Python: >=3.10
Requires-Dist: opentelemetry-exporter-otlp-proto-http>=1.30
Requires-Dist: opentelemetry-sdk>=1.30
Requires-Dist: pydantic-ai-slim<3,>=1.90
Provides-Extra: dev
Requires-Dist: logfire>=4.40; extra == 'dev'
Requires-Dist: pytest-asyncio>=0.23; extra == 'dev'
Requires-Dist: pytest>=8; extra == 'dev'
Description-Content-Type: text/markdown

# runtype-pydantic-ai

Send [Pydantic AI](https://ai.pydantic.dev) agent runs to [Runtype](https://runtype.com). Each run
appears as a first-class execution: transcript, system prompt, model calls, tool calls with arguments
and results, token usage, cache usage, cost, loop iterations, stop reason and sub-agents.

- Works with `pydantic-ai` 1.90+ and 2.x from one entry point.
- Composes with Logfire and any OpenTelemetry setup. Your other exporters keep receiving the original
  spans, and only agent-run spans reach Runtype.
- Sends everything Runtype can show by default. Every content kind has its own off switch, and
  `content=False` sends shape and cost only. Exception messages and stack traces never leave the
  process; a failed run reports the exception class name.

## Install

```bash
pip install runtype-pydantic-ai
```

## Quick start

Set `RUNTYPE_API_KEY` to a key with the `TELEMETRY:WRITE` scope and `RUNTYPE_AGENT_ID` to the Runtype
agent that owns the runs. Then add one call at startup:

```python
import runtype_pydantic_ai

runtype_pydantic_ai.instrument()
```

Every `Agent` in the process is now traced. Spans are exported in batches, so a run reaches Runtype a
few seconds after it ends. In a short-lived script or a test that checks the export, keep the handle
and flush before exit or before asserting:

```python
handle = runtype_pydantic_ai.instrument()
...
handle.force_flush()
```

## With Logfire

Call `instrument()` after `logfire.configure()` and it joins Logfire's provider. Alternatively, pass
the processor in:

```python
import logfire
import runtype_pydantic_ai

logfire.configure(additional_span_processors=[runtype_pydantic_ai.span_processor()])
logfire.instrument_pydantic_ai(include_content=True, version=5)
```

Any instrumentation format from 2 up works, so keep the one your Logfire queries read. Format 2 uses
the older span names (`agent run`, `running tool`), and Runtype receives the same attributes from it
as from format 5. `instrument()` itself sets format 5.

## With another OpenTelemetry provider

`instrument()` leaves a global non-Logfire provider alone and traces through a private one, because
prompt content would otherwise reach every exporter on it. To share a provider, pass it:

```python
runtype_pydantic_ai.instrument(tracer_provider=provider)
```

Its other exporters then receive raw content, and `redact` does not apply to them. Use `content=False`
to keep content off every exporter.

If `Agent.instrument_all()` already ran, `instrument()` keeps those settings and joins the provider
they use, and raises if it cannot reach that provider. An agent with its own instrumentation, such as
`Agent(instrument=True)`, traces to its own provider, so pass that provider too.

## Several agents in one process

Map Pydantic AI agent names to Runtype agents, or pin a block of code to one:

```python
runtype_pydantic_ai.instrument(agent_ids={"chat": "agent_a", "run": "agent_b"})

with runtype_pydantic_ai.runtype_agent("agent_c"):
    await agent.run("...")
```

A sub-agent called from a tool stays a branch of the outer run, under the outer run's Runtype agent.

Every top-level run is its own Runtype execution, even when several share a web request's span. A run
under such a span gets a new trace id in Runtype, so it will not match the id in your other backend.

## One agent only

To trace a single agent rather than all of them:

```python
handle = runtype_pydantic_ai.instrument()
Agent.instrument_all(False)
agent = Agent("openai:gpt-5-mini", capabilities=[runtype_pydantic_ai.capability(handle)])
```

Early 1.x releases have no public `Instrumentation` capability, and `capability()` says so. Use
`Agent(instrument=handle.settings)` there.

## Content

By default Runtype receives prompts, instructions, model output and tool arguments and results as
written. Any personal data your prompts carry, such as a user's record placed in the first message,
reaches Runtype too. Turn it off or scrub it here:

```python
from runtype_pydantic_ai import ContentOptions

runtype_pydantic_ai.instrument(content=False)
runtype_pydantic_ai.instrument(content=ContentOptions(tool_results=False, max_chars=16_384))
runtype_pydantic_ai.instrument(content=ContentOptions(redact=lambda value, attribute: scrub(value)))
```

Each switch also applies inside message lists. `tool_arguments=False` and `tool_results=False` remove
those payloads from tool call parts, `system_instructions=False` removes system messages, and
`output_messages=False` removes earlier assistant turns from `gen_ai.input.messages`.

`redact` receives the parsed value and the attribute name. Returning `None`, or raising, drops that one
attribute and keeps the span.

Values are capped to fit ingest's limits. Oversized message lists stay valid JSON: large parts become a
marker, then the oldest turns are dropped and the newest kept.

## Custom attributes

Attributes are allow-listed. To send your own, name their prefixes:

```python
runtype_pydantic_ai.instrument(passthrough_attribute_prefixes=["myapp."])
```

## Options

| Environment variable | Purpose                                                                  |
| -------------------- | ------------------------------------------------------------------------ |
| `RUNTYPE_API_KEY`    | Runtype key with the `TELEMETRY:WRITE` scope. Required.                  |
| `RUNTYPE_AGENT_ID`   | Default Runtype agent for runs.                                          |
| `RUNTYPE_OTLP_URL`   | Full trace URL. Defaults to `https://api.runtype.com/v1/otel/v1/traces`. |
