Chat Messages (🧪Beta)
albert.collections.chat_messages.ChatMessageCollection
Manage the message turns within an "Ask Albert" chat session (🧪 Beta).
A chat message (ChatMessage) is one turn, or
turn component, of a conversation with Albert's AI assistant. Messages always
belong to a parent session
(ChatSession, managed by
ChatSessionCollection), so every
method here takes a session_id. Within a session a message is addressed by
the pair (source_request_id, sequence), and
ChatComponentType distinguishes components
that share a request.
This is an async collection accessed as client.chat_messages on an
AsyncAlbert client.
Beta Feature!
Please do not use in production or without explicit guidance from Albert. You might otherwise have a bad experience. This feature currently falls outside of the Albert support contract, but we'd love your feedback!
Example
from albert import AsyncAlbert
from albert.resources.chats import ChatMessage, ChatComponentType, ChatUserType, ChatRole
async with AsyncAlbert() as client:
await client.chat_messages.create(
message=ChatMessage(
parent_id="<session id>",
component_type=ChatComponentType.TEXT,
user_type=ChatUserType.USER,
role=ChatRole.USER,
content="What raw materials contain titanium dioxide?",
)
)
async for message in client.chat_messages.get_all(session_id="<session id>"):
print(message.sequence, message.content)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
AsyncAlbertSession
|
The authenticated Albert async session used for API calls. |
required |
Methods:
| Name | Description |
|---|---|
create |
Add a message to a chat session. |
get_by_id |
Get a single message by its request ID and sequence. |
get_all |
Iterate over the messages in a session, oldest first. |
update |
Update the content of a message. |
delete |
Delete a message from a session. |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
AsyncAlbertSession
|
The authenticated Albert async session used for API calls. |
required |
Source code in src/albert/collections/chat_messages.py
create
create(*, message: ChatMessage) -> ChatMessage
Add a message to a chat session.
Example
from albert import AsyncAlbert
from albert.resources.chats import ChatMessage, ChatComponentType, ChatUserType, ChatRole
async with AsyncAlbert() as client:
message = await client.chat_messages.create(
message=ChatMessage(
parent_id="...",
component_type=ChatComponentType.TEXT,
user_type=ChatUserType.USER,
role=ChatRole.USER,
content="What raw materials contain titanium dioxide?",
)
)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
ChatMessage
|
The message to create. |
required |
Returns:
| Type | Description |
|---|---|
ChatMessage
|
The created message. |
Notes
The create response does not currently echo the message content, so the
returned object's content may be None. Use get_by_id to read
the stored message back in full.
Source code in src/albert/collections/chat_messages.py
get_by_id
get_by_id(
*,
session_id: str,
source_request_id: str,
sequence: str,
component_type: ChatComponentType | None = None,
) -> ChatMessage
Get a single message by its request ID and sequence.
Example
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_id
|
str
|
The ID of the parent |
required |
source_request_id
|
str
|
The request trace identifier of the message. |
required |
sequence
|
str
|
The zero-padded sequence of the message within the session
(e.g. |
required |
component_type
|
ChatComponentType | None
|
Narrow the lookup to a single
|
None
|
Returns:
| Type | Description |
|---|---|
ChatMessage
|
The fully populated message. |
Source code in src/albert/collections/chat_messages.py
get_all
get_all(
*, session_id: str, max_items: int | None = None
) -> AsyncIterator[ChatMessage]
Iterate over the messages in a session, oldest first.
Transparently pages through results, yielding one message at a time.
Returns the paginator directly so has_more remains available.
Example
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_id
|
str
|
The ID of the |
required |
max_items
|
int | None
|
Maximum number of messages to yield in total. If |
None
|
Returns:
| Type | Description |
|---|---|
AsyncIterator[ChatMessage]
|
Messages in the session, oldest first. |
Source code in src/albert/collections/chat_messages.py
update
update(
*,
session_id: str,
source_request_id: str,
sequence: str,
content: str | dict,
) -> ChatMessage
Update the content of a message.
Example
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_id
|
str
|
The ID of the parent |
required |
source_request_id
|
str
|
The request trace identifier of the message. |
required |
sequence
|
str
|
The zero-padded sequence of the message within the session
(e.g. |
required |
content
|
str | dict
|
The new content for the message. Use a string for text components or an
object for richer components, matching the message's
|
required |
Returns:
| Type | Description |
|---|---|
ChatMessage
|
The updated message. |
Notes
The following fields can be updated: content.
Source code in src/albert/collections/chat_messages.py
delete
Delete a message from a session.
Example
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_id
|
str
|
The ID of the parent |
required |
source_request_id
|
str
|
The request trace identifier of the message. |
required |
sequence
|
str
|
The zero-padded sequence of the message within the session
(e.g. |
required |
Returns:
| Type | Description |
|---|---|
None
|
|