@@ -6,20 +6,38 @@ summary: Construct messages, add files, read outputs, serialize history, handle
66---
77
88Messages and events make up AI SDK for Python's data model. Events are used
9- to communicate transient streaming state, while messages are used to accumulate,
9+ to communicate transient streaming state, while messages are used to accumulate,
1010store, and pass around non-streaming state.
1111
12- Both are Pydantic models, so you can pattern-match, serialize, and persist them
12+ Both are Pydantic models, so you can pattern-match, serialize, and persist them
1313when your app needs that.
1414
1515## Build messages
1616
1717Messages store the conversation history you send to the model. Each message
18- represents a turn in the conversation and is made up of ` Part ` 's :
18+ represents a turn in the conversation and is made up of ` Part ` values :
1919
20- > insert flowchart of what the message is made out of
20+ ``` python
21+ class Message :
22+ role: " system" | " user" | " assistant" | " tool" | " internal"
23+ parts: list[Part]
24+ ...
25+
26+
27+ Part = (
28+ TextPart
29+ | ReasoningPart
30+ | ToolCallPart
31+ | ToolResultPart
32+ | BuiltinToolCallPart
33+ | BuiltinToolReturnPart
34+ | FilePart
35+ | HookPart
36+ )
37+
38+ ```
2139
22- Use builtin factory functions to quickly construct frequently used shapes of
40+ Use built-in factory functions to quickly construct frequently used shapes of
2341messages:
2442
2543``` python
@@ -38,7 +56,7 @@ restored = [ai.messages.Message.model_validate(item) for item in encoded]
3856
3957## Add files and multimodal input
4058
41- File parts let you send images, audio, or documents when the provider supports
59+ File parts let you send images, audio, or documents when the provider supports
4260them.
4361
4462``` python
@@ -61,17 +79,17 @@ print(message.tool_calls)
6179print (message.files)
6280```
6381
64- When working with structured outputs, use ` get_output ` to get the final assistant
65- message wrapped into the provided Pydantic model.
82+ When working with structured outputs, use ` get_output ` to get the final
83+ assistant message wrapped into the provided Pydantic model.
6684
6785``` python
6886forecast = message.get_output(Forecast)
6987```
7088
7189## Handle stream events
7290
73- Streams and agents both yield event objects from ` ai.events ` . Applications can handle
74- each kind of event as they see fit:
91+ Streams and agents both yield event objects from ` ai.events ` . Applications can
92+ handle each kind of event as they see fit:
7593
7694``` python
7795if isinstance (event, ai.events.TextDelta):
@@ -83,6 +101,43 @@ arrive through `ToolStart`, `ToolDelta`, and `ToolEnd` events. Provider-executed
83101tools use the ` BuiltinToolStart ` , ` BuiltinToolDelta ` , ` BuiltinToolEnd ` , and
84102` BuiltinToolResult ` events.
85103
104+ ``` text
105+ StreamStart
106+
107+ # text output
108+ TextStart
109+ TextDelta ...
110+ TextEnd
111+
112+ # reasoning output
113+ ReasoningStart
114+ ReasoningDelta ...
115+ ReasoningEnd
116+
117+ # host-executed tool call
118+ ToolStart
119+ ToolDelta ...
120+ ToolEnd
121+
122+ # provider-executed tool call
123+ BuiltinToolStart
124+ BuiltinToolDelta ...
125+ BuiltinToolEnd
126+ BuiltinToolResult
127+
128+ # generated file
129+ FileEvent
130+
131+ StreamEnd
132+ ```
133+
134+ ``` text
135+ # Agent runs can also interleave:
136+ ToolCallResult # emitted after a Python tool finishes
137+ PartialToolCallResult # emitted while a streaming tool or subagent yields
138+ HookEvent # emitted when a hook is pending, resolved, or cancelled
139+ ```
140+
86141``` python
87142async with ai.stream(model, messages, tools = tools) as stream:
88143 async for event in stream:
@@ -96,6 +151,9 @@ Agents can also emit tool results, hook events, and partial tool output. You can
96151ignore events you do not need, or route them into your application UI,
97152observability pipeline, or durable workflow.
98153
154+ Every event also carries a reference to a ` ai.Message ` that has partial content
155+ accumulated from the stream * so far* .
156+
99157## Track usage
100158
101159Providers attach usage to events when they report it. The latest value is also
0 commit comments