Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Cohere ai Cohere python ChatMessage Model

From Leeroopedia
Knowledge Sources
Domains SDK, Chat
Last Updated 2026-02-15 14:00 GMT

Overview

The ChatMessage class is a Pydantic model representing a single message in the chat history for the legacy Cohere chat API.

Description

The ChatMessage model represents a single message in the chat history, excluding the current user turn. It contains a message field with the text content and an optional tool_calls field with a list of ToolCall objects. The role of the message sender (CHATBOT, SYSTEM, or USER) is determined by context in the chat history. The docstring notes that the chat_history parameter should not be used for SYSTEM messages in most cases; instead, the preamble parameter should be used to add a system-level message at the beginning of a conversation. This model extends UncheckedBaseModel and is auto-generated from the Cohere API definition by Fern.

Usage

Use this model when constructing the chat_history parameter for the legacy Cohere chat API. Each ChatMessage represents a prior turn in the conversation, enabling multi-turn dialogue by providing context from previous exchanges.

Code Reference

Source Location

Signature

class ChatMessage(UncheckedBaseModel):
    message: str = pydantic.Field()
    tool_calls: typing.Optional[typing.List[ToolCall]] = None

Import

from cohere.types import ChatMessage

I/O Contract

Field Type Required Default Description
message str Yes - Contents of the chat message.
tool_calls Optional[List[ToolCall]] No None List of tool calls associated with this message.

Usage Examples

import cohere
from cohere.types import ChatMessage

client = cohere.Client(api_key="YOUR_API_KEY")

# Build chat history with ChatMessage objects
chat_history = [
    ChatMessage(message="Hello, how can I help you?"),
    ChatMessage(message="What is the weather like today?"),
    ChatMessage(message="I can help you check the weather. Where are you located?"),
]

# Use chat history in a legacy chat API call
response = client.chat(
    model="command-r-plus",
    message="I am in San Francisco.",
    chat_history=chat_history,
)

print(response.text)

# ChatMessage with tool calls
from cohere.types import ToolCall

msg_with_tools = ChatMessage(
    message="Let me check the weather for you.",
    tool_calls=[
        ToolCall(
            name="get_weather",
            parameters={"location": "San Francisco"},
        ),
    ],
)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment