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.
ChatResponseParser is a stateless, final utility class in the aisdk/openai-compatible package responsible for converting a raw decoded /chat/completions response payload — an array<string, mixed> obtained by JSON-decoding the HTTP response body — into a fully typed TextModelResponse. It extracts text content, tool-call descriptors, token usage, and provider-specific metadata, normalizing them all into the portable AiSdk response model so that application code never has to inspect raw OpenAI JSON.
Namespace
Method: parse()
/chat/completions response object into a TextModelResponse. Only choices[0] is examined; additional choices are ignored.
Parameters
The fully decoded response array from the
/chat/completions endpoint. This is the value you get from json_decode($responseBody, true). The parser reads the following keys:| Key path | Used for |
|---|---|
choices[0].message.content | TextPart |
choices[0].message.tool_calls[] | ToolCallPart list |
choices[0].finish_reason | FinishReason mapping |
choices[0].index | providerMetadata.choice_index |
usage.prompt_tokens | usage.inputTokens |
usage.completion_tokens | usage.outputTokens |
usage.total_tokens | usage.totalTokens |
usage.completion_tokens_details.reasoning_tokens | usage.reasoningTokens |
usage.prompt_tokens_details.cached_tokens | usage.cachedInputTokens |
id, object, created, model, system_fingerprint, service_tier | providerMetadata |
The provider namespace string, e.g.
"openai". This becomes the key under which provider-specific metadata is nested in TextModelResponse::$providerMetadata.Return value: TextModelResponse
An ordered array of typed response parts.
TextPart appears first (when present), followed by one ToolCallPart per entry in choices[0].message.tool_calls.A
FinishReason enum value mapped from choices[0].finish_reason using MapsFinishReason::map(). The mapping is:| Raw string | FinishReason |
|---|---|
"stop", "end_turn" | FinishReason::Stop |
"length", "max_tokens" | FinishReason::Length |
"tool_calls", "function_call" | FinishReason::ToolCalls |
"content_filter" | FinishReason::ContentFilter |
null / missing | FinishReason::Stop |
| anything else | FinishReason::Unknown |
Token consumption figures. Falls back to
Usage::empty() when the usage key is absent from the payload.Usage property | Source field |
|---|---|
inputTokens | usage.prompt_tokens |
outputTokens | usage.completion_tokens |
totalTokens | usage.total_tokens (nullable) |
reasoningTokens | usage.completion_tokens_details.reasoning_tokens (nullable) |
cachedInputTokens | usage.prompt_tokens_details.cached_tokens (nullable) |
The original
$payload array passed to parse(), stored verbatim for debugging or low-level access.Provider-specific metadata keyed by
Only keys that are present in the payload are included; absent keys are not set to
$providerName. The inner array contains response-level and choice-level fields:| Key | Source |
|---|---|
id | $payload['id'] |
object | $payload['object'] |
created | $payload['created'] |
model | $payload['model'] |
system_fingerprint | $payload['system_fingerprint'] |
service_tier | $payload['service_tier'] |
choice_index | choices[0]['index'] |
choice_finish_reason | choices[0]['finish_reason'] |
null.Part types
TextPart
Created when choices[0].message.content is a non-empty string. The content string is stored verbatim; no trimming or transformation is applied.
ToolCallPart
Created once per entry in choices[0].message.tool_calls. Each ToolCallPart has:
| Property | Source |
|---|---|
id | tool_calls[n].id |
name | tool_calls[n].function.name |
arguments | JSON-decoded tool_calls[n].function.arguments (always an array) |
arguments is always decoded from the JSON string in the payload. If the arguments string is empty, it is treated as {} and decoded to an empty array.Usage example
The example below uses the exact payload from theChatWireFormatTest suite: