You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
team that standardises how frontend applications communicate with AI agents, with support for streaming, frontend tools, shared state, and custom events.
6
6
7
-
Any Pydantic AI agent can be exposed as an AG-UI server using the [`Agent.to_ag_ui()`][pydantic_ai.Agent.to_ag_ui] convenience method.
8
-
9
7
!!! note
10
8
The AG-UI integration was originally built by the team at [Rocket Science](https://www.rocketscience.gg/) and contributed in collaboration with the Pydantic AI and CopilotKit teams. Thanks Rocket Science!
11
9
12
10
## Installation
13
11
14
12
The only dependencies are:
15
13
16
-
-[ag-ui-protocol](https://docs.ag-ui.com/introduction): to provide the AG-UI types and encoder
17
-
-[starlette](https://www.starlette.io): to expose the AG-UI server as an [ASGI application](https://asgi.readthedocs.io/en/latest/)
14
+
-[ag-ui-protocol](https://docs.ag-ui.com/introduction): to provide the AG-UI types and encoder.
15
+
-[starlette](https://www.starlette.io): to handle [ASGI](https://asgi.readthedocs.io/en/latest/) requests from a framework like FastAPI.
18
16
19
17
You can install Pydantic AI with the `ag-ui` extra to ensure you have all the
20
18
required AG-UI dependencies:
@@ -31,9 +29,95 @@ To run the examples you'll also need:
31
29
pip/uv-add uvicorn
32
30
```
33
31
34
-
## Quick start
32
+
## Usage
33
+
34
+
There are three ways to run a Pydantic AI agent based on AG-UI run input with streamed AG-UI events as output, from most to least flexible. If you're using a Starlette-based web framework like FastAPI, you'll typically want to use the second method.
35
+
36
+
1.[`run_ag_ui()`][pydantic_ai.ag_ui.run_ag_ui] takes an agent and an AG-UI [`RunAgentInput`](https://docs.ag-ui.com/sdk/python/core/types#runagentinput) object, and returns a stream of AG-UI events encoded as strings. It also takes optional [`Agent.iter()`][pydantic_ai.Agent.iter] arguments including `deps`. Use this if you're using a web framework not based on Starlette (e.g. Django or Flask) or want to modify the input or output some way.
37
+
2.[`handle_ag_ui_request()`][pydantic_ai.ag_ui.handle_ag_ui_request] takes an agent and a Starlette request (e.g. from FastAPI) coming from an AG-UI frontend, and returns a streaming Starlette response of AG-UI events that you can return directly from your endpoint. It also takes optional [`Agent.iter()`][pydantic_ai.Agent.iter] arguments including `deps`, that you can vary for each request (e.g. based on the authenticated user).
38
+
3.[`Agent.to_ag_ui()`][pydantic_ai.Agent.to_ag_ui] returns an ASGI application that handles every AG-UI request by running the agent. It also takes optional [`Agent.iter()`][pydantic_ai.Agent.iter] arguments including `deps`, but these will be the same for each request, with the exception of the AG-UI state that's injected as described under [state management](#state-management). This ASGI app can be [mounted](https://fastapi.tiangolo.com/advanced/sub-applications/) at a given path in an existing FastAPI app.
39
+
40
+
### Handle run input and output directly
41
+
42
+
This example uses [`run_ag_ui()`][pydantic_ai.ag_ui.run_ag_ui] and performs its own request parsing and response generation.
43
+
This can be modified to work with any web framework.
44
+
45
+
```py {title="run_ag_ui.py"}
46
+
from ag_ui.core import RunAgentInput
47
+
from fastapi import FastAPI
48
+
from http import HTTPStatus
49
+
from fastapi.requests import Request
50
+
from fastapi.responses import Response, StreamingResponse
51
+
from pydantic import ValidationError
52
+
import json
53
+
54
+
from pydantic_ai import Agent
55
+
from pydantic_ai.ag_ui import run_ag_ui, SSE_CONTENT_TYPE
Since `app` is an ASGI application, it can be used with any ASGI server:
81
+
82
+
```shell
83
+
uvicorn run_ag_ui:app
84
+
```
85
+
86
+
This will expose the agent as an AG-UI server, and your frontend can start sending requests to it.
87
+
88
+
### Handle a Starlette request
89
+
90
+
This example uses [`handle_ag_ui_request()`][pydantic_ai.ag_ui.run_ag_ui] to directly handle a FastAPI request and return a response. Something analogous to this will work with any Starlette-based web framework.
91
+
92
+
```py {title="handle_ag_ui_request.py"}
93
+
from fastapi import FastAPI
94
+
from starlette.requests import Request
95
+
from starlette.responses import Response
96
+
97
+
from pydantic_ai import Agent
98
+
from pydantic_ai.ag_ui import handle_ag_ui_request
This will expose the agent as an AG-UI server, and your frontend can start sending requests to it.
52
136
53
-
The `to_ag_ui()` method accepts the same arguments as the [`Agent.iter()`][pydantic_ai.agent.Agent.iter] method as well as arguments that let you configure the [Starlette](https://www.starlette.io)-based ASGI app.
54
-
55
137
## Design
56
138
57
139
The Pydantic AI AG-UI integration supports all features of the spec:
@@ -61,14 +143,11 @@ The Pydantic AI AG-UI integration supports all features of the spec:
that describes the details of the requested agent run including message history, state, and available tools.
69
149
70
-
Events from the agent, including tool calls, are converted to AG-UI events and
71
-
streamed back to the caller as Server-Sent Events (SSE).
150
+
These are converted to Pydantic AI types and passed to the agent's run method. Events from the agent, including tool calls, are converted to AG-UI events and streamed back to the caller as Server-Sent Events (SSE).
72
151
73
152
A user request may require multiple round trips between client UI and Pydantic AI
74
153
server, depending on the tools and events needed.
@@ -77,7 +156,7 @@ server, depending on the tools and events needed.
77
156
78
157
### State management
79
158
80
-
The adapter provides full support for
159
+
The integration provides full support for
81
160
[AG-UI state management](https://docs.ag-ui.com/concepts/state), which enables
82
161
real-time synchronization between agents and frontend applications.
0 commit comments