trait ParsesTextResponses

Traits

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 Anthropic response data.

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

Parse the Anthropic response data into a single step response.

buildStepResponse(array $content, Provider $provider, string $model, TextUsage $usage, FinishReason $finishReason, bool $structured)

Build a single step response from Anthropic content blocks.

string
extractText(array $content)

Extract the text content from Anthropic content blocks.

string
textSeparator(string $before, string $after)

Get the separator between text written before a tool block and text written after it.

bool
isToolBlock(string $type)

Determine if the given block type is a server tool call or its result.

bool
isProviderToolResultBlock(string $type)

Determine if the given block type is a provider tool result.

string
extractReasoning(array $content)

Extract the reasoning text from Anthropic content blocks.

array
extractProviderToolCalls(array $content)

Extract the server tool use and result blocks, each keyed by the tool use it belongs to.

array
extractToolCalls(array $content)

Extract tool calls from Anthropic content blocks.

extractCitations(array $content)

Extract citations from Anthropic content blocks.

extractUsage(array $data)

Extract usage data from the Anthropic response.

FinishReason
extractFinishReason(array $data)

Extract and map the finish reason from the Anthropic response.

array
extractStructuredOutput(array $content)

Extract structured output from the synthetic tool call.

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 30
protected void validateTextResponse(array $data)

Validate the Anthropic response data.

Parameters

array $data

Return Value

void

Exceptions

AiException

at line 44
protected StepResponse parseTextResponse(array $data, Provider $provider, bool $structured)

Parse the Anthropic response data into a single step response.

Parameters

array $data
Provider $provider
bool $structured

Return Value

StepResponse

at line 62
protected StepResponse buildStepResponse(array $content, Provider $provider, string $model, TextUsage $usage, FinishReason $finishReason, bool $structured)

Build a single step response from Anthropic content blocks.

Parameters

array $content
Provider $provider
string $model
TextUsage $usage
FinishReason $finishReason
bool $structured

Return Value

StepResponse

at line 114
protected string extractText(array $content)

Extract the text content from Anthropic content blocks.

Parameters

array $content

Return Value

string

at line 143
protected string textSeparator(string $before, string $after)

Get the separator between text written before a tool block and text written after it.

Parameters

string $before
string $after

Return Value

string

at line 151
protected bool isToolBlock(string $type)

Determine if the given block type is a server tool call or its result.

Parameters

string $type

Return Value

bool

at line 159
protected bool isProviderToolResultBlock(string $type)

Determine if the given block type is a provider tool result.

Parameters

string $type

Return Value

bool

at line 167
protected string extractReasoning(array $content)

Extract the reasoning text from Anthropic content blocks.

Parameters

array $content

Return Value

string

at line 179
protected array extractProviderToolCalls(array $content)

Extract the server tool use and result blocks, each keyed by the tool use it belongs to.

Parameters

array $content

Return Value

array

at line 192
protected array extractToolCalls(array $content)

Extract tool calls from Anthropic content blocks.

Parameters

array $content

Return Value

array

at line 207
protected Collection extractCitations(array $content)

Extract citations from Anthropic content blocks.

Parameters

array $content

Return Value

Collection

at line 252
protected TextUsage extractUsage(array $data)

Extract usage data from the Anthropic response.

Parameters

array $data

Return Value

TextUsage

at line 271
protected FinishReason extractFinishReason(array $data)

Extract and map the finish reason from the Anthropic response.

Parameters

array $data

Return Value

FinishReason

at line 286
protected array extractStructuredOutput(array $content)

Extract structured output from the synthetic tool call.

Parameters

array $content

Return Value

array