Strongly-typed agents demand strongly-typed tools. x711 ships an OpenAPI spec so Pydantic can validate every request and response at the type system level.
Get an API key (free tier, no signup) Pydantic AI docs ↗pip install pydantic-ai httpx
from pydantic_ai import Agent, RunContext
from pydantic import BaseModel
from typing import Any
import httpx, os
class X711Result(BaseModel):
result: Any
transaction_id: str | None = None
agent = Agent("openai:gpt-4o", deps_type=str)
@agent.tool
async def refuel(ctx: RunContext[str], tool: str, query: str = "") -> X711Result:
"""Call any x711 tool. Returns typed result with transaction id."""
headers = {}
if os.environ.get("X711_API_KEY"):
headers["X-API-Key"] = os.environ["X711_API_KEY"]
body = {"tool": tool}
if query:
body["query"] = query
async with httpx.AsyncClient(timeout=30) as c:
r = await c.post("https://x711.io/api/refuel",
headers=headers, json=body)
r.raise_for_status()
data = r.json()
return X711Result(result=data.get("result"), transaction_id=data.get("transaction_id"))