pydantic_ai.ag_ui
Provides an AG-UI protocol adapter for the Pydantic AI agent.
This package provides seamless integration between pydantic-ai agents and ag-ui for building interactive AI applications with streaming event-based communication.
SSE_CONTENT_TYPE
module-attribute
Content type header value for Server-Sent Events (SSE).
AGUIApp
Bases: Generic[AgentDepsT, OutputDataT]
, Starlette
ASGI application for running Pydantic AI agents with AG-UI protocol support.
Source code in pydantic_ai_slim/pydantic_ai/ag_ui.py
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
|
__init__
__init__(
agent: Agent[AgentDepsT, OutputDataT],
*,
output_type: OutputSpec[OutputDataT] | None = None,
model: Model | KnownModelName | str | None = None,
deps: AgentDepsT = None,
model_settings: ModelSettings | None = None,
usage_limits: UsageLimits | None = None,
usage: Usage | None = None,
infer_name: bool = True,
toolsets: (
Sequence[AbstractToolset[AgentDepsT]] | None
) = None,
debug: bool = False,
routes: Sequence[BaseRoute] | None = None,
middleware: Sequence[Middleware] | None = None,
exception_handlers: (
Mapping[Any, ExceptionHandler] | None
) = None,
on_startup: Sequence[Callable[[], Any]] | None = None,
on_shutdown: Sequence[Callable[[], Any]] | None = None,
lifespan: (
Lifespan[AGUIApp[AgentDepsT, OutputDataT]] | None
) = None
) -> None
Initialise the AG-UI application.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
agent
|
Agent[AgentDepsT, OutputDataT]
|
The Pydantic AI |
required |
output_type
|
OutputSpec[OutputDataT] | None
|
Custom output type to use for this run, |
None
|
model
|
Model | KnownModelName | str | None
|
Optional model to use for this run, required if |
None
|
deps
|
AgentDepsT
|
Optional dependencies to use for this run. |
None
|
model_settings
|
ModelSettings | None
|
Optional settings to use for this model's request. |
None
|
usage_limits
|
UsageLimits | None
|
Optional limits on model request count or token usage. |
None
|
usage
|
Usage | None
|
Optional usage to start with, useful for resuming a conversation or agents used in tools. |
None
|
infer_name
|
bool
|
Whether to try to infer the agent name from the call frame if it's not set. |
True
|
toolsets
|
Sequence[AbstractToolset[AgentDepsT]] | None
|
Optional list of toolsets to use for this agent, defaults to the agent's toolset. |
None
|
debug
|
bool
|
Boolean indicating if debug tracebacks should be returned on errors. |
False
|
routes
|
Sequence[BaseRoute] | None
|
A list of routes to serve incoming HTTP and WebSocket requests. |
None
|
middleware
|
Sequence[Middleware] | None
|
A list of middleware to run for every request. A starlette application will always
automatically include two middleware classes. |
None
|
exception_handlers
|
Mapping[Any, ExceptionHandler] | None
|
A mapping of either integer status codes, or exception class types onto
callables which handle the exceptions. Exception handler callables should be of the form
|
None
|
on_startup
|
Sequence[Callable[[], Any]] | None
|
A list of callables to run on application startup. Startup handler callables do not take any arguments, and may be either standard functions, or async functions. |
None
|
on_shutdown
|
Sequence[Callable[[], Any]] | None
|
A list of callables to run on application shutdown. Shutdown handler callables do not take any arguments, and may be either standard functions, or async functions. |
None
|
lifespan
|
Lifespan[AGUIApp[AgentDepsT, OutputDataT]] | None
|
A lifespan context function, which can be used to perform startup and shutdown tasks.
This is a newer style that replaces the |
None
|
Source code in pydantic_ai_slim/pydantic_ai/ag_ui.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
|
StateHandler
Bases: Protocol
Protocol for state handlers in agent runs.
Source code in pydantic_ai_slim/pydantic_ai/ag_ui.py
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 |
|
state
property
writable
state: State
Get the current state of the agent run.
StateDeps
Bases: Generic[StateT]
Provides AG-UI state management.
This class is used to manage the state of an agent run. It allows setting
the state of the agent run with a specific type of state model, which must
be a subclass of BaseModel
.
The state is set using the state
setter by the Adapter
when the run starts.
Implements the StateHandler
protocol.
Source code in pydantic_ai_slim/pydantic_ai/ag_ui.py
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 |
|
__init__
__init__(default: StateT) -> None
Initialize the state with the provided state type.
Source code in pydantic_ai_slim/pydantic_ai/ag_ui.py
583 584 585 |
|
state
property
writable
state: StateT
Get the current state of the agent run.
Returns:
Type | Description |
---|---|
StateT
|
The current run state. |