Skip to main content

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.

MapsFinishReason normalizes the raw finish_reason string returned by OpenAI-compatible APIs into the portable AiSdk\FinishReason enum used across the SDK. It is called by both ChatResponseParser (for non-streaming responses) and ChatStreamParser (for the final chunk of a streaming response) to ensure a consistent finish-reason value regardless of which provider issued the response.

Namespace

AiSdk\OpenAICompatible\Converters\MapsFinishReason

Methods

map()

public static function map(?string $reason): FinishReason
Accepts the raw finish_reason string from an API response and returns the corresponding AiSdk\FinishReason enum case. The input is nullable because some streaming chunks carry a null finish reason before the final chunk arrives; those are treated identically to 'stop'.
reason
string|null
The raw finish_reason value from the API response. May be null for intermediate streaming chunks or when the field is absent from the payload.
Returns AiSdk\FinishReason — one of the portable enum cases described in the mapping table below.

Finish reason mapping

Input finish_reasonFinishReason valueNotes
'stop'FinishReason::StopNormal end of generation
'end_turn'FinishReason::StopAnthropic-style alias for stop
'length'FinishReason::LengthMax output tokens reached
'max_tokens'FinishReason::LengthProvider alias for length
'tool_calls'FinishReason::ToolCallsModel called one or more tools
'function_call'FinishReason::ToolCallsLegacy function-call alias
'content_filter'FinishReason::ContentFilterOutput blocked by safety policy
nullFinishReason::StopTreated as a normal stop
any other valueFinishReason::UnknownForward-compatible fallback
The end_turn and function_call aliases ensure that providers using slightly different naming conventions (such as those following an Anthropic-style API) are handled transparently without changes to calling code.

Example

use AiSdk\FinishReason;
use AiSdk\OpenAICompatible\Converters\MapsFinishReason;

MapsFinishReason::map('stop');          // => FinishReason::Stop
MapsFinishReason::map('end_turn');      // => FinishReason::Stop
MapsFinishReason::map('length');        // => FinishReason::Length
MapsFinishReason::map('max_tokens');    // => FinishReason::Length
MapsFinishReason::map('tool_calls');    // => FinishReason::ToolCalls
MapsFinishReason::map('function_call'); // => FinishReason::ToolCalls
MapsFinishReason::map('content_filter'); // => FinishReason::ContentFilter
MapsFinishReason::map(null);            // => FinishReason::Stop
MapsFinishReason::map('some_new_reason'); // => FinishReason::Unknown
In practice, the value appears inside the parsed response or stream state:
use AiSdk\OpenAICompatible\ChatResponseParser;

$payload = [
    'choices' => [[
        'message'       => ['content' => 'Sure, here you go.'],
        'finish_reason' => 'stop',
    ]],
    'usage' => ['prompt_tokens' => 12, 'completion_tokens' => 7],
];

$response = ChatResponseParser::parse($payload, 'openai');

// $response->finishReason === FinishReason::Stop

Build docs developers (and LLMs) love