Skip to content

Commit 7335f3b

Browse files
committed
Update messages and tools sections
1 parent 378dfa0 commit 7335f3b

2 files changed

Lines changed: 80 additions & 15 deletions

File tree

docs/ai-python/content/docs/basics/messages-and-events.mdx

Lines changed: 68 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,38 @@ summary: Construct messages, add files, read outputs, serialize history, handle
66
---
77

88
Messages 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,
1010
store, 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
1313
when your app needs that.
1414

1515
## Build messages
1616

1717
Messages 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
2341
messages:
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
4260
them.
4361

4462
```python
@@ -61,17 +79,17 @@ print(message.tool_calls)
6179
print(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
6886
forecast = 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
7795
if isinstance(event, ai.events.TextDelta):
@@ -83,6 +101,43 @@ arrive through `ToolStart`, `ToolDelta`, and `ToolEnd` events. Provider-executed
83101
tools 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
87142
async 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
96151
ignore events you do not need, or route them into your application UI,
97152
observability 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

101159
Providers attach usage to events when they report it. The latest value is also

docs/ai-python/content/docs/basics/tools.mdx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ type: guide
55
summary: Define function tools, design schemas, handle tool errors, use schema-only tools, provider tools, and MCP tools.
66
---
77

8-
AI SDK for Python supports multiple types of tools, inlcuding function tools
8+
AI SDK for Python supports multiple types of tools, including function tools
99
defined in code using `@ai.tool`, as well as built-in provider-side tools.
1010

1111
## Declare function tools
@@ -52,7 +52,7 @@ use those when your tools need to return partial output, e.g. when wrapping
5252
subagents.
5353

5454
Streaming tools require a special return type in order to get correctly
55-
handled by the SDK, since partial outputs need to be accumulated, passed though,
55+
handled by the SDK, since partial outputs need to be accumulated, passed through,
5656
and the LLM needs to receive an accumulated result. That return type must be
5757
a subtype of `ai.Aggregator`.
5858

@@ -71,13 +71,20 @@ async def draft_reply(topic: str) -> ai.StreamingTextTool:
7171
Use `ai.StreamingStatusTool[T]` when intermediate yields are progress updates
7272
and the last yielded value is the final result.
7373

74-
The agent emits `PartialToolCallResult` events for those values, then sends
74+
The agent emits `PartialToolCallResult` events for those values, then sends
7575
the aggregated result back to the model on the next turn.
7676

7777
## Understand tool anatomy
7878

79-
> provide a diagram of ai.Tool, AgentTool, provider tools, etc.
80-
> mention that tools contain spec and params, and which tools have which
79+
`ai.Tool` represents tools of all kinds in the SDK.
80+
81+
Each tool carries an optional `spec`, which is a description consumed by the
82+
model, as well as a `tool_config` that contains tool-specific execution config
83+
(e.g. retry policy).
84+
85+
`AgentTool` wraps `ai.Tool` together with its corresponding Python function,
86+
which allows `agent.run` to find and call that function when the model requests
87+
to do so.
8188

8289
Pass `ai.Tool` objects directly to `ai.stream` when you want the model to emit
8390
tool calls but you do not want the SDK to execute them:

0 commit comments

Comments
 (0)