liveBook · Manning

ChatRole

As explained in Build a Multi-Agent System (from Scratch)

ChatRole is an enumeration of the valid roles for messages in an LLM conversation.

ChatRole is a string-backed enumeration that limits a ChatMessage’s role to one of four named values. It identifies whether a message provides context, comes from the user or LLM, or carries a tool result. section 3.1.1 section 3.1

The roles distinguish these message functions:

Tool-call exchange

A user input becomes a USER message; an LLM response becomes an ASSISTANT message. When the response carries a ToolCall, its content is an empty string. A ToolCallResult can be converted into a TOOL message whose content serializes the result. section 3.1.1 section 3.1

Listing 3.2 Implementing ChatMessage and ChatRole
# llm_agents_from_scratch/data_structures/llm.py
from pydantic import BaseModel
from typing_extensions import Self
from llm_agents_from_scratch.data_structures.tool import ToolCall
 
… #A
 
class ChatRole(str, Enum):
    """Roles for chat messages."""
 
    USER = "user"
    ASSISTANT = "assistant"
    SYSTEM = "system"
    TOOL = "tool"
 
class ChatMessage(BaseModel):
    """The chat message data model.
 
    Attributes:
        role: The role of the message.
        content: The content of the message.
        tool_calls: Tool calls associated with the message.
    """
 
    model_config = ConfigDict(arbitrary_types_allowed=True)
    role: ChatRole
    content: str
    tool_calls: list[ToolCall] | None = None #B
 
    @classmethod #C
    def from_tool_call_result(
        cls,
        tool_call_result: ToolCallResult
    ) -> Self:
        """Create a ChatMessage from a ToolCallResult."""
        return cls(
            role=ChatRole.TOOL, #D
            content=tool_call_result.model_dump_json(indent=4), #E
        )

Inspect the ChatRole and ChatMessage implementation, including the role values and tool-result conversion method. section 3.1.1

How Build a Multi-Agent System (from Scratch) covers it

How deep it goes
Defines four enum values and describes their message functions and tool-result flow.
Examples
Uses user and assistant messages in chat, including an assistant ToolCall and a serialized tool result.
What you can do after
Understand how to label user, assistant, system, and tool messages in chat interactions.
How it connects
Connects ChatRole to ChatMessage, chat history, LLM responses, and tool results.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Multi-Agent System (from Scratch). The section links point to the parts of the book it draws on.