You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/ag-ui.md
+9-4Lines changed: 9 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -82,10 +82,15 @@ The adapter provides full support for
82
82
real-time synchronization between agents and frontend applications.
83
83
84
84
In the example below we have document state which is shared between the UI and
85
-
server using the [`StateDeps`][pydantic_ai.ag_ui.StateDeps] which implements the
86
-
[`StateHandler`][pydantic_ai.ag_ui.StateHandler] protocol that can be used to automatically
87
-
decode state contained in [`RunAgentInput.state`](https://docs.ag-ui.com/sdk/js/core/types#runagentinput)
88
-
when processing requests.
85
+
server using the [`StateDeps`][pydantic_ai.ag_ui.StateDeps][dependencies type](./dependencies.md) that can be used to automatically
86
+
validate state contained in [`RunAgentInput.state`](https://docs.ag-ui.com/sdk/js/core/types#runagentinput) using a Pydantic `BaseModel` specified as a generic parameter.
87
+
88
+
!!! note "Custom dependencies type with AG-UI state"
89
+
If you want to use your own dependencies type to hold AG-UI state as well as other things, it needs to implements the
90
+
[`StateHandler`][pydantic_ai.ag_ui.StateHandler] protocol, meaning it needs to be a [dataclass](https://docs.python.org/3/library/dataclasses.html) with a non-optional `state` field. This lets Pydantic AI ensure that state is properly isolated between requests by building a new dependencies object each time.
91
+
92
+
If the `state` field's type is a Pydantic `BaseModel` subclass, the raw state dictionary on the request is automatically validated. If not, you can validate the raw value yourself in your dependencies dataclass's `__post_init__` method.
f'AG-UI state is provided but `deps` of type `{type(deps).__name__}` does not implement the `StateHandler` protocol: it needs to be a dataclass with a non-optional `state` field.'
312
+
)
313
+
else:
314
+
# `deps` not being a `StateHandler` is OK if there is no state.
match='AG-UI state is provided but `deps` of type `NoneType` does not implement the `StateHandler` protocol: it needs to be a dataclass with a non-optional `state` field.',
0 commit comments