Skip to content

ant_ai.core.message

AnyMessage

AnyMessage = Annotated[
    Message | ToolCallMessage | ToolCallResultMessage,
    Field(discriminator="kind"),
]

Type alias for any message type in the system, discriminated by the _kind field.

Message pydantic-model

Bases: BaseModel

Generic message used in a conversation

Show JSON schema:
{
  "description": "Generic message used in a conversation",
  "properties": {
    "kind": {
      "const": "message",
      "default": "message",
      "title": "Kind",
      "type": "string"
    },
    "role": {
      "title": "Role",
      "type": "string"
    },
    "content": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "null"
        }
      ],
      "default": null,
      "title": "Content"
    },
    "metadata": {
      "additionalProperties": true,
      "title": "Metadata",
      "type": "object"
    }
  },
  "required": [
    "role"
  ],
  "title": "Message",
  "type": "object"
}

Fields:

Source code in src/ant_ai/core/message.py
 8
 9
10
11
12
13
14
15
16
17
18
class Message(BaseModel):
    """Generic message used in a conversation"""

    kind: Literal["message"] = Field(default="message")
    """Type of the message. Used to reconstruct, not passed to the LLM"""
    role: str
    """Role of the message sender (e.g. "user", "assistant")"""
    content: str | None = Field(default=None)
    """Text content of the message"""
    metadata: dict[str, Any] = Field(default_factory=dict)
    """Additional metadata associated with the message"""

kind pydantic-field

kind: Literal['message'] = 'message'

Type of the message. Used to reconstruct, not passed to the LLM

role pydantic-field

role: str

Role of the message sender (e.g. "user", "assistant")

content pydantic-field

content: str | None = None

Text content of the message

metadata pydantic-field

metadata: dict[str, Any]

Additional metadata associated with the message

MessageChunk pydantic-model

Bases: BaseModel

Represents partial output from an LLM during streaming. delta is the content's only the newly streamed text fragment, not the whole message so far.

Show JSON schema:
{
  "description": "Represents *partial output* from an LLM during streaming.\n`delta` is the content's only the newly streamed text fragment,\nnot the whole message so far.",
  "properties": {
    "role": {
      "title": "Role",
      "type": "string"
    },
    "delta": {
      "title": "Delta",
      "type": "string"
    },
    "metadata": {
      "additionalProperties": true,
      "title": "Metadata",
      "type": "object"
    }
  },
  "required": [
    "role",
    "delta"
  ],
  "title": "MessageChunk",
  "type": "object"
}

Fields:

  • role (str)
  • delta (str)
  • metadata (dict)
Source code in src/ant_ai/core/message.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class MessageChunk(BaseModel):
    """
    Represents *partial output* from an LLM during streaming.
    `delta` is the content's only the newly streamed text fragment,
    not the whole message so far.
    """

    role: str
    delta: str
    metadata: dict = Field(default_factory=dict)

    def merge(self, other: MessageChunk) -> MessageChunk:
        """Combines two MessageChunk into one

        Args:
            other (MessageChunk): The other message chunk to combine with

        Returns:
            MessageChunk: A combined chunk where this is before the other chunk
        """
        if self.role != other.role:
            raise ValueError("roles must match to merge")
        return MessageChunk(
            role=self.role,
            delta=self.delta + other.delta,
            metadata={**self.metadata, **other.metadata},
        )

    def to_message(self) -> Message:
        """Convert final chunk to a full message once streaming ends."""
        return Message(role=self.role, content=self.delta, metadata=self.metadata)

merge

merge(other: MessageChunk) -> MessageChunk

Combines two MessageChunk into one

Parameters:

Name Type Description Default
other MessageChunk

The other message chunk to combine with

required

Returns:

Name Type Description
MessageChunk MessageChunk

A combined chunk where this is before the other chunk

Source code in src/ant_ai/core/message.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
def merge(self, other: MessageChunk) -> MessageChunk:
    """Combines two MessageChunk into one

    Args:
        other (MessageChunk): The other message chunk to combine with

    Returns:
        MessageChunk: A combined chunk where this is before the other chunk
    """
    if self.role != other.role:
        raise ValueError("roles must match to merge")
    return MessageChunk(
        role=self.role,
        delta=self.delta + other.delta,
        metadata={**self.metadata, **other.metadata},
    )

to_message

to_message() -> Message

Convert final chunk to a full message once streaming ends.

Source code in src/ant_ai/core/message.py
49
50
51
def to_message(self) -> Message:
    """Convert final chunk to a full message once streaming ends."""
    return Message(role=self.role, content=self.delta, metadata=self.metadata)

ToolCallMessage pydantic-model

Bases: Message

Message representing a tool call in a conversation

