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

---

# MessagePart::fromArray( $array )

## In this article

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

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

{@inheritDoc}

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

    ```php
    public static function fromArray(array $array): self
    {
        if (isset($array[self::KEY_CHANNEL])) {
            $channel = MessagePartChannelEnum::from($array[self::KEY_CHANNEL]);
        } else {
            $channel = null;
        }
        $thoughtSignature = $array[self::KEY_THOUGHT_SIGNATURE] ?? null;
        // Check which properties are set to determine how to construct the MessagePart
        if (isset($array[self::KEY_TEXT])) {
            return new self($array[self::KEY_TEXT], $channel, $thoughtSignature);
        } elseif (isset($array[self::KEY_FILE])) {
            return new self(File::fromArray($array[self::KEY_FILE]), $channel, $thoughtSignature);
        } elseif (isset($array[self::KEY_FUNCTION_CALL])) {
            return new self(FunctionCall::fromArray($array[self::KEY_FUNCTION_CALL]), $channel, $thoughtSignature);
        } elseif (isset($array[self::KEY_FUNCTION_RESPONSE])) {
            return new self(FunctionResponse::fromArray($array[self::KEY_FUNCTION_RESPONSE]), $channel, $thoughtSignature);
        } else {
            throw new InvalidArgumentException('MessagePart requires one of: text, file, functionCall, or functionResponse.');
        }
    }
    ```

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

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

| Uses | Description | 
| [FunctionResponse::fromArray()](https://developer.wordpress.org/reference/classes/wordpress-aiclient-tools-dto-functionresponse/fromarray/)`wp-includes/php-ai-client/src/Tools/DTO/FunctionResponse.php` |

{@inheritDoc}

  | 
| [FunctionCall::fromArray()](https://developer.wordpress.org/reference/classes/wordpress-aiclient-tools-dto-functioncall/fromarray/)`wp-includes/php-ai-client/src/Tools/DTO/FunctionCall.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.

  | 
| [File::fromArray()](https://developer.wordpress.org/reference/classes/wordpress-aiclient-files-dto-file/fromarray/)`wp-includes/php-ai-client/src/Files/DTO/File.php` |

{@inheritDoc}

  |

| Used by | Description | 
| [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}

  | 
| [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.

  | 
| [MessageBuilder::__construct()](https://developer.wordpress.org/reference/classes/wordpress-aiclient-builders-messagebuilder/__construct/)`wp-includes/php-ai-client/src/Builders/MessageBuilder.php` |

Constructor.

  |

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