Skip to content

Commit 2fccb3e

Browse files
committed
document chunk events
1 parent 509e00e commit 2fccb3e

File tree

3 files changed

+128
-8
lines changed

3 files changed

+128
-8
lines changed

docs/concepts/events.mdx

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -237,18 +237,23 @@ automatic scrolling to ensure the full message is visible.
237237

238238
### TextMessageChunk
239239

240-
A self-contained text message event that combines start, content, and end.
240+
Convenience event that expands to Start → Content → End automatically.
241241

242-
The `TextMessageChunk` event provides a convenient way to send complete text messages
243-
in a single event instead of the three-event sequence (start, content, end). This is
244-
particularly useful for simple messages or when the entire content is available at once.
245-
The event includes both the message metadata and content, making it more efficient for
246-
non-streaming scenarios.
242+
The `TextMessageChunk` event lets you omit explicit `TextMessageStart` and
243+
`TextMessageEnd` events. The client stream transformer expands chunks into the
244+
standard triad:
245+
246+
- First chunk for a message must include `messageId` and will emit
247+
`TextMessageStart` (role defaults to `assistant` when not provided).
248+
- Each chunk with a `delta` emits a `TextMessageContent` for the current
249+
`messageId`.
250+
- `TextMessageEnd` is emitted automatically when the stream switches to a new
251+
message ID or when the stream completes.
247252

248253
| Property | Description |
249254
| ----------- | ------------------------------------------------------------------------------------- |
250-
| `messageId` | Optional unique identifier for the message |
251-
| `role` | Optional role of the sender ("developer", "system", "assistant", "user", "tool") |
255+
| `messageId` | Optional unique identifier for the message; required on the first chunk of a message |
256+
| `role` | Optional role of the sender ("developer", "system", "assistant", "user") |
252257
| `delta` | Optional text content of the message |
253258

254259
## Tool Call Events
@@ -359,6 +364,28 @@ the tool's output.
359364
| `content` | The actual result/output content from the tool execution |
360365
| `role` | Optional role identifier, typically "tool" for tool results |
361366

367+
### ToolCallChunk
368+
369+
Convenience event that expands to Start → Args → End automatically.
370+
371+
The `ToolCallChunk` event lets you omit explicit `ToolCallStart` and
372+
`ToolCallEnd` events. The client stream transformer expands chunks into the
373+
standard tool-call triad:
374+
375+
- First chunk for a tool call must include `toolCallId` and `toolCallName` and
376+
will emit `ToolCallStart` (propagating any `parentMessageId`).
377+
- Each chunk with a `delta` emits a `ToolCallArgs` for the current
378+
`toolCallId`.
379+
- `ToolCallEnd` is emitted automatically when the stream switches to a new
380+
`toolCallId` or when the stream completes.
381+
382+
| Property | Description |
383+
| ----------------- | --------------------------------------------------------------------------- |
384+
| `toolCallId` | Optional on later chunks; required on the first chunk of a tool call |
385+
| `toolCallName` | Optional on later chunks; required on the first chunk of a tool call |
386+
| `parentMessageId` | Optional ID of the parent message |
387+
| `delta` | Optional argument data chunk (often a JSON fragment) |
388+
362389
## State Management Events
363390

364391
These events are used to manage and synchronize the agent's state with the

