trading_agent.agent.messages
¶
The messages of a conversation, in a form that does not depend on any model.
Each backend converts these into its own wire format, so the same conversation can move from Gemma to Qwen to Claude when the router changes backend.
Typical usage example:
call = ToolCall.new('get_time', {}) message = ConversationMessage.assistant('', [call])
Role
¶
Bases: StrEnum
Who a message comes from.
ToolCall
dataclass
¶
One request from the model to run a tool.
Attributes:
| Name | Type | Description |
|---|---|---|
call_id |
str
|
An identifier that matches the call with its result. Ollama gives none, so it is created here when missing. |
name |
str
|
The tool's name. |
arguments |
dict[str, Any]
|
The arguments, as the model wrote them. |
new
classmethod
¶
Creates a tool call with a fresh identifier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The tool's name. |
required |
arguments
|
dict[str, Any]
|
The arguments. |
required |
Returns:
| Type | Description |
|---|---|
ToolCall
|
The new call. |
Source code in src/trading_agent/agent/messages.py
MessageImage
dataclass
¶
A picture sent to the model, with a user message or a tool's result.
Attributes:
| Name | Type | Description |
|---|---|---|
media_type |
str
|
The picture's media type, image/jpeg or image/png. |
data |
bytes
|
The encoded picture. |
base64_data
¶
The picture in base64, as the model APIs take it.
Returns:
| Type | Description |
|---|---|
str
|
The base64 text. |
ConversationMessage
dataclass
¶
ConversationMessage(role, text, tool_calls=(), tool_call_id=None, tool_name=None, is_error=False, image=None, images=(), reasoning='')
One message in a conversation.
Attributes:
| Name | Type | Description |
|---|---|---|
role |
Role
|
Who the message comes from. |
text |
str
|
The message's text; may be empty for an assistant message that only calls tools. |
tool_calls |
tuple[ToolCall, ...]
|
The tools an assistant message asks to run. |
tool_call_id |
str | None
|
For a tool message, the identifier of the call it answers. |
tool_name |
str | None
|
For a tool message, the name of the tool that ran. |
is_error |
bool
|
For a tool message, whether the tool failed. |
image |
MessageImage | None
|
For a tool message, a picture the tool returned, or None. |
images |
tuple[MessageImage, ...]
|
For a user message, the pictures sent with it. |
reasoning |
str
|
For an assistant message, the model's visible thinking, which some servers, such as DeepSeek's, require to be sent back with the message. |
user
classmethod
¶
Creates a user message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
What the user, or a trigger, says. |
required |
images
|
tuple[MessageImage, ...]
|
The pictures sent with it. |
()
|
Returns:
| Type | Description |
|---|---|
ConversationMessage
|
The message. |
Source code in src/trading_agent/agent/messages.py
assistant
classmethod
¶
Creates an assistant message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The model's text. |
required |
tool_calls
|
list[ToolCall]
|
The tools it asks to run. |
required |
reasoning
|
str
|
The model's visible thinking, or empty. |
''
|
Returns:
| Type | Description |
|---|---|
ConversationMessage
|
The message. |
Source code in src/trading_agent/agent/messages.py
tool_result
classmethod
¶
Creates the message that carries a tool's result back to the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
call
|
ToolCall
|
The call being answered. |
required |
text
|
str
|
The tool's output. |
required |
is_error
|
bool
|
Whether the tool failed. |
required |
image
|
MessageImage | None
|
A picture the tool returned, or None. |
None
|
Returns:
| Type | Description |
|---|---|
ConversationMessage
|
The message. |