trait ParsesTextResponses

Traits

Constants

private REQUEST_STEP_TYPES

The step types Gemini derives from our own request rather than the model's turn.

Methods

array
decodeStructuredOutput(string|null $text)

Decode a structured output payload, tolerating markdown code fences.

static string
joinReasoning(iterable $blocks)

Join reasoning blocks, separating each block with a blank line.

void
validateTextResponse(array $data)

Validate the Gemini response data.

parseTextResponse(array $data, Provider $provider, string $model, bool $structured)

Parse the Gemini response data into a single step response.

array
replayableSteps(array $steps)

Get the steps that must be replayed verbatim on the next request.

array
extractProviderToolCalls(array $steps)

Extract the provider-executed tool steps, such as search and code execution.

bool
isProviderToolStep(array $step)

Determine if the given step was executed by Gemini rather than by us.

bool
isThinkingStep(array $step)

Determine if a step carries the model's reasoning.

string
extractReasoning(array $steps)

Extract the reasoning text from the response steps.

string
extractText(array $steps)

Extract the answer text from the model output steps.

string
textOf(array $step)

Concatenate the text blocks of the given step, which thought steps carry as a summary.

array
extractFunctionCallSteps(array $steps)

Extract the steps carrying function calls.

array
mapToolCalls(array $functionCallSteps, string|null $thoughtSignature = null)

Map function call steps to ToolCall DTOs.

string|null
thoughtSignature(array $steps)

Get the signature of the turn's last thought step, which the next request must replay beside its calls.

array
decodeArguments(mixed $arguments)

Normalize function call arguments, which stream as a partial JSON string.

extractCitations(array $steps)

Extract citations from the annotations Gemini attaches to its text blocks.

extractUsage(array $data)

Extract usage data from the response.

extractImageUsage(array $data)

Extract usage data from an image generation response.

int|null
modalityTokens(array $details, string $modality)

Get the token count Gemini reported for the given modality.

FinishReason
extractFinishReason(array $data, array $functionCallSteps)

Extract and map the finish reason from the Gemini response.

bool
wasBlocked(array $errors)

Determine if the interaction failed because Gemini withheld the content.

Details

protected array decodeStructuredOutput(string|null $text)

Decode a structured output payload, tolerating markdown code fences.

Parameters

string|null $text

Return Value

array

in JoinsReasoning at line 16
static protected string joinReasoning(iterable $blocks)

Join reasoning blocks, separating each block with a blank line.

Parameters

iterable $blocks

Return Value

string

at line 37
protected void validateTextResponse(array $data)

Validate the Gemini response data.

Parameters

array $data

Return Value

void

Exceptions

AiException

at line 62
protected StepResponse parseTextResponse(array $data, Provider $provider, string $model, bool $structured)

Parse the Gemini response data into a single step response.

Parameters

array $data
Provider $provider
string $model
bool $structured

Return Value

StepResponse

at line 89
protected array replayableSteps(array $steps)

Get the steps that must be replayed verbatim on the next request.

Parameters

array $steps

Return Value

array

at line 102
protected array extractProviderToolCalls(array $steps)

Extract the provider-executed tool steps, such as search and code execution.

Parameters

array $steps

Return Value

array

at line 117
protected bool isProviderToolStep(array $step)

Determine if the given step was executed by Gemini rather than by us.

Parameters

array $step

Return Value

bool

at line 128
protected bool isThinkingStep(array $step)

Determine if a step carries the model's reasoning.

Parameters

array $step

Return Value

bool

at line 136
protected string extractReasoning(array $steps)

Extract the reasoning text from the response steps.

Parameters

array $steps

Return Value

string

at line 147
protected string extractText(array $steps)

Extract the answer text from the model output steps.

Parameters

array $steps

Return Value

string

at line 158
protected string textOf(array $step)

Concatenate the text blocks of the given step, which thought steps carry as a summary.

Parameters

array $step

Return Value

string

at line 172
protected array extractFunctionCallSteps(array $steps)

Extract the steps carrying function calls.

Parameters

array $steps

Return Value

array

at line 184
protected array mapToolCalls(array $functionCallSteps, string|null $thoughtSignature = null)

Map function call steps to ToolCall DTOs.

Parameters

array $functionCallSteps
string|null $thoughtSignature

Return Value

array

at line 202
protected string|null thoughtSignature(array $steps)

Get the signature of the turn's last thought step, which the next request must replay beside its calls.

Parameters

array $steps

Return Value

string|null

at line 215
protected array decodeArguments(mixed $arguments)

Normalize function call arguments, which stream as a partial JSON string.

Parameters

mixed $arguments

Return Value

array

at line 229
protected Collection extractCitations(array $steps)

Extract citations from the annotations Gemini attaches to its text blocks.

Parameters

array $steps

Return Value

Collection

at line 256
protected TextUsage extractUsage(array $data)

Extract usage data from the response.

Parameters

array $data

Return Value

TextUsage

at line 274
protected ImageUsage extractImageUsage(array $data)

Extract usage data from an image generation response.

Parameters

array $data

Return Value

ImageUsage

at line 294
protected int|null modalityTokens(array $details, string $modality)

Get the token count Gemini reported for the given modality.

Parameters

array $details
string $modality

Return Value

int|null

at line 308
protected FinishReason extractFinishReason(array $data, array $functionCallSteps)

Extract and map the finish reason from the Gemini response.

Parameters

array $data
array $functionCallSteps

Return Value

FinishReason

at line 332
protected bool wasBlocked(array $errors)

Determine if the interaction failed because Gemini withheld the content.

Parameters

array $errors

Return Value

bool