strands.types.streaming
¶
Streaming-related type definitions for the SDK.
These types are modeled after the Bedrock API.
- Bedrock docs: https://docs.aws.amazon.com/bedrock/latest/APIReference/API_Types_Amazon_Bedrock_Runtime.html
CitationLocation = Union[DocumentCharLocationDict, DocumentPageLocationDict, DocumentChunkLocationDict, SearchResultLocationDict, WebLocationDict]
module-attribute
¶
Role = Literal['user', 'assistant']
module-attribute
¶
Role of a message sender.
- "user": Messages from the user to the assistant
- "assistant": Messages from the assistant to the user
StopReason = Literal['content_filtered', 'end_turn', 'guardrail_intervened', 'interrupt', 'max_tokens', 'stop_sequence', 'tool_use']
module-attribute
¶
Reason for the model ending its response generation.
- "content_filtered": Content was filtered due to policy violation
- "end_turn": Normal completion of the response
- "guardrail_intervened": Guardrail system intervened
- "interrupt": Agent was interrupted for human input
- "max_tokens": Maximum token limit reached
- "stop_sequence": Stop sequence encountered
- "tool_use": Model requested to use a tool
CitationSourceContentDelta
¶
Bases: TypedDict
Contains incremental updates to source content text during streaming.
Allows clients to build up the cited content progressively during streaming responses.
Attributes:
| Name | Type | Description |
|---|---|---|
text |
str
|
An incremental update to the text content from the source document that is being cited. |
Source code in strands/types/streaming.py
61 62 63 64 65 66 67 68 69 70 71 72 | |
CitationsDelta
¶
Bases: TypedDict
Contains incremental updates to citation information during streaming.
This allows clients to build up citation data progressively as the response is generated.
Attributes:
| Name | Type | Description |
|---|---|---|
location |
CitationLocation
|
Specifies the precise location within a source document where cited content can be found. This can include character-level positions, page numbers, or document chunks depending on the document type and indexing method. |
sourceContent |
list[CitationSourceContentDelta]
|
The specific content from the source document that was referenced or cited in the generated response. |
title |
str
|
The title or identifier of the source document being cited. |
Source code in strands/types/streaming.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | |
ContentBlockDelta
¶
Bases: TypedDict
A block of content in a streaming response.
Attributes:
| Name | Type | Description |
|---|---|---|
reasoningContent |
ReasoningContentBlockDelta
|
Contains content regarding the reasoning that is carried out by the model. |
text |
str
|
Text fragment being streamed. |
toolUse |
ContentBlockDeltaToolUse
|
Tool use input fragment being streamed. |
Source code in strands/types/streaming.py
110 111 112 113 114 115 116 117 118 119 120 121 122 | |
ContentBlockDeltaEvent
¶
Bases: TypedDict
Event containing a delta update for a content block in a streaming response.
Attributes:
| Name | Type | Description |
|---|---|---|
contentBlockIndex |
Optional[int]
|
Index of the content block within the message. This is optional to accommodate different model providers. |
delta |
ContentBlockDelta
|
The incremental content update for the content block. |
Source code in strands/types/streaming.py
125 126 127 128 129 130 131 132 133 134 135 | |
ContentBlockDeltaText
¶
Bases: TypedDict
Text content delta in a streaming response.
Attributes:
| Name | Type | Description |
|---|---|---|
text |
str
|
The text fragment being streamed. |
Source code in strands/types/streaming.py
41 42 43 44 45 46 47 48 | |
ContentBlockDeltaToolUse
¶
Bases: TypedDict
Tool use input delta in a streaming response.
Attributes:
| Name | Type | Description |
|---|---|---|
input |
str
|
The tool input fragment being streamed. |
Source code in strands/types/streaming.py
51 52 53 54 55 56 57 58 | |
ContentBlockStart
¶
Bases: TypedDict
Content block start information.
Attributes:
| Name | Type | Description |
|---|---|---|
toolUse |
Optional[ContentBlockStartToolUse]
|
Information about a tool that the model is requesting to use. |
Source code in strands/types/content.py
138 139 140 141 142 143 144 145 | |
ContentBlockStartEvent
¶
Bases: TypedDict
Event signaling the start of a content block in a streaming response.
Attributes:
| Name | Type | Description |
|---|---|---|
contentBlockIndex |
Optional[int]
|
Index of the content block within the message. This is optional to accommodate different model providers. |
start |
ContentBlockStart
|
Information about the content block being started. |
Source code in strands/types/streaming.py
28 29 30 31 32 33 34 35 36 37 38 | |
ContentBlockStopEvent
¶
Bases: TypedDict
Event signaling the end of a content block in a streaming response.
Attributes:
| Name | Type | Description |
|---|---|---|
contentBlockIndex |
Optional[int]
|
Index of the content block within the message. This is optional to accommodate different model providers. |
Source code in strands/types/streaming.py
138 139 140 141 142 143 144 145 146 | |
ExceptionEvent
¶
Bases: TypedDict
Base event for exceptions in a streaming response.
Attributes:
| Name | Type | Description |
|---|---|---|
message |
str
|
The error message describing what went wrong. |
Source code in strands/types/streaming.py
175 176 177 178 179 180 181 182 | |
MessageStartEvent
¶
Bases: TypedDict
Event signaling the start of a message in a streaming response.
Attributes:
| Name | Type | Description |
|---|---|---|
role |
Role
|
The role of the message sender (e.g., "assistant", "user"). |
Source code in strands/types/streaming.py
18 19 20 21 22 23 24 25 | |
MessageStopEvent
¶
Bases: TypedDict
Event signaling the end of a message in a streaming response.
Attributes:
| Name | Type | Description |
|---|---|---|
additionalModelResponseFields |
Optional[Union[dict, list, int, float, str, bool, None]]
|
Additional fields to include in model response. |
stopReason |
StopReason
|
The reason why the model stopped generating content. |
Source code in strands/types/streaming.py
149 150 151 152 153 154 155 156 157 158 | |
MetadataEvent
¶
Bases: TypedDict
Event containing metadata about the streaming response.
Attributes:
| Name | Type | Description |
|---|---|---|
metrics |
Metrics
|
Performance metrics related to the model invocation. |
trace |
Optional[Trace]
|
Trace information for debugging and monitoring. |
usage |
Usage
|
Resource usage information for the model invocation. |
Source code in strands/types/streaming.py
161 162 163 164 165 166 167 168 169 170 171 172 | |
Metrics
¶
Bases: TypedDict
Performance metrics for model interactions.
Attributes:
| Name | Type | Description |
|---|---|---|
latencyMs |
int
|
Latency of the model request in milliseconds. |
timeToFirstByteMs |
int
|
Latency from sending model request to first content chunk (contentBlockDelta or contentBlockStart) from the model in milliseconds. |
Source code in strands/types/event_loop.py
26 27 28 29 30 31 32 33 34 35 36 | |
ModelStreamErrorEvent
¶
Bases: ExceptionEvent
Event for model streaming errors.
Attributes:
| Name | Type | Description |
|---|---|---|
originalMessage |
str
|
The original error message from the model provider. |
originalStatusCode |
int
|
The HTTP status code returned by the model provider. |
Source code in strands/types/streaming.py
185 186 187 188 189 190 191 192 193 194 | |
ReasoningContentBlockDelta
¶
Bases: TypedDict
Delta for reasoning content block in a streaming response.
Attributes:
| Name | Type | Description |
|---|---|---|
redactedContent |
Optional[bytes]
|
The content in the reasoning that was encrypted by the model provider for safety reasons. |
signature |
Optional[str]
|
A token that verifies that the reasoning text was generated by the model. |
text |
Optional[str]
|
The reasoning that the model used to return the output. |
Source code in strands/types/streaming.py
96 97 98 99 100 101 102 103 104 105 106 107 | |
RedactContentEvent
¶
Bases: TypedDict
Event for redacting content.
Attributes:
| Name | Type | Description |
|---|---|---|
redactUserContentMessage |
Optional[str]
|
The string to overwrite the users input with. |
redactAssistantContentMessage |
Optional[str]
|
The string to overwrite the assistants output with. |
Source code in strands/types/streaming.py
197 198 199 200 201 202 203 204 205 206 207 | |
StreamEvent
¶
Bases: TypedDict
The messages output stream.
Attributes:
| Name | Type | Description |
|---|---|---|
contentBlockDelta |
ContentBlockDeltaEvent
|
Delta content for a content block. |
contentBlockStart |
ContentBlockStartEvent
|
Start of a content block. |
contentBlockStop |
ContentBlockStopEvent
|
End of a content block. |
internalServerException |
ExceptionEvent
|
Internal server error information. |
messageStart |
MessageStartEvent
|
Start of a message. |
messageStop |
MessageStopEvent
|
End of a message. |
metadata |
MetadataEvent
|
Metadata about the streaming response. |
modelStreamErrorException |
ModelStreamErrorEvent
|
Model streaming error information. |
serviceUnavailableException |
ExceptionEvent
|
Service unavailable error information. |
throttlingException |
ExceptionEvent
|
Throttling error information. |
validationException |
ExceptionEvent
|
Validation error information. |
Source code in strands/types/streaming.py
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | |
Trace
¶
Bases: TypedDict
A Top level guardrail trace object.
Attributes:
| Name | Type | Description |
|---|---|---|
guardrail |
GuardrailTrace
|
Trace information from guardrail processing. |
Source code in strands/types/guardrails.py
247 248 249 250 251 252 253 254 | |
Usage
¶
Bases: TypedDict
Token usage information for model interactions.
Attributes:
| Name | Type | Description |
|---|---|---|
inputTokens |
Required[int]
|
Number of tokens sent in the request to the model. |
outputTokens |
Required[int]
|
Number of tokens that the model generated for the request. |
totalTokens |
Required[int]
|
Total number of tokens (input + output). |
cacheReadInputTokens |
int
|
Number of tokens read from cache (optional). |
cacheWriteInputTokens |
int
|
Number of tokens written to cache (optional). |
Source code in strands/types/event_loop.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |