Title: Message
Published: May 20, 2026

---

# class Message {}

## In this article

 * [Description](https://developer.wordpress.org/reference/classes/wordpress-aiclient-messages-dto-message/?output_format=md#description)
 * [Methods](https://developer.wordpress.org/reference/classes/wordpress-aiclient-messages-dto-message/?output_format=md#methods)
 * [Source](https://developer.wordpress.org/reference/classes/wordpress-aiclient-messages-dto-message/?output_format=md#source)
 * [Changelog](https://developer.wordpress.org/reference/classes/wordpress-aiclient-messages-dto-message/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/classes/wordpress-aiclient-messages-dto-message/?output_format=md#wp--skip-link--target)

Represents a message in an AI conversation.

## 󠀁[Description](https://developer.wordpress.org/reference/classes/wordpress-aiclient-messages-dto-message/?output_format=md#description)󠁿

Messages are the fundamental unit of communication with AI models, containing a 
role and one or more parts with different content types.

## 󠀁[Methods](https://developer.wordpress.org/reference/classes/wordpress-aiclient-messages-dto-message/?output_format=md#methods)󠁿

| Name | Description | 
| [Message::__clone](https://developer.wordpress.org/reference/classes/wordpress-aiclient-messages-dto-message/__clone/) | Performs a deep clone of the message. | 
| [Message::__construct](https://developer.wordpress.org/reference/classes/wordpress-aiclient-messages-dto-message/__construct/) | Constructor. | 
| [Message::fromArray](https://developer.wordpress.org/reference/classes/wordpress-aiclient-messages-dto-message/fromarray/) | {@inheritDoc} | 
| [Message::getJsonSchema](https://developer.wordpress.org/reference/classes/wordpress-aiclient-messages-dto-message/getjsonschema/) | {@inheritDoc} | 
| [Message::getParts](https://developer.wordpress.org/reference/classes/wordpress-aiclient-messages-dto-message/getparts/) | Gets the message parts. | 
| [Message::getRole](https://developer.wordpress.org/reference/classes/wordpress-aiclient-messages-dto-message/getrole/) | Gets the role of the message sender. | 
| [Message::toArray](https://developer.wordpress.org/reference/classes/wordpress-aiclient-messages-dto-message/toarray/) | {@inheritDoc} | 
| [Message::validateParts](https://developer.wordpress.org/reference/classes/wordpress-aiclient-messages-dto-message/validateparts/) | Validates that the message parts are appropriate for the message role. | 
| [Message::withPart](https://developer.wordpress.org/reference/classes/wordpress-aiclient-messages-dto-message/withpart/) | Returns a new instance with the given part appended. |

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wordpress-aiclient-messages-dto-message/?output_format=md#source)󠁿

    ```php
    class Message extends AbstractDataTransferObject
    {
        public const KEY_ROLE = 'role';
        public const KEY_PARTS = 'parts';
        /**
         * @var MessageRoleEnum The role of the message sender.
         */
        protected MessageRoleEnum $role;
        /**
         * @var MessagePart[] The parts that make up this message.
         */
        protected array $parts;
        /**
         * Constructor.
         *
         * @since 0.1.0
         *
         * @param MessageRoleEnum $role The role of the message sender.
         * @param MessagePart[] $parts The parts that make up this message.
         * @throws InvalidArgumentException If parts contain invalid content for the role.
         */
        public function __construct(MessageRoleEnum $role, array $parts)
        {
            $this->role = $role;
            $this->parts = $parts;
            $this->validateParts();
        }
        /**
         * Gets the role of the message sender.
         *
         * @since 0.1.0
         *
         * @return MessageRoleEnum The role.
         */
        public function getRole(): MessageRoleEnum
        {
            return $this->role;
        }
        /**
         * Gets the message parts.
         *
         * @since 0.1.0
         *
         * @return MessagePart[] The message parts.
         */
        public function getParts(): array
        {
            return $this->parts;
        }
        /**
         * Returns a new instance with the given part appended.
         *
         * @since 0.1.0
         *
         * @param MessagePart $part The part to append.
         * @return Message A new instance with the part appended.
         * @throws InvalidArgumentException If the part is invalid for the role.
         */
        public function withPart(\WordPress\AiClient\Messages\DTO\MessagePart $part): \WordPress\AiClient\Messages\DTO\Message
        {
            $newParts = $this->parts;
            $newParts[] = $part;
            return new \WordPress\AiClient\Messages\DTO\Message($this->role, $newParts);
        }
        /**
         * Validates that the message parts are appropriate for the message role.
         *
         * @since 0.1.0
         *
         * @return void
         * @throws InvalidArgumentException If validation fails.
         */
        private function validateParts(): void
        {
            foreach ($this->parts as $part) {
                $type = $part->getType();
                if ($this->role->isUser() && $type->isFunctionCall()) {
                    throw new InvalidArgumentException('User messages cannot contain function calls.');
                }
                if ($this->role->isModel() && $type->isFunctionResponse()) {
                    throw new InvalidArgumentException('Model messages cannot contain function responses.');
                }
            }
        }
        /**
         * {@inheritDoc}
         *
         * @since 0.1.0
         */
        public static function getJsonSchema(): array
        {
            return ['type' => 'object', 'properties' => [self::KEY_ROLE => ['type' => 'string', 'enum' => MessageRoleEnum::getValues(), 'description' => 'The role of the message sender.'], self::KEY_PARTS => ['type' => 'array', 'items' => \WordPress\AiClient\Messages\DTO\MessagePart::getJsonSchema(), 'minItems' => 1, 'description' => 'The parts that make up this message.']], 'required' => [self::KEY_ROLE, self::KEY_PARTS]];
        }
        /**
         * {@inheritDoc}
         *
         * @since 0.1.0
         *
         * @return MessageArrayShape
         */
        public function toArray(): array
        {
            return [self::KEY_ROLE => $this->role->value, self::KEY_PARTS => array_map(function (\WordPress\AiClient\Messages\DTO\MessagePart $part) {
                return $part->toArray();
            }, $this->parts)];
        }
        /**
         * {@inheritDoc}
         *
         * @since 0.1.0
         *
         * @return self The specific message class based on the role.
         */
        final public static function fromArray(array $array): self
        {
            static::validateFromArrayData($array, [self::KEY_ROLE, self::KEY_PARTS]);
            $role = MessageRoleEnum::from($array[self::KEY_ROLE]);
            $partsData = $array[self::KEY_PARTS];
            $parts = array_map(function (array $partData) {
                return \WordPress\AiClient\Messages\DTO\MessagePart::fromArray($partData);
            }, $partsData);
            // Determine which concrete class to instantiate based on role
            if ($role->isUser()) {
                return new \WordPress\AiClient\Messages\DTO\UserMessage($parts);
            } elseif ($role->isModel()) {
                return new \WordPress\AiClient\Messages\DTO\ModelMessage($parts);
            } else {
                // Only USER and MODEL roles are supported
                throw new InvalidArgumentException('Invalid message role: ' . $role->value);
            }
        }
        /**
         * Performs a deep clone of the message.
         *
         * This method ensures that message part objects are cloned to prevent
         * modifications to the cloned message from affecting the original.
         *
         * @since 0.4.2
         */
        public function __clone()
        {
            $clonedParts = [];
            foreach ($this->parts as $part) {
                $clonedParts[] = clone $part;
            }
            $this->parts = $clonedParts;
        }
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/php-ai-client/src/messages/dto/message.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.0/src/wp-includes/php-ai-client/src/Messages/DTO/Message.php#L26)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.0/src/wp-includes/php-ai-client/src/Messages/DTO/Message.php#L26-L173)

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wordpress-aiclient-messages-dto-message/?output_format=md#changelog)󠁿

| Version | Description | 
| [0.1.0](https://developer.wordpress.org/reference/since/0.1.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwordpress-aiclient-messages-dto-message%2F)
before being able to contribute a note or feedback.