Skip to content

Commit bbfc941

Browse files
committed
docs(ag-ui): improve layout and structure
1 parent 564d9c3 commit bbfc941

File tree

2 files changed

+56
-53
lines changed

2 files changed

+56
-53
lines changed

docs/ag-ui.md

Lines changed: 55 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,55 @@ AG-UI protocol with PydanticAI agents.
1414
This also includes an [`Agent.to_ag_ui`][pydantic_ai.Agent.to_ag_ui] convenience
1515
method which simplifies the creation of [`Adapter`][pydantic_ai_ag_ui.Adapter]
1616
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.
1918

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"}
2166
"""Basic example for AG-UI with FastAPI and Pydantic AI."""
2267

2368
from __future__ import annotations
@@ -33,9 +78,9 @@ from pydantic_ai import Agent
3378
if TYPE_CHECKING:
3479
from ag_ui.core import RunAgentInput
3580

36-
app = FastAPI(title='AG-UI Endpoint')
3781
agent = Agent('openai:gpt-4.1', instructions='Be fun!')
3882
adapter = agent.to_ag_ui()
83+
app = FastAPI(title='AG-UI Endpoint')
3984

4085

4186
@app.post('/')
@@ -57,17 +102,6 @@ uvicorn agent_to_ag_ui:app --host 0.0.0.0 --port 8000
57102
This will expose the agent as an AG-UI server, and you can start sending
58103
requests to it.
59104

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-
71105
### Design
72106

73107
The adapter receives messages in the form of a
@@ -84,42 +118,11 @@ server, depending on the tools and events needed.
84118

85119
[Adapter][pydantic_ai_ag_ui.Adapter] can be used with any ASGI server.
86120

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
119122

120123
To expose a PydanticAI agent as an AG-UI server including state support, you can
121124
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/).
123126

124127
In the example below we have document state which is shared between the UI and
125128
server using the [`StateDeps`][pydantic_ai_ag_ui.StateDeps] which implements the
@@ -133,7 +136,7 @@ The adapter provides full support for
133136
[AG-UI state management](https://docs.ag-ui.com/concepts/state), which enables
134137
real-time synchronization between agents and frontend applications.
135138

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"}
137140
"""State example for AG-UI with FastAPI and Pydantic AI."""
138141

139142
from __future__ import annotations
@@ -157,13 +160,13 @@ class DocumentState(BaseModel):
157160
document: str
158161

159162

160-
app = FastAPI(title='AG-UI Endpoint')
161163
agent = Agent(
162164
'openai:gpt-4.1',
163165
instructions='Be fun!',
164166
deps_type=StateDeps[DocumentState],
165167
)
166168
adapter = agent.to_ag_ui()
169+
app = FastAPI(title='AG-UI Endpoint')
167170

168171

169172
@app.post('/')
@@ -199,7 +202,7 @@ which returns a type based off
199202
[`BaseEvent`](https://docs.ag-ui.com/sdk/js/core/events#baseevent) this allows
200203
for custom events and state updates.
201204

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"}
203206
"""Tool events example for AG-UI with FastAPI and Pydantic AI."""
204207

205208
from __future__ import annotations
@@ -224,13 +227,13 @@ class DocumentState(BaseModel):
224227
document: str
225228

226229

227-
app = FastAPI(title='AG-UI Endpoint')
228230
agent = Agent(
229231
'openai:gpt-4.1',
230232
instructions='Be fun!',
231233
deps_type=StateDeps[DocumentState],
232234
)
233235
adapter = agent.to_ag_ui()
236+
app = FastAPI(title='AG-UI Endpoint')
234237

235238

236239
@agent.tool

pydantic_ai_ag_ui/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# AG-UI Adapter
1+
# PydanticAI AG-UI Adapter
22

33
[![CI](https://github.com/pydantic/pydantic-ai/actions/workflows/ci.yml/badge.svg?event=push)](https://github.com/pydantic/pydantic-ai/actions/workflows/ci.yml?query=branch%3Amain)
44
[![Coverage](https://coverage-badge.samuelcolvin.workers.dev/pydantic/pydantic-ai.svg)](https://coverage-badge.samuelcolvin.workers.dev/redirect/pydantic/pydantic-ai)

0 commit comments

Comments
 (0)