Streaming API¶
The streaming module provides event types for real-time task progress monitoring.
Event Types¶
All events inherit from StreamEvent and can be used with Zap.stream_task().
StreamEvent¶
Base class for all streaming events.
zap_ai.streaming.StreamEvent
dataclass
¶
Base class for all streaming events.
Attributes:
| Name | Type | Description |
|---|---|---|
type |
str
|
Event type identifier. |
timestamp |
str
|
ISO format timestamp when event occurred. |
task_id |
str
|
ID of the task this event belongs to. |
seq |
int
|
Sequence number for ordering events. |
Source code in src/zap_ai/streaming/events.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | |
ThinkingEvent¶
Emitted when the LLM starts processing an iteration.
zap_ai.streaming.ThinkingEvent
dataclass
¶
Bases: StreamEvent
Emitted when the LLM starts reasoning/generating.
Attributes:
| Name | Type | Description |
|---|---|---|
iteration |
int
|
Current agentic loop iteration number. |
Source code in src/zap_ai/streaming/events.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | |
ToolCallEvent¶
Emitted when the agent calls a tool.
zap_ai.streaming.ToolCallEvent
dataclass
¶
Bases: StreamEvent
Emitted when a tool call is about to be executed.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Tool name being called. |
arguments |
dict[str, Any]
|
Tool call arguments. |
phrase |
str
|
Human-readable description of the tool call. |
tool_call_id |
str
|
Unique identifier for this tool call. |
Source code in src/zap_ai/streaming/events.py
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | |
ToolResultEvent¶
Emitted when a tool execution completes.
zap_ai.streaming.ToolResultEvent
dataclass
¶
Bases: StreamEvent
Emitted when a tool call completes.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Tool name that was called. |
result |
str
|
Tool execution result (may be truncated). |
tool_call_id |
str
|
Unique identifier matching the ToolCallEvent. |
success |
bool
|
Whether the tool executed successfully. |
Source code in src/zap_ai/streaming/events.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | |
TokenEvent¶
Reserved for future token-level streaming.
zap_ai.streaming.TokenEvent
dataclass
¶
Bases: StreamEvent
Emitted for individual tokens during streaming (Phase 2 only).
Attributes:
| Name | Type | Description |
|---|---|---|
token |
str
|
The token text. |
index |
int
|
Token index in the current generation. |
Source code in src/zap_ai/streaming/events.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | |
CompletedEvent¶
Emitted when the task completes successfully.
zap_ai.streaming.CompletedEvent
dataclass
¶
Bases: StreamEvent
Emitted when the task completes successfully.
Attributes:
| Name | Type | Description |
|---|---|---|
result |
str
|
Final result string from the agent. |
Source code in src/zap_ai/streaming/events.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | |
ErrorEvent¶
Emitted when the task fails.
zap_ai.streaming.ErrorEvent
dataclass
¶
Bases: StreamEvent
Emitted when the task fails.
Attributes:
| Name | Type | Description |
|---|---|---|
error |
str
|
Error message describing the failure. |
Source code in src/zap_ai/streaming/events.py
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | |
Utility Functions¶
generate_tool_phrase¶
zap_ai.streaming.generate_tool_phrase(tool_name, description, arguments)
¶
Generate a human-readable phrase for a tool call.
Converts tool descriptions to present participle form and substitutes argument values for a user-friendly status message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_name
|
str
|
Name of the tool being called. |
required |
description
|
str | None
|
Tool description from MCP registry. |
required |
arguments
|
dict[str, Any]
|
Arguments being passed to the tool. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Human-readable phrase like "Getting weather for London..." |
Example
generate_tool_phrase("get_weather", "Get weather for a city", {"city": "London"}) "Getting weather for London..."
Source code in src/zap_ai/streaming/events.py
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | |
parse_event¶
zap_ai.streaming.parse_event(event_data, task_id)
¶
Parse a raw event dictionary into a typed Event object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
dict[str, Any]
|
Raw event data from workflow query. |
required |
task_id
|
str
|
ID of the task this event belongs to. |
required |
Returns:
| Type | Description |
|---|---|
Event
|
Typed event object. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If event type is unknown. |
Source code in src/zap_ai/streaming/events.py
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 | |