Message
Message and content block primitives for agent graphs.
This module defines the core message representation, multimodal content blocks, token usage tracking, and utility functions for agent graph communication.
Classes:
| Name | Description |
|---|---|
TokenUsages |
Tracks token usage statistics for a message or model response. |
MediaRef |
Reference to media content (image/audio/video/document/data). |
AnnotationRef |
Reference to annotation metadata. |
Message |
Represents a message in a conversation, including content, role, metadata, and token usage. |
Functions:
| Name | Description |
|---|---|
generate_id |
Generates a message or tool call ID based on DI context and type. |
Attributes:
| Name | Type | Description |
|---|---|---|
logger |
|
Attributes¶
Classes¶
Message
¶
Bases: BaseModel
Represents a message in a conversation, including content, role, metadata, and token usage.
Attributes:
| Name | Type | Description |
|---|---|---|
message_id |
str | int
|
Unique identifier for the message. |
role |
Literal['user', 'assistant', 'system', 'tool']
|
The role of the message sender. |
content |
list[ContentBlock]
|
The message content blocks. |
delta |
bool
|
Indicates if this is a delta/partial message. |
tools_calls |
list[dict[str, Any]] | None
|
Tool call information, if any. |
reasoning |
str | None
|
Reasoning or explanation, if any. |
timestamp |
datetime | None
|
Timestamp of the message. |
metadata |
dict[str, Any]
|
Additional metadata. |
usages |
TokenUsages | None
|
Token usage statistics. |
raw |
dict[str, Any] | None
|
Raw data, if any. |
Example
msg = Message(message_id="abc123", role="user", content=[TextBlock(text="Hello!")])
Methods:
| Name | Description |
|---|---|
attach_media |
Append a media block to the content. |
text |
Best-effort text extraction from content blocks. |
text_message |
Create a Message instance from plain text. |
tool_message |
Create a tool message, optionally marking it as an error. |
Source code in agentflow/state/message.py
129 130 131 132 133 134 135 136 137 138 139 140 141 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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 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 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 | |
Attributes¶
message_id
class-attribute
instance-attribute
¶
message_id = Field(default_factory=lambda: generate_id(None))
timestamp
class-attribute
instance-attribute
¶
timestamp = Field(default_factory=lambda: timestamp())
Functions¶
attach_media
¶
attach_media(media, as_type)
Append a media block to the content.
If content was text, creates a block list. Supports image, audio, video, and document types.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
MediaRef
|
Reference to media content. |
required |
|
Literal['image', 'audio', 'video', 'document']
|
Type of media block to append. |
required |
Returns:
| Type | Description |
|---|---|
None
|
None |
Raises:
| Type | Description |
|---|---|
ValueError
|
If an unsupported media type is provided. |
Example
msg.attach_media(media_ref, as_type="image")
Source code in agentflow/state/message.py
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 | |
text
¶
text()
Best-effort text extraction from content blocks.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Concatenated text from TextBlock and ToolResultBlock outputs. |
Example
msg.text() 'Hello!Result text.'
Source code in agentflow/state/message.py
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | |
text_message
classmethod
¶
text_message(content, role='user', message_id=None)
Create a Message instance from plain text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
The message content. |
required |
|
Literal['user', 'assistant', 'system', 'tool']
|
The role of the sender. |
'user'
|
|
str | None
|
Optional message ID. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Message |
Message
|
The created Message instance. |
Example
Message.text_message("Hello!", role="user")
Source code in agentflow/state/message.py
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | |
tool_message
classmethod
¶
tool_message(content, message_id=None, meta=None)
Create a tool message, optionally marking it as an error.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
list[ContentBlock]
|
The message content blocks. |
required |
|
str | None
|
Optional message ID. |
None
|
|
dict[str, Any] | None
|
Optional metadata. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Message |
Message
|
The created tool message instance. |
Example
Message.tool_message([ToolResultBlock(...)], message_id="tool1")
Source code in agentflow/state/message.py
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | |
TokenUsages
¶
Bases: BaseModel
Tracks token usage statistics for a message or model response.
Attributes:
| Name | Type | Description |
|---|---|---|
completion_tokens |
int
|
Number of completion tokens used. |
prompt_tokens |
int
|
Number of prompt tokens used. |
total_tokens |
int
|
Total tokens used. |
reasoning_tokens |
int
|
Reasoning tokens used (optional). |
cache_creation_input_tokens |
int
|
Cache creation input tokens (optional). |
cache_read_input_tokens |
int
|
Cache read input tokens (optional). |
image_tokens |
int | None
|
Image tokens for multimodal models (optional). |
audio_tokens |
int | None
|
Audio tokens for multimodal models (optional). |
Example
usage = TokenUsages(completion_tokens=10, prompt_tokens=20, total_tokens=30)
Source code in agentflow/state/message.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | |
Attributes¶
Functions¶
generate_id
¶
generate_id(default_id)
Generate a message or tool call ID based on DI context and type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str | int | None
|
Default ID to use if provided and matches type. |
required |
Returns:
| Type | Description |
|---|---|
str | int
|
str | int: Generated or provided ID, type determined by DI context. |
Example
generate_id("abc123") 'abc123' generate_id(None) 'a-uuid-string'
Source code in agentflow/state/message.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 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 | |