Title: PromptBuilder::parseMessage
Published: May 20, 2026

---

# PromptBuilder::parseMessage( mixed $input, WordPressAiClientMessagesEnumsMessageRoleEnum $defaultRole ): WordPressAiClientMessagesDTOMessage

## In this article

 * [Parameters](https://developer.wordpress.org/reference/classes/wordpress-aiclient-builders-promptbuilder/parsemessage/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/wordpress-aiclient-builders-promptbuilder/parsemessage/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wordpress-aiclient-builders-promptbuilder/parsemessage/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/classes/wordpress-aiclient-builders-promptbuilder/parsemessage/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wordpress-aiclient-builders-promptbuilder/parsemessage/?output_format=md#changelog)

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

This function’s access is marked private. This means it is not intended for use 
by plugin or theme developers, only by core. It is listed here for completeness.

Parses various input types into a Message with the given role.

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/wordpress-aiclient-builders-promptbuilder/parsemessage/?output_format=md#parameters)󠁿

 `$input`mixedrequired

The input to parse.

`$defaultRole`WordPressAiClientMessagesEnumsMessageRoleEnumrequired

The role for the message if not specified by input.

## 󠀁[Return](https://developer.wordpress.org/reference/classes/wordpress-aiclient-builders-promptbuilder/parsemessage/?output_format=md#return)󠁿

 WordPressAiClientMessagesDTOMessage The parsed message.

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

    ```php
    private function parseMessage($input, MessageRoleEnum $defaultRole): Message
    {
        // Handle Message input directly
        if ($input instanceof Message) {
            return $input;
        }
        // Handle single MessagePart
        if ($input instanceof MessagePart) {
            return new Message($defaultRole, [$input]);
        }
        // Handle string input
        if (is_string($input)) {
            if (trim($input) === '') {
                throw new InvalidArgumentException('Cannot create a message from an empty string.');
            }
            return new Message($defaultRole, [new MessagePart($input)]);
        }
        // Handle array input
        if (!is_array($input)) {
            throw new InvalidArgumentException('Input must be a string, MessagePart, MessagePartArrayShape, ' . 'a list of string|MessagePart|MessagePartArrayShape, or a Message instance.');
        }
        // Handle MessageArrayShape input
        if (Message::isArrayShape($input)) {
            return Message::fromArray($input);
        }
        // Check if it's a MessagePartArrayShape
        if (MessagePart::isArrayShape($input)) {
            return new Message($defaultRole, [MessagePart::fromArray($input)]);
        }
        // It should be a list of string|MessagePart|MessagePartArrayShape
        if (!array_is_list($input)) {
            throw new InvalidArgumentException('Array input must be a list array.');
        }
        // Empty array check
        if (empty($input)) {
            throw new InvalidArgumentException('Cannot create a message from an empty array.');
        }
        $parts = [];
        foreach ($input as $item) {
            if (is_string($item)) {
                $parts[] = new MessagePart($item);
            } elseif ($item instanceof MessagePart) {
                $parts[] = $item;
            } elseif (is_array($item) && MessagePart::isArrayShape($item)) {
                $parts[] = MessagePart::fromArray($item);
            } else {
                throw new InvalidArgumentException('Array items must be strings, MessagePart instances, or MessagePartArrayShape.');
            }
        }
        return new Message($defaultRole, $parts);
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/php-ai-client/src/builders/promptbuilder.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.0/src/wp-includes/php-ai-client/src/Builders/PromptBuilder.php#L1311)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.0/src/wp-includes/php-ai-client/src/Builders/PromptBuilder.php#L1311-L1361)

## 󠀁[Related](https://developer.wordpress.org/reference/classes/wordpress-aiclient-builders-promptbuilder/parsemessage/?output_format=md#related)󠁿

| Uses | Description | 
| [MessagePart::fromArray()](https://developer.wordpress.org/reference/classes/wordpress-aiclient-messages-dto-messagepart/fromarray/)`wp-includes/php-ai-client/src/Messages/DTO/MessagePart.php` |

{@inheritDoc}

  | 
| [MessagePart::__construct()](https://developer.wordpress.org/reference/classes/wordpress-aiclient-messages-dto-messagepart/__construct/)`wp-includes/php-ai-client/src/Messages/DTO/MessagePart.php` |

Constructor that accepts various content types and infers the message part type.

  | 
| [Message::fromArray()](https://developer.wordpress.org/reference/classes/wordpress-aiclient-messages-dto-message/fromarray/)`wp-includes/php-ai-client/src/Messages/DTO/Message.php` |

{@inheritDoc}

  | 
| [Message::__construct()](https://developer.wordpress.org/reference/classes/wordpress-aiclient-messages-dto-message/__construct/)`wp-includes/php-ai-client/src/Messages/DTO/Message.php` |

Constructor.

  |

| Used by | Description | 
| [PromptBuilder::__construct()](https://developer.wordpress.org/reference/classes/wordpress-aiclient-builders-promptbuilder/__construct/)`wp-includes/php-ai-client/src/Builders/PromptBuilder.php` |

Constructor.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wordpress-aiclient-builders-promptbuilder/parsemessage/?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-builders-promptbuilder%2Fparsemessage%2F)
before being able to contribute a note or feedback.