Skip to main content

Documentation Index

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

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

TextResult is the value object returned after a text generation request completes. It aggregates the model’s generated text, optional reasoning output, any tool interactions, structured output, token usage statistics, and the raw provider response — everything you need to inspect or act on a completed generation.

Properties

text
string
required
The primary text content generated by the model. This is the fully assembled response string for the turn.
reasoning
string | null
The model’s internal reasoning or extended thinking text, when a reasoning-capable model was asked to reason before answering. null if reasoning was not requested or the model did not emit a reasoning trace.
output
mixed
The decoded structured output object when the request was made with ->output(). null when structured output was not requested. The shape depends on the schema passed to the generation call.
toolCalls
array<int, ToolCall>
All tool calls the model made during this generation turn, in the order they were emitted. An empty array when no tools were invoked.
toolResults
array<int, ToolResult>
The results returned from executing each tool call, in the same positional order as toolCalls. An empty array when no tools were executed.
finishReason
FinishReason
required
The reason the model stopped generating. Defaults to FinishReason::Stop.
usage
Usage
required
Token consumption statistics for this request.
rawResponse
array<string, mixed>
The unmodified response payload returned by the provider’s API. Useful for debugging or accessing provider-specific fields that are not surfaced in the structured properties above.
providerMetadata
array<string, mixed>
Provider-specific metadata keyed by provider name. The contents vary across providers and may include additional billing details, safety scores, or model-specific annotations.

Code example

use AiSdk\FinishReason;

$result = $ai->text('Explain PHP fibers in one paragraph.');

// Access the generated text
echo $result->text;

// Check why the model stopped
if ($result->finishReason === FinishReason::Length) {
    echo "Response was truncated — consider raising max_tokens.";
}

// Inspect reasoning output (reasoning models only)
if ($result->reasoning !== null) {
    echo "Model reasoning:\n" . $result->reasoning;
}

// Read token usage
echo sprintf(
    "Tokens — input: %d, output: %d, total: %d\n",
    $result->usage->inputTokens,
    $result->usage->outputTokens,
    $result->usage->totalTokens,
);

// Iterate tool calls and their results
foreach ($result->toolCalls as $call) {
    echo "Tool called: {$call->name} (id: {$call->id})\n";
    echo "Arguments: " . json_encode($call->arguments) . "\n";
}

foreach ($result->toolResults as $toolResult) {
    echo "Result for {$toolResult->toolName}: " . json_encode($toolResult->output) . "\n";
}

// Access structured output (when using ->output())
if ($result->output !== null) {
    $structured = $result->output;
    echo "Structured field: " . $structured->someField;
}

// Inspect raw provider payload
$raw = $result->rawResponse;

Build docs developers (and LLMs) love