@@ -14,10 +14,55 @@ AG-UI protocol with PydanticAI agents.
14
14
This also includes an [ ` Agent.to_ag_ui ` ] [ pydantic_ai.Agent.to_ag_ui ] convenience
15
15
method which simplifies the creation of [ ` Adapter ` ] [ pydantic_ai_ag_ui.Adapter ]
16
16
for PydanticAI agents, which can then be used by as part of a
17
- [ fastapi] ( https://fastapi.tiangolo.com/ ) app. Let's have a quick look at how to
18
- use it:
17
+ [ fastapi] ( https://fastapi.tiangolo.com/ ) app.
19
18
20
- ``` py {title="agent_to_ag_ui.py" py="3.10" hl_lines="17-27"}
19
+ ## AG-UI Adapter
20
+
21
+ The [ Adapter] [ pydantic_ai_ag_ui.Adapter ] class is an adapter between
22
+ PydanticAI agents and the AG-UI protocol written in Python. It provides support
23
+ for all aspects of spec including:
24
+
25
+ - [ Events] ( https://docs.ag-ui.com/concepts/events )
26
+ - [ Messages] ( https://docs.ag-ui.com/concepts/messages )
27
+ - [ State Management] ( https://docs.ag-ui.com/concepts/state )
28
+ - [ Tools] ( https://docs.ag-ui.com/concepts/tools )
29
+
30
+ Let's have a quick look at how to use it:
31
+
32
+ ### Installation
33
+
34
+ [ Adapter] [ pydantic_ai_ag_ui.Adapter ] is available on PyPI as
35
+ [ ` pydantic-ai-ag-ui ` ] ( https://pypi.org/project/pydantic-ai-ag-ui/ ) so installation is as
36
+ simple as:
37
+
38
+ ``` bash
39
+ pip/uv-add pydantic-ai-ag-ui
40
+ ```
41
+
42
+ The only dependencies are:
43
+
44
+ - [ ag-ui-protocol] ( https://docs.ag-ui.com/introduction ) : to provide the AG-UI
45
+ types and encoder.
46
+ - [ pydantic] ( https://pydantic.dev ) : to validate the request/response messages
47
+ - [ pydantic-ai] ( https://ai.pydantic.dev/ ) : to provide the agent framework
48
+
49
+ To run the examples you'll also need:
50
+
51
+ - [ fastapi] ( https://fastapi.tiangolo.com/ ) : to provide ASGI compatible server
52
+
53
+ ``` bash
54
+ pip/uv-add ' fastapi'
55
+ ```
56
+
57
+ You can install PydanticAI with the ` ag-ui ` extra to include ** Adapter** :
58
+
59
+ ``` bash
60
+ pip/uv-add ' pydantic-ai-slim[ag-ui]'
61
+ ```
62
+
63
+ ### Quick start
64
+
65
+ ``` py {title="agent_to_ag_ui.py" py="3.10" hl_lines="17-28"}
21
66
""" Basic example for AG-UI with FastAPI and Pydantic AI."""
22
67
23
68
from __future__ import annotations
@@ -33,9 +78,9 @@ from pydantic_ai import Agent
33
78
if TYPE_CHECKING :
34
79
from ag_ui.core import RunAgentInput
35
80
36
- app = FastAPI(title = ' AG-UI Endpoint' )
37
81
agent = Agent(' openai:gpt-4.1' , instructions = ' Be fun!' )
38
82
adapter = agent.to_ag_ui()
83
+ app = FastAPI(title = ' AG-UI Endpoint' )
39
84
40
85
41
86
@app.post (' /' )
@@ -57,17 +102,6 @@ uvicorn agent_to_ag_ui:app --host 0.0.0.0 --port 8000
57
102
This will expose the agent as an AG-UI server, and you can start sending
58
103
requests to it.
59
104
60
- ## AG-UI Adapter
61
-
62
- The [ Adapter] [ pydantic_ai_ag_ui.Adapter ] class is an adapter between
63
- PydanticAI agents and the AG-UI protocol written in Python. It provides support
64
- for all aspects of spec including:
65
-
66
- - [ Events] ( https://docs.ag-ui.com/concepts/events )
67
- - [ Messages] ( https://docs.ag-ui.com/concepts/messages )
68
- - [ State Management] ( https://docs.ag-ui.com/concepts/state )
69
- - [ Tools] ( https://docs.ag-ui.com/concepts/tools )
70
-
71
105
### Design
72
106
73
107
The adapter receives messages in the form of a
@@ -84,42 +118,11 @@ server, depending on the tools and events needed.
84
118
85
119
[ Adapter] [ pydantic_ai_ag_ui.Adapter ] can be used with any ASGI server.
86
120
87
- ### Installation
88
-
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
91
- simple as:
92
-
93
- ``` bash
94
- pip/uv-add pydantic-ai-ag-ui
95
- ```
96
-
97
- The only dependencies are:
98
-
99
- - [ ag-ui-protocol] ( https://docs.ag-ui.com/introduction ) : to provide the AG-UI
100
- types and encoder.
101
- - [ pydantic] ( https://pydantic.dev ) : to validate the request/response messages
102
- - [ pydantic-ai] ( https://ai.pydantic.dev/ ) : to provide the agent framework
103
-
104
- To run the examples you'll also need:
105
-
106
- - [ fastapi] ( https://fastapi.tiangolo.com/ ) : to provide ASGI compatible server
107
-
108
- ``` bash
109
- pip/uv-add ' fastapi'
110
- ```
111
-
112
- You can install PydanticAI with the ` ag-ui ` extra to include ** Adapter** :
113
-
114
- ``` bash
115
- pip/uv-add ' pydantic-ai-slim[ag-ui]'
116
- ```
117
-
118
- ### Usage
121
+ ### Features
119
122
120
123
To expose a PydanticAI agent as an AG-UI server including state support, you can
121
124
use the [ ` to_ag_ui ` ] [ pydantic_ai.agent.Agent.to_ag_ui ] method in combination
122
- with fastapi.
125
+ with [ fastapi] ( https://fastapi.tiangolo.com/ ) .
123
126
124
127
In the example below we have document state which is shared between the UI and
125
128
server using the [ ` StateDeps ` ] [ pydantic_ai_ag_ui.StateDeps ] which implements the
@@ -133,7 +136,7 @@ The adapter provides full support for
133
136
[ AG-UI state management] ( https://docs.ag-ui.com/concepts/state ) , which enables
134
137
real-time synchronization between agents and frontend applications.
135
138
136
- ``` python {title="ag_ui_state.py" py="3.10" hl_lines="18-21,28,38 "}
139
+ ``` python {title="ag_ui_state.py" py="3.10" hl_lines="18-40 "}
137
140
""" State example for AG-UI with FastAPI and Pydantic AI."""
138
141
139
142
from __future__ import annotations
@@ -157,13 +160,13 @@ class DocumentState(BaseModel):
157
160
document: str
158
161
159
162
160
- app = FastAPI(title = ' AG-UI Endpoint' )
161
163
agent = Agent(
162
164
' openai:gpt-4.1' ,
163
165
instructions = ' Be fun!' ,
164
166
deps_type = StateDeps[DocumentState],
165
167
)
166
168
adapter = agent.to_ag_ui()
169
+ app = FastAPI(title = ' AG-UI Endpoint' )
167
170
168
171
169
172
@app.post (' /' )
@@ -199,7 +202,7 @@ which returns a type based off
199
202
[ ` BaseEvent ` ] ( https://docs.ag-ui.com/sdk/js/core/events#baseevent ) this allows
200
203
for custom events and state updates.
201
204
202
- ``` python {title="ag_ui_tool_events.py" py="3.10" hl_lines="35-56 "}
205
+ ``` python {title="ag_ui_tool_events.py" py="3.10" hl_lines="34-55 "}
203
206
""" Tool events example for AG-UI with FastAPI and Pydantic AI."""
204
207
205
208
from __future__ import annotations
@@ -224,13 +227,13 @@ class DocumentState(BaseModel):
224
227
document: str
225
228
226
229
227
- app = FastAPI(title = ' AG-UI Endpoint' )
228
230
agent = Agent(
229
231
' openai:gpt-4.1' ,
230
232
instructions = ' Be fun!' ,
231
233
deps_type = StateDeps[DocumentState],
232
234
)
233
235
adapter = agent.to_ag_ui()
236
+ app = FastAPI(title = ' AG-UI Endpoint' )
234
237
235
238
236
239
@agent.tool
0 commit comments