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 ChatMessageV2 Types

From Leeroopedia
Metadata
Source Repo Cohere Python SDK
Domains NLP, Chat_API, Message_Formatting
Last Updated 2026-02-15 14:00 GMT

Overview

Concrete Pydantic models for constructing typed chat messages in the Cohere V2 API.

Description

ChatMessageV2 is a discriminated union of four message types (UserChatMessageV2, AssistantChatMessageV2, SystemChatMessageV2, ToolChatMessageV2), discriminated on the role field. Each type is a Pydantic model extending UncheckedBaseModel with role-specific fields. ChatMessages is typed as List[ChatMessageV2].

Usage

Import the specific message types and construct a list to pass as the messages parameter to V2Client.chat() or V2Client.chat_stream().

Code Reference

  • Source Location: Repository cohere-ai/cohere-python, File src/cohere/types/chat_message_v2.py, Lines L1-94
  • Signature:
class UserChatMessageV2(UncheckedBaseModel):
    role: typing.Literal["user"] = "user"
    content: UserMessageV2Content

class AssistantChatMessageV2(UncheckedBaseModel):
    role: typing.Literal["assistant"] = "assistant"
    tool_calls: typing.Optional[typing.List[ToolCallV2]] = None
    tool_plan: typing.Optional[str] = None
    content: typing.Optional[AssistantMessageV2Content] = None
    citations: typing.Optional[typing.List[Citation]] = None

class SystemChatMessageV2(UncheckedBaseModel):
    role: typing.Literal["system"] = "system"
    content: SystemMessageV2Content

class ToolChatMessageV2(UncheckedBaseModel):
    role: typing.Literal["tool"] = "tool"
    tool_call_id: str
    content: ToolMessageV2Content

ChatMessageV2 = Annotated[
    Union[UserChatMessageV2, AssistantChatMessageV2, SystemChatMessageV2, ToolChatMessageV2],
    UnionMetadata(discriminant="role"),
]
  • Import: from cohere import UserChatMessageV2, AssistantChatMessageV2, SystemChatMessageV2, ToolChatMessageV2

I/O Contract

Name Type Required Description
Inputs
role Literal string Yes Discriminant field ("user", "assistant", "system", "tool")
content Varies by role Yes (except assistant) Message content (string or content blocks)
tool_calls Optional[List[ToolCallV2]] No Only on AssistantChatMessageV2
tool_call_id str Yes (tool only) Links tool result to its call
Outputs
Return value ChatMessages (List[ChatMessageV2]) Message list ready for chat API

Usage Examples

from cohere import ClientV2, UserChatMessageV2, SystemChatMessageV2

messages = [
    SystemChatMessageV2(content="You are a helpful assistant."),
    UserChatMessageV2(content="What is machine learning?"),
]

client = ClientV2()
response = client.chat(model="command-a-03-2025", messages=messages)

Related Pages

Page Connections

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