Skip to content

Commit 36a81ba

Browse files
committed
Clarify aggregators
1 parent 7335f3b commit 36a81ba

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,21 @@ AI SDK for Python supports tools that are async-generators. You might want to
5151
use those when your tools need to return partial output, e.g. when wrapping
5252
subagents.
5353

54-
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 through,
56-
and the LLM needs to receive an accumulated result. That return type must be
57-
a subtype of `ai.Aggregator`.
54+
Streaming tools use aggregators. An aggregator solves two problems: the tool can
55+
yield many values over time, but the agent still needs one final tool result;
56+
and your app may want a rich stored result while the model needs a simpler value
57+
on the next turn.
58+
59+
The core interface is:
60+
61+
```python
62+
class Aggregator[Item, Result, ModelInput]:
63+
def feed(self, item: Item) -> None: ...
64+
def snapshot(self) -> Result: ...
65+
66+
@classmethod
67+
def to_model_input(cls, snapshot: Result) -> ModelInput: ...
68+
```
5869

5970
Use `ai.StreamingTextTool` when yielded strings should concatenate into the
6071
tool result:
@@ -71,6 +82,9 @@ async def draft_reply(topic: str) -> ai.StreamingTextTool:
7182
Use `ai.StreamingStatusTool[T]` when intermediate yields are progress updates
7283
and the last yielded value is the final result.
7384

85+
Use `ai.SubAgentTool` when a tool streams events from a nested agent. The stored
86+
result is a message bundle, and the model sees the final assistant text.
87+
7488
The agent emits `PartialToolCallResult` events for those values, then sends
7589
the aggregated result back to the model on the next turn.
7690

0 commit comments

Comments
 (0)