Title: MessagePart::toArray
Published: May 20, 2026

---

# MessagePart::toArray(): WordPressAiClientMessagesDTOMessagePartArrayShape

## In this article

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

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

{@inheritDoc}

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

 WordPressAiClientMessagesDTOMessagePartArrayShape

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

    ```php
    public function toArray(): array
    {
        $data = [self::KEY_CHANNEL => $this->channel->value, self::KEY_TYPE => $this->type->value];
        if ($this->text !== null) {
            $data[self::KEY_TEXT] = $this->text;
        } elseif ($this->file !== null) {
            $data[self::KEY_FILE] = $this->file->toArray();
        } elseif ($this->functionCall !== null) {
            $data[self::KEY_FUNCTION_CALL] = $this->functionCall->toArray();
        } elseif ($this->functionResponse !== null) {
            $data[self::KEY_FUNCTION_RESPONSE] = $this->functionResponse->toArray();
        } else {
            throw new RuntimeException('MessagePart requires one of: text, file, functionCall, or functionResponse. ' . 'This should not be a possible condition.');
        }
        if ($this->thoughtSignature !== null) {
            $data[self::KEY_THOUGHT_SIGNATURE] = $this->thoughtSignature;
        }
        return $data;
    }
    ```

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

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