docs/sdk/js/core/events.mdx

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,29 @@ type TextMessageEndEvent = BaseEvent & {
201201
| ----------- | -------- | ----------------------------------------- |
202202
| `messageId` | `string` | Matches the ID from TextMessageStartEvent |
203203
204+
### TextMessageChunkEvent
205+
206+
Convenience event that expands to `TextMessageStart``TextMessageContent`
207+
`TextMessageEnd` automatically in the JS/TS client.
208+
209+
```typescript
210+
type TextMessageChunkEvent = BaseEvent & {
211+
type: EventType.TEXT_MESSAGE_CHUNK
212+
messageId?: string // required on the first chunk for a message
213+
role?: 'developer' | 'system' | 'assistant' | 'user'
214+
delta?: string
215+
}
216+
```
217+
218+
Behavior
219+
- Omit start/end: The client transforms chunk sequences into the standard
220+
start/content/end triad, so you don’t need to emit them manually.
221+
- First chunk requirements: The first chunk for a message must include
222+
`messageId`. When `role` is omitted, it defaults to `assistant`.
223+
- Streaming: Subsequent chunks with the same `messageId` emit
224+
`TextMessageContent` events. `TextMessageEnd` is emitted automatically when a
225+
different message starts or when the stream completes.
226+
204227
## Tool Call Events
205228
206229
These events represent the lifecycle of tool calls made by agents.
@@ -430,3 +453,26 @@ const EventSchemas = z.discriminatedUnion("type", [
430453

431454
This allows for runtime validation of events and provides TypeScript type
432455
inference.
456+
### ToolCallChunkEvent
457+
458+
Convenience event that expands to `ToolCallStart``ToolCallArgs`
459+
`ToolCallEnd` automatically in the JS/TS client.
460+
461+
```typescript
462+
type ToolCallChunkEvent = BaseEvent & {
463+
type: EventType.TOOL_CALL_CHUNK
464+
toolCallId?: string // required on the first chunk for a tool call
465+
toolCallName?: string // required on the first chunk for a tool call
466+
parentMessageId?: string
467+
delta?: string
468+
}
469+
```
470+
471+
Behavior
472+
- Omit start/end: The client transforms chunk sequences into the standard
473+
start/args/end triad.
474+
- First chunk requirements: The first chunk must include both `toolCallId` and
475+
`toolCallName`; `parentMessageId` is propagated to `ToolCallStart` if given.
476+
- Streaming: Subsequent chunks with the same `toolCallId` emit `ToolCallArgs`.
477+
`ToolCallEnd` is emitted automatically when the tool call changes or when the
478+
stream completes.

docs/sdk/python/core/events.mdx

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,3 +461,50 @@ Event = Annotated[
461461

462462
This allows for runtime validation of events and type checking at development
463463
time.
464+
### TextMessageChunkEvent
465+
466+
Convenience event for complete text messages without manually emitting
467+
`TextMessageStart`/`TextMessageEnd`.
468+
469+
```python
470+
from ag_ui.core import TextMessageChunkEvent
471+
472+
class TextMessageChunkEvent(BaseEvent):
473+
type: Literal[EventType.TEXT_MESSAGE_CHUNK]
474+
message_id: Optional[str] = None # required on first chunk for a message
475+
role: Optional[TextMessageRole] = None # defaults to "assistant" in JS client
476+
delta: Optional[str] = None
477+
```
478+
479+
Behavior
480+
- Convenience: Some consumers (e.g., the JS/TS client) expand chunk events into
481+
the standard start/content/end sequence automatically, allowing producers to
482+
omit explicit start/end events when using chunks.
483+
- First chunk requirements: The first chunk for a given message must include
484+
`message_id`.
485+
- Streaming: Subsequent chunks with the same `message_id` correspond to content
486+
pieces; completion triggers an implied end in clients that perform expansion.
487+
488+
### ToolCallChunkEvent
489+
490+
Convenience event for tool calls without manually emitting
491+
`ToolCallStart`/`ToolCallEnd`.
492+
493+
```python
494+
from ag_ui.core import ToolCallChunkEvent
495+
496+
class ToolCallChunkEvent(BaseEvent):
497+
type: Literal[EventType.TOOL_CALL_CHUNK]
498+
tool_call_id: Optional[str] = None # required on first chunk
499+
tool_call_name: Optional[str] = None # required on first chunk
500+
parent_message_id: Optional[str] = None
501+
delta: Optional[str] = None
502+
```
503+
504+
Behavior
505+
- Convenience: Consumers may expand chunk sequences into the standard
506+
start/args/end triad (the JS/TS client does this automatically).
507+
- First chunk requirements: Include both `tool_call_id` and `tool_call_name` on
508+
the first chunk.
509+
- Streaming: Subsequent chunks with the same `tool_call_id` correspond to args
510+
pieces; completion triggers an implied end in clients that perform expansion.

0 commit comments

Comments
 (0)