# PydanticAI

Source: https://docs.tradefloor.dev/pydantic-ai.html

> Run a PydanticAI agent inside a simulated tradefloor market: its decisions become orders, and the run is scored and can be replayed without calling the model.

`tradefloor.integrations.pydantic_ai` wraps a PydanticAI `Agent`. The adapter runs your agent inside a simulated market on your machine. At each decision tradefloor sends it a JSON observation, checks the decision it returns, places the orders in the simulation and scores the run. [LLM adapters](https://docs.tradefloor.dev/llm-adapters.html) covers [the observation payload](https://docs.tradefloor.dev/llm-adapters.html#the-observation-payload) and [decisions and model calls](https://docs.tradefloor.dev/llm-adapters.html#decisions-and-model-calls) for all the adapters.

## Install

```
pip install "tradefloor[pydantic-ai]"
```

## A first run

This runs with no API key. The adapter gets PydanticAI's offline `TestModel`.

`TestModel` answers every request with the arguments it's given. You can pass your own model through the adapter's `model=` argument in the same way.

```
import tradefloor as tf
from pydantic_ai import Agent
from pydantic_ai.models.test import TestModel
from tradefloor.integrations.pydantic_ai import PydanticAIAdapter

# a scripted model: no API key, the same answer at every decision
offline = TestModel(custom_output_args={
    "actions": [{"symbol": "AAA", "side": "BUY", "quantity": 200}],
    "rationale": "add to AAA"})

pm = Agent(instructions="You manage a small portfolio.")
adapter = PydanticAIAdapter(pm, model=offline)

market = tf.Universe.random(12, seed=4242)
card = tf.evaluate({"pm": adapter}, seed=4242, universe=market, days=5)["pm"]
print(card)
print(len(adapter.record), "decisions")
```

```
Scorecard('pm', pnl=1,621, return=+0.16%, trades=5, impact=+0.38bps, sharpe=n/a (short run), vol=0.7%, in_market=100%, exposure=0.03x)
5 decisions
```

For a live run, build the `Agent` with your provider's model, as PydanticAI's documentation describes, and leave out `model=`. The adapter passes your agent's tools, `deps` and instructions through unchanged and binds the decision schema as the output type for each run.

## Tested with

`pydantic-ai-slim` 2.54.0 and tradefloor 0.11.0.