Show JSON schema:
{
  "$defs": {
    "ToolCall": {
      "description": "Single tool call object inside assistant.tool_calls (OpenAI schema).",
      "properties": {
        "id": {
          "title": "Id",
          "type": "string"
        },
        "type": {
          "default": "function",
          "title": "Type",
          "type": "string"
        },
        "function": {
          "$ref": "#/$defs/ToolFunction"
        }
      },
      "required": [
        "id",
        "function"
      ],
      "title": "ToolCall",
      "type": "object"
    },
    "ToolFunction": {
      "description": "Inner function payload for a tool call (OpenAI schema).",
      "properties": {
        "name": {
          "title": "Name",
          "type": "string"
        },
        "arguments": {
          "title": "Arguments",
          "type": "string"
        }
      },
      "required": [
        "name",
        "arguments"
      ],
      "title": "ToolFunction",
      "type": "object"
    }
  },
  "description": "Message representing a tool call in a conversation",
  "properties": {
    "kind": {
      "const": "tool_call",
      "default": "tool_call",
      "title": "Kind",
      "type": "string"
    },
    "role": {
      "default": "assistant",
      "title": "Role",
      "type": "string"
    },
    "content": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "null"
        }
      ],
      "default": null,
      "title": "Content"
    },
    "metadata": {
      "additionalProperties": true,
      "title": "Metadata",
      "type": "object"
    },
    "tool_calls": {
      "items": {
        "$ref": "#/$defs/ToolCall"
      },
      "title": "Tool Calls",
      "type": "array"
    }
  },
  "required": [
    "tool_calls"
  ],
  "title": "ToolCallMessage",
  "type": "object"
}

Fields:

  • content (str | None)
  • metadata (dict[str, Any])
  • kind (Literal['tool_call'])
  • role (str)
  • tool_calls (list[ToolCall])
Source code in src/ant_ai/core/message.py
54
55
56
57
58
59
class ToolCallMessage(Message):
    """Message representing a tool call in a conversation"""

    kind: Literal["tool_call"] = Field(default="tool_call")
    role: str = "assistant"
    tool_calls: list[ToolCall]

ToolCallResultMessage pydantic-model

Bases: Message

Message representing the result of a tool call in a conversation

Show JSON schema:
{
  "description": "Message representing the result of a tool call in a conversation",
  "properties": {
    "kind": {
      "const": "tool_call_result",
      "default": "tool_call_result",
      "title": "Kind",
      "type": "string"
    },
    "role": {
      "default": "tool",
      "title": "Role",
      "type": "string"
    },
    "content": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "null"
        }
      ],
      "default": null,
      "title": "Content"
    },
    "metadata": {
      "additionalProperties": true,
      "title": "Metadata",
      "type": "object"
    },
    "tool_call_id": {
      "title": "Tool Call Id",
      "type": "string"
    },
    "name": {
      "title": "Name",
      "type": "string"
    }
  },
  "required": [
    "tool_call_id",
    "name"
  ],
  "title": "ToolCallResultMessage",
  "type": "object"
}

Fields:

  • content (str | None)
  • metadata (dict[str, Any])
  • kind (Literal['tool_call_result'])
  • role (str)
  • tool_call_id (str)
  • name (str)
Source code in src/ant_ai/core/message.py
62
63
64
65
66
67
68
class ToolCallResultMessage(Message):
    """Message representing the result of a tool call in a conversation"""

    kind: Literal["tool_call_result"] = Field(default="tool_call_result")
    role: str = "tool"
    tool_call_id: str
    name: str

ToolFunction pydantic-model

Bases: BaseModel

Inner function payload for a tool call (OpenAI schema).

Show JSON schema:
{
  "description": "Inner function payload for a tool call (OpenAI schema).",
  "properties": {
    "name": {
      "title": "Name",
      "type": "string"
    },
    "arguments": {
      "title": "Arguments",
      "type": "string"
    }
  },
  "required": [
    "name",
    "arguments"
  ],
  "title": "ToolFunction",
  "type": "object"
}

Fields:

Source code in src/ant_ai/core/message.py
71
72
73
74
75
76
77
78
class ToolFunction(BaseModel):
    """Inner function payload for a tool call (OpenAI schema)."""

    name: str
    """Name of the tool called"""
    # OpenAI expects a JSON string, not a dict.
    arguments: str
    """Arguments for the tool"""

name pydantic-field

name: str

Name of the tool called

arguments pydantic-field

arguments: str

Arguments for the tool

ToolCall pydantic-model

Bases: BaseModel

Single tool call object inside assistant.tool_calls (OpenAI schema).

Show JSON schema:
{
  "$defs": {
    "ToolFunction": {
      "description": "Inner function payload for a tool call (OpenAI schema).",
      "properties": {
        "name": {
          "title": "Name",
          "type": "string"
        },
        "arguments": {
          "title": "Arguments",
          "type": "string"
        }
      },
      "required": [
        "name",
        "arguments"
      ],
      "title": "ToolFunction",
      "type": "object"
    }
  },
  "description": "Single tool call object inside assistant.tool_calls (OpenAI schema).",
  "properties": {
    "id": {
      "title": "Id",
      "type": "string"
    },
    "type": {
      "default": "function",
      "title": "Type",
      "type": "string"
    },
    "function": {
      "$ref": "#/$defs/ToolFunction"
    }
  },
  "required": [
    "id",
    "function"
  ],
  "title": "ToolCall",
  "type": "object"
}

Fields:

Source code in src/ant_ai/core/message.py
81
82
83
84
85
86
87
88
89
class ToolCall(BaseModel):
    """Single tool call object inside assistant.tool_calls (OpenAI schema)."""

    id: str
    """Unique ID for the tool call, is generated by the OpenAI compatible endpoint"""
    type: str = "function"
    """Type of the tool call"""
    function: ToolFunction
    """Tool called"""

id pydantic-field

id: str

Unique ID for the tool call, is generated by the OpenAI compatible endpoint

type pydantic-field

type: str = 'function'

Type of the tool call

function pydantic-field

function: ToolFunction

Tool called