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.
ChatRequestBuilder is a stateless, final utility class in the aisdk/openai-compatible package that converts a portable TextModelRequest object into a raw associative array ready to send as the JSON body of an OpenAI-compatible /chat/completions HTTP request. It is called by every provider adapter that speaks the OpenAI wire format — you will typically invoke it inside your own provider’s doChat() method rather than calling it directly from application code.
Namespace
Method: build()
$request that has an OpenAI-compatible wire representation and returns the complete request body as a PHP array. The returned array can be passed directly to a JSON-encoding HTTP client.
Parameters
The model identifier that will be placed verbatim in the
model field of the request body, e.g. "gpt-4o" or "o3".The provider namespace string, e.g.
"openai". Used exclusively to read $request->providerOptionsFor($providerName)['raw'] when merging raw override keys. It must match the key your application uses when constructing providerOptions.The portable, provider-agnostic request object.
ChatRequestBuilder reads the following properties:| Property | Wire field |
|---|---|
$request->messages | messages (via ChatMessageConverter) |
$request->system | Prepended as a system role message |
$request->temperature | temperature |
$request->maxTokens | max_tokens |
$request->topP | top_p (omitted when null) |
$request->tools | tools + tool_choice (omitted when empty) |
$request->toolChoice | tool_choice |
$request->output | response_format (see below) |
$request->reasoning | reasoning_effort (see below) |
$request->providerOptionsFor($providerName)['raw'] | merged last via array_replace() |
When
true, the body will include "stream": true and "stream_options": {"include_usage": true}. When false, "stream": false is set and stream_options is omitted entirely.Return value
Returnsarray<string, mixed>. The shape of the returned array depends on which optional features are active:
Usage example
response_format logic
The response_format key is set only when $request->output is an Output instance. Two cases are handled:
Structured output with a named schema — when $request->output->kind === Output::KIND_OBJECT and $request->output->schema is an AiSdk\Schema instance, the builder emits the json_schema variant with strict: true:
$request->output->kind === Output::KIND_OBJECT but no AiSdk\Schema is attached, the builder emits the simpler variant:
Output::kind value results in response_format being omitted entirely.
Reasoning behavior
OpenAI-compatible adapters support a single reasoning path: normalized effort levels.Token budget reasoning (
Reasoning::budget(int $tokens)) is not supported and will always throw. Use Reasoning::effort() or the raw escape hatch instead.Reasoning factory | Behaviour |
|---|---|
Reasoning::effort('low' | 'medium' | 'high') | Sets reasoning_effort in the request body |
Reasoning::budget(int $tokens) | Throws AiSdk\Exceptions\InvalidArgumentException |
Raw escape hatch
When you need to send a field that the portable API does not yet model — such asseed, a custom logit_bias, or a preview parameter — you can pass it through providerOptions[$providerName]['raw']. This array is merged into the body last using array_replace(), so it can overwrite any key the builder already set.