Documentation Index
Fetch the complete documentation index at: https://mintlify.com/phpaisdk/openai-compatible/llms.txt
Use this file to discover all available pages before exploring further.
ChatMessageConverter transforms portable AiSdk\Message objects — including plain text, tool result, assistant tool-call, and multimodal messages — into the messages array required by the OpenAI /chat/completions endpoint. It is called internally by ChatRequestBuilder::build() and never needs to be instantiated directly; all methods are static.
Namespace
Methods
convert()
Message objects into the OpenAI wire-format messages array. When $system is provided and non-empty, a {"role": "system", "content": "..."} entry is prepended as the first element.
An ordered array of
AiSdk\Message objects representing the conversation history. At least one message is required by the API.An optional system prompt. When non-null and non-empty, it is prepended to the output as
{"role": "system", "content": $system} before all other messages.array<int, array<string, mixed>> — the fully serialized messages array ready for inclusion in a /chat/completions request body.
Role handling
Tool messages (Message::ROLE_TOOL)
Tool result messages are serialized with the tool role and carry the call identifier and function name alongside the plain-text output:
Assistant messages with tool calls
When an assistantMessage carries one or more ToolCall objects, the converter emits an assistant entry that includes a tool_calls array. The content field is null when the assistant produced no accompanying text:
arguments is always JSON-encoded from the ToolCall::$arguments array.
All other messages (single-text shorthand)
Foruser, assistant, and other roles whose content resolves to a single text part, the converter applies a flattening shorthand — rather than wrapping the content in a one-element array, it inlines the string directly:
Content type mappings
EachAiSdk\Content part on a message is converted to an OpenAI content-part object according to its type:
Content type | OpenAI part structure |
|---|---|
Content::TYPE_TEXT | {"type": "text", "text": "<string>"} |
Content::TYPE_IMAGE (URL source) | {"type": "image_url", "image_url": {"url": "<url>"}} |
Content::TYPE_IMAGE (base64 source) | {"type": "image_url", "image_url": {"url": "<data URI>"}} |
Content::TYPE_AUDIO | {"type": "input_audio", "input_audio": {"data": "<base64>", "format": "mp3|wav|ogg"}} |
Content::TYPE_FILE | {"type": "file", "file": {"filename": "<name>", "file_data": "<url or data URI>"}} |
Passing a
Content type not listed above (e.g. a custom type) throws AiSdk\Exceptions\InvalidArgumentException with a message identifying the unsupported type.Audio format mapping
Theformat field of input_audio parts is derived from the content’s MIME type:
| MIME type | format value |
|---|---|
audio/mpeg, audio/mp3 | mp3 |
audio/wav, audio/x-wav | wav |
audio/ogg | ogg |
anything else / null | wav (default) |