Skip to content

Commit 9f81820

Browse files
committed
docs(ag-ui): various documentation improvements
1 parent a471089 commit 9f81820

File tree

3 files changed

+45
-29
lines changed

3 files changed

+45
-29
lines changed

adapter_ag_ui/adapter_ag_ui/adapter.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@
7777

7878

7979
_LOGGER: logging.Logger = logging.getLogger(__name__)
80-
_LOGGER.setLevel(logging.DEBUG)
8180

8281

8382
@dataclass(repr=False)
@@ -105,12 +104,9 @@ def new_message_id(self) -> str:
105104
class AdapterAGUI(Generic[AgentDepsT, OutputDataT]):
106105
"""An agent adapter providing AG-UI protocol support for PydanticAI agents.
107106
108-
This class manages the agent runs, tool calls, and provides an adapter
109-
for running agents with Server-Sent Event (SSE) streaming responses using
110-
the AG-UI protocol.
111-
112-
It supports multiple threads and runs, allowing for concurrent agent
113-
execution and tool calls.
107+
This class manages the agent runs, tool calls, state storage and providing
108+
an adapter for running agents with Server-Sent Event (SSE) streaming
109+
responses using the AG-UI protocol.
114110
115111
Example:
116112
@@ -143,8 +139,8 @@ async def root(input_data: RunAgentInput, accept: Annotated[str, Header()] = SSE
143139
media_type=SSE_CONTENT_TYPE,
144140
)
145141
146-
PydanticAI Tools which return AG-UI events will be sent to the client
147-
as part of the event stream, single events and iterables of events are
142+
PydanticAI tools which return AG-UI events will be sent to the client
143+
as part of the event stream, single events and event iterables are
148144
supported.
149145
150146
Examples:
@@ -296,8 +292,9 @@ async def _tool_events(
296292
Yields:
297293
AG-UI Server-Sent Events (SSE).
298294
"""
299-
# TODO(steve): Determine how to handle when we have multiple parts. Currently
300-
# AG-UI only seems to support a single tool call per request.
295+
# TODO(steve): Determine how to handle multiple parts. Currently
296+
# AG-UI only supports a single tool call per request, but that
297+
# may change in the future.
301298
part: ModelRequestPart
302299
for part in parts:
303300
if not isinstance(part, ToolReturnPart):
@@ -323,9 +320,9 @@ async def _tool_events(
323320
def _convert_tools(self, run_tools: list[ToolAGUI]) -> list[Tool[AgentDepsT]]:
324321
"""Convert AG-UI tools to PydanticAI tools.
325322
326-
Creates placeholder Tool objects from AG-UI tool definitions. These tools
327-
don't actually execute anything - they just provide the necessary tool
328-
definitions and names for the PydanticAI agent.
323+
Creates `Tool` objects from AG-UI tool definitions. These tools don't
324+
actually execute anything, that is done by AG-UI client - they just
325+
provide the necessary tool definitions to PydanticAI agent.
329326
330327
Args:
331328
run_tools: List of AG-UI tool definitions to convert.
@@ -342,7 +339,7 @@ def _tool_call(self, tool: ToolAGUI) -> Tool[AgentDepsT]:
342339
tool: The AG-UI tool definition to convert.
343340
344341
Returns:
345-
A PydanticAI Tool object that calls the AG-UI tool.
342+
A PydanticAI `Tool` object that calls the AG-UI tool.
346343
"""
347344

348345
def _tool_stub(*args: Any, **kwargs: Any) -> ToolResult:
@@ -352,7 +349,7 @@ def _tool_stub(*args: Any, **kwargs: Any) -> ToolResult:
352349
Never returns as it always raises an exception.
353350
354351
Raises:
355-
UnexpectedToolCallError: Always raised since this is a stub.
352+
UnexpectedToolCallError: Always raised since this should never be called.
356353
"""
357354
raise UnexpectedToolCallError(tool_name=tool.name) # pragma: no cover
358355

@@ -521,13 +518,13 @@ async def _handle_agent_event(
521518

522519

523520
def _convert_history(messages: list[Message]) -> list[ModelMessage]:
524-
"""Convert a list of AG-UI history to a PydanticAI one.
521+
"""Convert a AG-UI history to a PydanticAI one.
525522
526523
Args:
527-
messages: List of `ag_ui.Message` to convert.
524+
messages: List of AG-UI messages to convert.
528525
529526
Returns:
530-
List of `ModelMessage` compatible with PydanticAI.
527+
List of PydanticAI model messages.
531528
"""
532529
msg: Message
533530
result: list[ModelMessage] = []

adapter_ag_ui/adapter_ag_ui/deps.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,21 @@
99
from ._exceptions import InvalidStateError
1010

1111
StateT = TypeVar('StateT', bound=BaseModel, contravariant=True)
12+
"""Type variable for the state type, which must be a subclass of `BaseModel`."""
1213

1314

1415
@dataclass(kw_only=True)
1516
class StateDeps(Generic[StateT]):
16-
"""Provides AG-UI state management."""
17+
"""Provides AG-UI state management.
18+
19+
This class is used to manage the state of an agent run. It allows setting
20+
the state of the agent run with a specific type of state model, which must
21+
be a subclass of `BaseModel`.
22+
23+
The state is set using the `set_state` when the run starts by the `AdapterAGUI`.
24+
25+
Implements the `StateHandler` protocol.
26+
"""
1727

1828
state_type: type[StateT]
1929
state: StateT = field(init=False)

docs/ag-ui.md

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
The [Agent User Interaction (AG-UI) Protocol](https://docs.ag-ui.com/introduction)
44
is an open standard introduced by the
55
[CopilotKit](https://webflow.copilotkit.ai/blog/introducing-ag-ui-the-protocol-where-agents-meet-users)
6-
team that standardises how front-end applications connect to AI agents through
6+
team that standardises how frontend applications connect to AI agents through
77
an open protocol. Think of it as a universal translator for AI-driven systems
88
no matter what language an agent speaks: AG-UI ensures fluent communication.
99

1010
The team at [Rocket Science](https://www.rocketscience.gg/), contributed the
1111
[adapter-ag-ui](#adapter-ag-ui) library to make it easy to implement the AG-UI
1212
protocol with PydanticAI agents.
1313

14-
This also includes a convenience method that expose PydanticAI agents as AG-UI
15-
servers - let's have a quick look at how to use it:
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
16+
[fastapi](https://fastapi.tiangolo.com/) app. Let's have a quick look at how to
17+
use it:
1618

1719
```py {title="agent_to_ag_ui.py" py="3.10" hl_lines="17-27"}
1820
"""Basic example for AG-UI with FastAPI and Pydantic AI."""
@@ -53,20 +55,27 @@ uvicorn agent_to_ag_ui:app --host 0.0.0.0 --port 8000
5355
This will expose the agent as an AG-UI server, and you can start sending
5456
requests to it.
5557

56-
## Adapter AG UI
58+
## AG-UI Adapter
5759

58-
[AdapterAGUI][adapter_ag_ui.AdapterAGUI]is an adapter between PydanticAI agents
59-
and the AG-UI protocol written in Python.
60+
The [AdapterAGUI][adapter_ag_ui.AdapterAGUI] class is an adapter between
61+
PydanticAI agents and the AG-UI protocol written in Python. It provides support
62+
for all aspects of spec including:
63+
64+
- [Events](https://docs.ag-ui.com/concepts/events)
65+
- [Messages](https://docs.ag-ui.com/concepts/messages)
66+
- [State Management](https://docs.ag-ui.com/concepts/state)
67+
- [Tools](https://docs.ag-ui.com/concepts/tools)
6068

6169
### Design
6270

6371
The adapter receives messages in the form of a
6472
[`RunAgentInput`](https://docs.ag-ui.com/sdk/js/core/types#runagentinput)
6573
which describes the details of a request being passed to the agent including
6674
messages and state. These are then converted to PydanticAI types, passed to the
67-
provided agent which then process the request. Results from the agent are
68-
converted from PydanticAI types to AG-UI events and streamed back to the caller
69-
as Server-Sent Events (SSE).
75+
agent which then process the request.
76+
77+
Results from the agent are converted from PydanticAI types to AG-UI events and
78+
streamed back to the caller as Server-Sent Events (SSE).
7079

7180
A user request may require multiple round trips between client UI and PydanticAI
7281
server, depending on the tools and events needed.

0 commit comments

Comments
 (0)