
This app sets up a direct connection between Pydantic AI and CopilotKit vNext to create a playground for chatting with an AI agent.
CopilotKit connects directly to the endpoint created by the Pydantic AI agent. The React frontend then displays the chat and shows streaming updates of what the agent is doing in real-time.
-
Backend (Python): Creates a Pydantic AI agent and sets up an endpoint using
handle_ag_ui_request()
. -
Frontend (React): Uses CopilotKit's chat component to communicate directly with the Pydantic AI agent.
- uv for Python package management
- pnpm for Node.js package management
- An OpenAI API key in your
.env
file or elsewhere in your environment
Build the React app once and let Python serve both the static bundle and the agent on port 8000.
cd frontend
pnpm install
pnpm run build:static
cd ..
cat > .env <<'EOF'
OPENAI_API_KEY=your-openai-api-key
EOF
uv venv
uv pip install -r requirements.txt
uv run python server.py
Visit http://localhost:8000. The static frontend is served from /
, and /api
streams agent responses.
Run the backend and frontend separately to get hot reloads during development.
cat > .env <<'EOF'
OPENAI_API_KEY=your-openai-api-key
EOF
uv venv
uv pip install -r requirements.txt
uv run python server.py
cd frontend
pnpm install
pnpm dev
Open http://localhost:3000 and start chatting. The dev server reflects code changes immediately.
- Tool Calls: The agent has a
get_weather
tool. When you ask about weather, you'll see the tool being called in the UI. - Real-time Streaming: Responses stream in as they're generated.
- Tool Visualization: The
WildCardToolCallRender
component shows you exactly what tools are being called with what arguments and results.
How the frontend is set up (see frontend/app/page.tsx
)
- Import
PydanticAIAgent
from@ag-ui/pydantic-ai
- Creates the outer
CopilotKitProvider
component and configures thedefault
agent to use the Pydantic AI agent. - Adds the
WildCardToolCallRender
component to therenderToolCalls
to surface tool calls in the UI. - Adds the
CopilotChat
component inside a full-screen div.
agent.py
- The Pydantic AI agent definition.server.py
- Starlette app that serves/api
and the static frontend.frontend/app/page.tsx
- Sets up CopilotKit and connects to the backend.
- Add more tools: Just add more
@agent.tool
decorated functions inagent.py
- Add custom tool renderers: Define them with
defineToolCallRender
and add to therenderToolCalls
prop infrontend/app/page.tsx
- Customize the UI ...