@@ -8,23 +8,25 @@ an open protocol. Think of it as a universal translator for AI-driven systems
8
8
no matter what language an agent speaks: AG-UI ensures fluent communication.
9
9
10
10
The team at [ Rocket Science] ( https://www.rocketscience.gg/ ) , contributed the
11
- [ adapter- ag-ui] ( #adapter- ag-ui ) library to make it easy to implement the AG-UI
12
- protocol with PydanticAI agents.
11
+ [ pydantic-ai- ag-ui] ( #ag-ui-adapter ) package to make it easy to implement the
12
+ AG-UI protocol with PydanticAI agents.
13
13
14
- This also includes a convenience method that exposes PydanticAI agents as an
15
- AG-UI server, which can then be used by as part of a
14
+ This also includes an [ ` Agent.to_ag_ui ` ] [ pydantic_ai.Agent.to_ag_ui ] convenience
15
+ method which simplifies the creation of [ ` Adapter ` ] [ pydantic_ai_ag_ui.Adapter ]
16
+ for PydanticAI agents, which can then be used by as part of a
16
17
[ fastapi] ( https://fastapi.tiangolo.com/ ) app. Let's have a quick look at how to
17
18
use it:
18
19
19
20
``` py {title="agent_to_ag_ui.py" py="3.10" hl_lines="17-27"}
20
21
""" Basic example for AG-UI with FastAPI and Pydantic AI."""
22
+
21
23
from __future__ import annotations
22
24
23
25
from typing import TYPE_CHECKING , Annotated
24
26
25
- from adapter_ag_ui import SSE_CONTENT_TYPE
26
27
from fastapi import FastAPI, Header
27
28
from fastapi.responses import StreamingResponse
29
+ from pydantic_ai_ag_ui import SSE_CONTENT_TYPE
28
30
29
31
from pydantic_ai import Agent
30
32
@@ -57,7 +59,7 @@ requests to it.
57
59
58
60
## AG-UI Adapter
59
61
60
- The [ AdapterAGUI ] [ adapter_ag_ui.AdapterAGUI ] class is an adapter between
62
+ The [ Adapter ] [ pydantic_ai_ag_ui.Adapter ] class is an adapter between
61
63
PydanticAI agents and the AG-UI protocol written in Python. It provides support
62
64
for all aspects of spec including:
63
65
@@ -80,16 +82,16 @@ streamed back to the caller as Server-Sent Events (SSE).
80
82
A user request may require multiple round trips between client UI and PydanticAI
81
83
server, depending on the tools and events needed.
82
84
83
- [ AdapterAGUI ] [ adapter_ag_ui.AdapterAGUI ] can be used with any ASGI server.
85
+ [ Adapter ] [ pydantic_ai_ag_ui.Adapter ] can be used with any ASGI server.
84
86
85
87
### Installation
86
88
87
- [ AdapterAGUI ] [ adapter_ag_ui.AdapterAGUI ] is available on PyPI as
88
- [ ` adapter- ag-ui` ] ( https://pypi.org/project/adapter -ag-ui/ ) so installation is as
89
+ [ Adapter ] [ pydantic_ai_ag_ui.Adapter ] is available on PyPI as
90
+ [ ` pydantic-ai- ag-ui` ] ( https://pypi.org/project/pydantic-ai -ag-ui/ ) so installation is as
89
91
simple as:
90
92
91
93
``` bash
92
- pip/uv-add adapter -ag-ui
94
+ pip/uv-add pydantic-ai -ag-ui
93
95
```
94
96
95
97
The only dependencies are:
@@ -107,7 +109,7 @@ To run the examples you'll also need:
107
109
pip/uv-add ' fastapi'
108
110
```
109
111
110
- You can install PydanticAI with the ` ag-ui ` extra to include ** AdapterAGUI ** :
112
+ You can install PydanticAI with the ` ag-ui ` extra to include ** Adapter ** :
111
113
112
114
``` bash
113
115
pip/uv-add ' pydantic-ai-slim[ag-ui]'
@@ -120,8 +122,8 @@ use the [`to_ag_ui`][pydantic_ai.agent.Agent.to_ag_ui] method in combination
120
122
with fastapi.
121
123
122
124
In the example below we have document state which is shared between the UI and
123
- server using the [ ` StateDeps ` ] [ adapter_ag_ui .StateDeps] which implements the
124
- [ ` StateHandler ` ] [ adapter_ag_ui .StateHandler] that can be used to automatically
125
+ server using the [ ` StateDeps ` ] [ pydantic_ai_ag_ui .StateDeps] which implements the
126
+ [ ` StateHandler ` ] [ pydantic_ai_ag_ui .StateHandler] that can be used to automatically
125
127
decode state contained in [ ` RunAgentInput.state ` ] ( https://docs.ag-ui.com/sdk/js/core/types#runagentinput )
126
128
when processing requests.
127
129
@@ -138,10 +140,10 @@ from __future__ import annotations
138
140
139
141
from typing import TYPE_CHECKING , Annotated
140
142
141
- from adapter_ag_ui import SSE_CONTENT_TYPE , StateDeps
142
143
from fastapi import FastAPI, Header
143
144
from fastapi.responses import StreamingResponse
144
145
from pydantic import BaseModel
146
+ from pydantic_ai_ag_ui import SSE_CONTENT_TYPE , StateDeps
145
147
146
148
from pydantic_ai import Agent
147
149
@@ -172,7 +174,6 @@ async def root(
172
174
adapter.run(input_data, accept, deps = StateDeps(state_type = DocumentState)),
173
175
media_type = SSE_CONTENT_TYPE ,
174
176
)
175
-
176
177
```
177
178
178
179
Since ` app ` is an ASGI application, it can be used with any ASGI server.
@@ -183,7 +184,7 @@ uvicorn agent_to_ag_ui:app --host 0.0.0.0 --port 8000
183
184
184
185
Since the goal of [ ` to_ag_ui ` ] [ pydantic_ai.agent.Agent.to_ag_ui ] is to be a
185
186
convenience method, it accepts the same arguments as the
186
- [ ` AdapterAGUI ` ] [ adapter_ag_ui.AdapterAGUI ] constructor.
187
+ [ ` Adapter ` ] [ pydantic_ai_ag_ui.Adapter ] constructor.
187
188
188
189
#### Tools
189
190
@@ -205,11 +206,11 @@ from __future__ import annotations
205
206
206
207
from typing import TYPE_CHECKING , Annotated
207
208
208
- from adapter_ag_ui import SSE_CONTENT_TYPE , StateDeps
209
209
from ag_ui.core import CustomEvent, EventType, StateSnapshotEvent
210
210
from fastapi import FastAPI, Header
211
211
from fastapi.responses import StreamingResponse
212
212
from pydantic import BaseModel
213
+ from pydantic_ai_ag_ui import SSE_CONTENT_TYPE , StateDeps
213
214
214
215
from pydantic_ai import Agent, RunContext
215
216
@@ -224,7 +225,6 @@ class DocumentState(BaseModel):
224
225
225
226
226
227
app = FastAPI(title = ' AG-UI Endpoint' )
227
-
228
228
agent = Agent(
229
229
' openai:gpt-4.1' ,
230
230
instructions = ' Be fun!' ,
@@ -265,30 +265,29 @@ async def root(
265
265
adapter.run(input_data, accept, deps = StateDeps(state_type = DocumentState)),
266
266
media_type = SSE_CONTENT_TYPE ,
267
267
)
268
-
269
268
```
270
269
271
270
### Examples
272
271
273
- For more examples of how to use [ ` AdapterAGUI ` ] [ adapter_ag_ui.AdapterAGUI ] see
274
- [ ` adapter_ag_ui_examples ` ] ( https://github.com/pydantic/pydantic-ai/tree/main/examples/adapter_ag_ui_examples ) ,
272
+ For more examples of how to use [ ` Adapter ` ] [ pydantic_ai_ag_ui.Adapter ] see
273
+ [ ` pydantic_ai_ag_ui_examples ` ] ( https://github.com/pydantic/pydantic-ai/tree/main/examples/pydantic_ai_ag_ui_examples ) ,
275
274
which includes working server for the with the
276
275
[ AG-UI Dojo] ( https://docs.ag-ui.com/tutorials/debugging#the-ag-ui-dojo ) which
277
- can be run from a clone of the repo or with the ` adapter_ag_ui_examples ` package
276
+ can be run from a clone of the repo or with the ` pydantic_ai_ag_ui_examples ` package
278
277
installed with either of the following:
279
278
280
279
``` bash
281
- pip/uv-add ' adapter_ag_ui_examples '
280
+ pip/uv-add ' pydantic_ai_ag_ui_examples '
282
281
```
283
282
284
283
Direct:
285
284
286
285
``` shell
287
- python -m adapter_ag_ui_examples .dojo_server
286
+ python -m pydantic_ai_ag_ui_examples .dojo_server
288
287
```
289
288
290
289
Using uvicorn:
291
290
292
291
``` shell
293
- uvicorn adapter_ag_ui_examples .dojo_server:app --host 0.0.0.0 --port 8000
292
+ uvicorn pydantic_ai_ag_ui_examples .dojo_server:app --host 0.0.0.0 --port 8000
294
293
```
0 commit comments