Skip to the page
INTEGRATIONS/PYDANTICAI

PydanticAI

tradefloor.integrations.pydantic_ai wraps a PydanticAI Agent. The adapter runs your agent inside a simulated market on your machine. tradefloor sends it a JSON observation each decision, checks the decision it returns, places its orders in the simulation and scores the run. LLM adapters covers what every adapter shares: the observation payload and decisions and model calls.

Install

pip install "tradefloor[pydantic-ai]"

A first run

The block runs with no API key, because it hands the adapter PydanticAI's offline TestModel.

TestModel answers every request with the arguments it is given. Pass your own model through the adapter's model= argument 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=2,055, return=+0.21%, trades=5, impact=+0.18bps, 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=. Your agent's tools, deps and instructions are passed through unchanged, and the adapter binds the decision schema as the output type for each run.

Tested with

pydantic-ai-slim 2.54.0 and tradefloor 0.9.1.