Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/phpaisdk/xai/llms.txt

Use this file to discover all available pages before exploring further.

The aisdk/core abstraction covers the capabilities common to all providers — prompts, tools, schemas, reasoning effort, and so on. When you need to use an xAI-specific parameter that has no portable equivalent, providerOptions() gives you a direct escape hatch: values placed under the 'raw' key are merged into the JSON request body that is sent to the xAI API, after all portable fields have been set.

The providerOptions() Method

Call ->providerOptions() on any Generate builder, passing the provider name 'xai' as the first argument and an options array as the second. Put any parameters you want forwarded verbatim inside the 'raw' key:
->providerOptions('xai', [
    'raw' => [
        'some_xai_parameter' => 'value',
    ],
])
The 'xai' namespace ensures these options are only applied when the request is processed by the xAI provider and are silently ignored by any other provider your code may be configured to use.

Example: Live Search Parameters

xAI’s Grok models support a search_parameters field that controls real-time web search behaviour. Pass it through providerOptions() to enable or configure live search on a request:
use AiSdk\Generate;
use AiSdk\XAI;

$result = Generate::text('What happened in tech news today?')
    ->model(XAI::model('grok-4'))
    ->providerOptions('xai', [
        'raw' => ['search_parameters' => ['mode' => 'auto']],
    ])
    ->run();
With 'mode' => 'auto', Grok decides whether a web search is necessary to answer the question. Refer to the xAI API documentation for the full list of search_parameters options.

Example: Image Resolution

When generating images, xAI-specific generation parameters such as resolution can be forwarded the same way:
Generate::image()
    ->model(XAI::image('grok-imagine-image-quality'))
    ->prompt('A futuristic skyline')
    ->providerOptions('xai', ['raw' => ['resolution' => '2k']])
    ->run();
The resolution key is merged directly into the /v1/images/generations request body, alongside the portable fields (model, prompt, n, response_format, etc.) that the SDK sets automatically.

How It Works

Internally, the ChatRequestBuilder (and its image counterpart) checks $request->providerOptions('xai')['raw'] ?? [] and merges those entries into the final request array before it is JSON-encoded and dispatched. This means:
  • Raw keys override any portable field with the same name if there is a collision.
  • The merged body is sent as-is to the xAI API — no further transformation is applied.
  • Other providers in the same application are unaffected because the options are scoped to 'xai'.
Raw parameters are not validated by the SDK. If you pass an unrecognised or incorrectly typed parameter, the xAI API will return an error at runtime. Always refer to the xAI API documentation to confirm the correct parameter names, value types, and which endpoints support them.

Build docs developers (and LLMs) love