Title: Message::fromArray
Published: May 20, 2026

---

# Message::fromArray( $array ): self

## In this article

 * [Return](https://developer.wordpress.org/reference/classes/wordpress-aiclient-messages-dto-message/fromarray/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wordpress-aiclient-messages-dto-message/fromarray/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/classes/wordpress-aiclient-messages-dto-message/fromarray/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wordpress-aiclient-messages-dto-message/fromarray/?output_format=md#changelog)

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

{@inheritDoc}

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

 self The specific message class based on the role.

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

    ```php
    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);
        }
    }
    ```

[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#L139)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.0/src/wp-includes/php-ai-client/src/Messages/DTO/Message.php#L139-L156)

## 󠀁[Related](https://developer.wordpress.org/reference/classes/wordpress-aiclient-messages-dto-message/fromarray/?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}

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

Constructor.

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

Constructor.

  |

| Used by | Description | 
| [Candidate::fromArray()](https://developer.wordpress.org/reference/classes/wordpress-aiclient-results-dto-candidate/fromarray/)`wp-includes/php-ai-client/src/Results/DTO/Candidate.php` |

{@inheritDoc}

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

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

  |

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