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.

XAITextModel lives in the AiSdk\XAI\Models namespace and is the concrete text model implementation for xAI’s Grok family. It extends BaseModel and implements TextModelInterface, delegating HTTP work to the shared aisdk/openai-compatible helpers — ChatRequestBuilder serialises the outgoing request, ChatResponseParser deserialises blocking responses, and ChatStreamParser drives token-by-token streaming. The model catalog (resources/models.json) supplies capability data for every known Grok variant; unknown model IDs fall back gracefully rather than hard-failing.

Namespace

use AiSdk\XAI\Models\XAITextModel;
XAITextModel is typically obtained via XAI::model() rather than constructed directly. See the XAI facade reference for setup instructions.

Constructor

public function __construct(
    private readonly string $modelId,
    private readonly XAIOptions $options,
    private readonly ?ModelRegistry $registry = null,
)
ParameterTypeDescription
$modelIdstringThe Grok model identifier, e.g. 'grok-4' or 'grok-3-mini'.
$optionsXAIOptionsProvider configuration: base URL, API key, and SDK HTTP dependencies.
$registryModelRegistry|nullOptional runtime registry for custom capability overrides. Defaults to null.

Methods

provider(): string

Returns the provider name used throughout the SDK for namespacing metadata and capability lookups.
$model->provider(); // 'xai'

modelId(): string

Returns the model identifier that was supplied at construction time.
$model->modelId(); // e.g. 'grok-4'

capabilities(): array<int, Capability>

Returns the full list of Capability enum cases supported by this model. The method resolves the list in priority order:
  1. Model registry — if a ModelRegistry was injected and contains an entry for this (provider, modelId) pair, that definition’s capability list is used.
  2. Catalog — otherwise the bundled resources/models.json catalog is consulted.
$capabilities = XAI::model('grok-4')->capabilities();
// [Capability::TextGeneration, Capability::Streaming, Capability::ToolCalling, ...]
Grok-4 and Grok-3 model families expose: TextGeneration, Streaming, ToolCalling, StructuredOutput, Reasoning, TextInput, ImageInput.

capability(Capability $capability): CapabilitySupport

Resolves the support status for a single capability, walking the following chain until a definitive answer is found:
  1. Configured overrides — capability flags set on the XAIOptions instance.
  2. Model registry — result from the injected ModelRegistry, if present.
  3. Catalog — bundled resources/models.json.
  4. Unknown-model fallback — if the model ID is not present in the catalog and the queried capability is Capability::TextGeneration, the method returns CapabilitySupport::supported($capability, 'unknown-model-fallback') instead of reporting unsupported.
use AiSdk\Capability;

$support = XAI::model('grok-4')->capability(Capability::Reasoning);
$support->isSupported(); // true

generate(TextModelRequest $request): TextModelResponse

Sends a blocking chat completion request and returns a fully-resolved TextModelResponse. Internally:
  • Serialises the request with ChatRequestBuilder::build() with stream: false.
  • POSTs to {baseUrl}/chat/completions via the SDK HTTP runner.
  • Deserialises the JSON response with ChatResponseParser::parse().
// Via the fluent Generate builder (recommended)
$result = Generate::text('Hello')
    ->model(XAI::model('grok-4'))
    ->run();

echo $result->text;
// Provider metadata is keyed by provider name:
echo $result->providerMetadata['xai']['model']; // 'grok-4'
echo $result->usage->inputTokens;
ParameterTypeDescription
$requestTextModelRequestThe portable text model request object.
Returns TextModelResponse — contains text, usage, finishReason, and providerMetadata.

stream(TextModelRequest $request): Generator

Sends a streaming chat completion request and returns a PHP Generator that yields partial TextModelResponse chunks as they arrive from the server. Internally:
  • Serialises the request with ChatRequestBuilder::build() with stream: true.
  • Opens a streaming POST to {baseUrl}/chat/completions.
  • Pipes server-sent events through ChatStreamParser::parse(), which yields each delta chunk.
$stream = Generate::text('Tell me a story.')
    ->model(XAI::model('grok-4'))
    ->stream();

foreach ($stream as $chunk) {
    echo $chunk->text;
}
ParameterTypeDescription
$requestTextModelRequestThe portable text model request object.
Returns Generator — each iteration yields a partial TextModelResponse chunk.

Endpoint

POST {baseUrl}/chat/completions
Default base URL: https://api.x.ai/v1
Full default endpoint: https://api.x.ai/v1/chat/completions
The base URL can be overridden by passing a custom baseUrl in XAI::create() options.

Build docs developers (and LLMs) love