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
.envfile 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.pyVisit 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.pycd frontend
pnpm install
pnpm devOpen http://localhost:3000 and start chatting. The dev server reflects code changes immediately.
- Tool Calls: The agent has a
get_weathertool. 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
WildCardToolCallRendercomponent 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
PydanticAIAgentfrom@ag-ui/pydantic-ai - Creates the outer
CopilotKitProvidercomponent and configures thedefaultagent to use the Pydantic AI agent. - Adds the
WildCardToolCallRendercomponent to therenderToolCallsto surface tool calls in the UI. - Adds the
CopilotChatcomponent inside a full-screen div.
agent.py- The Pydantic AI agent definition.server.py- Starlette app that serves/apiand the static frontend.frontend/app/page.tsx- Sets up CopilotKit and connects to the backend.
- Add more tools: Just add more
@agent.tooldecorated functions inagent.py - Add custom tool renderers: Define them with
defineToolCallRenderand add to therenderToolCallsprop infrontend/app/page.tsx - Customize the UI ...