class AzureOpenAiGateway implements EmbeddingGateway, ImageGateway, StepTextGateway

Traits

Methods

array
buildTextRequestBody(Provider $provider, string $model, string|null $instructions, array $messages, array $tools, array|null $schema, TextGenerationOptions|null $options)

Build the request body for the OpenAI Responses API.

array
buildContinuationBody(Provider $provider, string $model, string $continuationToken, array $messages, array $tools, array|null $schema, TextGenerationOptions|null $options = null)

Build the request body for a stateful Responses API continuation.

array
extractToolResultsInput(array $messages)

Extract the latest tool results for a stateful continuation request.

array
mergeSharedResponsesRequestOptions(array $body, array $tools, array|null $schema, TextGenerationOptions|null $options, Provider $provider)

Merge shared Responses API options onto the given request body.

string|array
mapToolChoice(ToolChoice $choice)

Map a tool choice to the OpenAI Responses tool_choice shape.

bool
isStateless(Provider $provider)

Determine if OpenAI should receive full stateless conversation history.

bool
isReasoningModel(string $model)

Determine if the model supports encrypted reasoning content.

array
buildSchemaFormat(array $schema, bool $strict)

Build the text format options for structured output.

createClient(string $baseUrl, array $headers = [], array $configuredHeaders = [], int|null $timeout = 60, bool $throw = true)

Create an HTTP client with the provider's request options.

string
httpConnection(Provider $provider)

Select a boot-registered connection without retaining dynamic provider names.

client(Provider $provider, int|null $timeout = null)

Get an HTTP client for the Azure OpenAI v1-compatible API.

mixed
withErrorHandling(string $providerName, Closure $callback)

Execute a callback with failoverable error handling.

int|null
retryAfterSeconds(Throwable $exception)

Get the short Retry-After delay of a rate limited or overloaded response, if it has one.

bool
isInsufficientCredits(RequestException $exception)

Determine if the response is a 402, or a 429 insufficient credits or quota error rather than a rate limit.

array
overloadedStatusCodes()

Get the status codes that indicate a provider is transiently unavailable and the request should fail over.

array
insufficientCreditPatterns()

Get the patterns used to detect insufficient credits or quota errors.

array
insufficientCreditCodes()

Get the error codes or types that indicate insufficient credits or quota on a 429 response.

processTextStream(string $invocationId, Provider $provider, string $model, StreamInterface $streamBody)

Process streamed response events and return the completed step.

string
generateEventId()

Generate a lowercase UUID v7 for use as a stream event ID.

generateTextStep(TextProvider $provider, string $model, string|null $instructions, array $messages, array $tools, array|null $schema, TextGenerationOptions|null $options, int|null $timeout, StepContext $stepContext)

Generate text for a single Responses API step.

generateStreamStep(string $invocationId, TextProvider $provider, string $model, string|null $instructions, array $messages, array $tools, array|null $schema, TextGenerationOptions|null $options, int|null $timeout, StepContext $stepContext)

Stream text for a single Responses API step.

array
buildStepBody(TextProvider $provider, string $model, string|null $instructions, array $messages, array $tools, array|null $schema, TextGenerationOptions|null $options, StepContext $stepContext)

Build the request body for the current text generation step.

string
fallbackFilename(string|null $mimeType)

Derive a filename for a nameless document from its MIME type.

array
mapAttachments(Collection $attachments, Provider $provider)

Map the given Hypervel attachments to OpenAI content parts.

bool
isImage(UploadedFile $attachment)

Determine if the given uploaded file is an image.

array
mapMessagesToInput(array $messages, string|null $instructions, Provider $provider)

Map the given Hypervel messages to OpenAI Responses API input format.

void
mapUserMessage(UserMessage|Message $message, array $input, Provider $provider)

Map a user message to OpenAI format.

void
mapAssistantMessage(AssistantMessage|Message $message, array $input, Provider $provider)

Map an assistant message to OpenAI format.

array
withoutStoredOnlyItems(array $blocks)

Remove file_search_call items, which the API resolves by id and so rejects when store is false.

array
functionCallItem(ToolCall $toolCall)

Map a tool call to a function_call input item, keeping the item id only when OpenAI issued it and its reasoning survived.

void
mapToolResultMessage(ToolResultMessage|Message $message, array $input)

Map a tool result message to OpenAI format.

array
mapTools(array $tools, Provider $provider, bool $stateless = false)

Map the given tools to OpenAI function definitions.

void
guardStatelessToolSearch(Provider $provider, bool $stateless)

Ensure hosted tool search is not used while response storage is disabled.

array
mapTool(Tool $tool, bool $defer = false)

Map a regular tool to an Azure OpenAI function definition.

array
mapProviderTool(ProviderTool $tool, Provider $provider)

Map a provider tool to an OpenAI provider tool definition.

array
mapCodeExecutionTool(CodeExecution $tool, Provider $provider)

Map a code execution tool to an OpenAI code interpreter definition.

array
mapFileSearchTool(FileSearch $tool, Provider $provider)

Map a file search tool to an OpenAI file search definition.

array
mapWebSearchTool(WebSearch $tool, Provider $provider)

Map a web search tool to an OpenAI web search definition.

array
parseIndexedEmbeddings(array $records, int $expectedCount, string $providerName)

Return embedding values in input order without accepting ambiguous indices.

parseServerSentEvents(StreamInterface $streamBody)

Parse an SSE stream body into decoded JSON data objects.

string
readLine(StreamInterface $streamBody)

Read one line without waiting for a larger body buffer to fill.

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

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

Parse the OpenAI response data into a single step response.

string
extractText(array $output)

Extract the text content from the output array.

string
extractReasoning(array $output)

Extract the reasoning text from the output array.

extractCitations(array $output)

Extract citations from the output array.

array
extractReplayBlocks(array $output)

Extract the ordered response output for full-history replay.

array
extractProviderToolCalls(array $output)

Extract the provider-hosted tool items from the output array.

extractUsage(array $data)

Extract usage data from the response.

extractImageUsage(array $data)

Extract usage data from an image generation response.

FinishReason
extractFinishReason(array $data)

Extract and map the finish reason from the response.

array
mapToolCallsWithReasoning(array $output)

Map tool calls with their associated reasoning blocks.

__construct(Dispatcher $events)

Create an Azure OpenAI gateway instance.

generateEmbeddings(EmbeddingProvider $provider, string $model, array $inputs, int $dimensions, int $timeout = 30, array $providerOptions = [])

Generate embeddings for the given inputs.

generateImage(ImageProvider $provider, string $model, string $prompt, array $attachments = [], string|null $size = null, string|null $quality = null, int|null $timeout = null, array $providerOptions = [])

Generate an image.

sendImageEditRequest(ImageProvider $provider, string $model, string $prompt, array $attachments, string|null $size, string|null $quality, int|null $timeout, array $providerOptions = [])

Send an image edit request with attachments.

Details

in BuildsTextRequests at line 21
protected array buildTextRequestBody(Provider $provider, string $model, string|null $instructions, array $messages, array $tools, array|null $schema, TextGenerationOptions|null $options)

Build the request body for the OpenAI Responses API.

Parameters

Provider $provider
string $model
string|null $instructions
array $messages
array $tools
array|null $schema
TextGenerationOptions|null $options

Return Value

array

in BuildsTextRequests at line 41
protected array buildContinuationBody(Provider $provider, string $model, string $continuationToken, array $messages, array $tools, array|null $schema, TextGenerationOptions|null $options = null)

Build the request body for a stateful Responses API continuation.

Parameters

Provider $provider
string $model
string $continuationToken
array $messages
array $tools
array|null $schema
TextGenerationOptions|null $options

Return Value

array

in BuildsTextRequests at line 62
protected array extractToolResultsInput(array $messages)

Extract the latest tool results for a stateful continuation request.

Parameters

array $messages

Return Value

array

in BuildsTextRequests at line 82
protected array mergeSharedResponsesRequestOptions(array $body, array $tools, array|null $schema, TextGenerationOptions|null $options, Provider $provider)

Merge shared Responses API options onto the given request body.

Parameters

array $body
array $tools
array|null $schema
TextGenerationOptions|null $options
Provider $provider

Return Value

array

in BuildsTextRequests at line 142
protected string|array mapToolChoice(ToolChoice $choice)

Map a tool choice to the OpenAI Responses tool_choice shape.

Parameters

ToolChoice $choice

Return Value

string|array

in BuildsTextRequests at line 156
protected bool isStateless(Provider $provider)

Determine if OpenAI should receive full stateless conversation history.

Parameters

Provider $provider

Return Value

bool

in BuildsTextRequests at line 168
protected bool isReasoningModel(string $model)

Determine if the model supports encrypted reasoning content.

Parameters

string $model

Return Value

bool

in BuildsTextRequests at line 180
protected array buildSchemaFormat(array $schema, bool $strict)

Build the text format options for structured output.

Parameters

array $schema
bool $strict

Return Value

array

in CreatesClient at line 21
protected PendingRequest createClient(string $baseUrl, array $headers = [], array $configuredHeaders = [], int|null $timeout = 60, bool $throw = true)

Create an HTTP client with the provider's request options.

Parameters

string $baseUrl
array $headers
array $configuredHeaders
int|null $timeout
bool $throw

Return Value

PendingRequest

in CreatesClient at line 43
protected string httpConnection(Provider $provider)

Select a boot-registered connection without retaining dynamic provider names.

Parameters

Provider $provider

Return Value

string

protected PendingRequest client(Provider $provider, int|null $timeout = null)

Get an HTTP client for the Azure OpenAI v1-compatible API.

Parameters

Provider $provider
int|null $timeout

Return Value

PendingRequest

protected mixed withErrorHandling(string $providerName, Closure $callback)

Execute a callback with failoverable error handling.

Parameters

string $providerName
Closure $callback

Return Value

mixed

protected int|null retryAfterSeconds(Throwable $exception)

Get the short Retry-After delay of a rate limited or overloaded response, if it has one.

Parameters

Throwable $exception

Return Value

int|null

protected bool isInsufficientCredits(RequestException $exception)

Determine if the response is a 402, or a 429 insufficient credits or quota error rather than a rate limit.

Parameters

RequestException $exception

Return Value

bool

protected array overloadedStatusCodes()

Get the status codes that indicate a provider is transiently unavailable and the request should fail over.

Return Value

array

protected array insufficientCreditPatterns()

Get the patterns used to detect insufficient credits or quota errors.

Return Value

array

protected array insufficientCreditCodes()

Get the error codes or types that indicate insufficient credits or quota on a 429 response.

Return Value

array

protected Generator processTextStream(string $invocationId, Provider $provider, string $model, StreamInterface $streamBody)

Process streamed response events and return the completed step.

Parameters

string $invocationId
Provider $provider
string $model
StreamInterface $streamBody

Return Value

Generator

protected string generateEventId()

Generate a lowercase UUID v7 for use as a stream event ID.

Return Value

string

in HandlesTextSteps at line 19
StepResponse generateTextStep(TextProvider $provider, string $model, string|null $instructions, array $messages, array $tools, array|null $schema, TextGenerationOptions|null $options, int|null $timeout, StepContext $stepContext)

Generate text for a single Responses API step.

Parameters

TextProvider $provider
string $model
string|null $instructions
array $messages
array $tools
array|null $schema
TextGenerationOptions|null $options
int|null $timeout
StepContext $stepContext

Return Value

StepResponse

in HandlesTextSteps at line 48
Generator generateStreamStep(string $invocationId, TextProvider $provider, string $model, string|null $instructions, array $messages, array $tools, array|null $schema, TextGenerationOptions|null $options, int|null $timeout, StepContext $stepContext)

Stream text for a single Responses API step.

Parameters

string $invocationId
TextProvider $provider
string $model
string|null $instructions
array $messages
array $tools
array|null $schema
TextGenerationOptions|null $options
int|null $timeout
StepContext $stepContext

Return Value

Generator

in HandlesTextSteps at line 77
protected array buildStepBody(TextProvider $provider, string $model, string|null $instructions, array $messages, array $tools, array|null $schema, TextGenerationOptions|null $options, StepContext $stepContext)

Build the request body for the current text generation step.

Parameters

TextProvider $provider
string $model
string|null $instructions
array $messages
array $tools
array|null $schema
TextGenerationOptions|null $options
StepContext $stepContext

Return Value

array

protected string fallbackFilename(string|null $mimeType)

Derive a filename for a nameless document from its MIME type.

Parameters

string|null $mimeType

Return Value

string

in MapsAttachments at line 34
protected array mapAttachments(Collection $attachments, Provider $provider)

Map the given Hypervel attachments to OpenAI content parts.

Parameters

Collection $attachments
Provider $provider

Return Value

array

in MapsAttachments at line 115
protected bool isImage(UploadedFile $attachment)

Determine if the given uploaded file is an image.

Parameters

UploadedFile $attachment

Return Value

bool

in MapsMessages at line 21
protected array mapMessagesToInput(array $messages, string|null $instructions, Provider $provider)

Map the given Hypervel messages to OpenAI Responses API input format.

Parameters

array $messages
string|null $instructions
Provider $provider

Return Value

array

in MapsMessages at line 48
protected void mapUserMessage(UserMessage|Message $message, array $input, Provider $provider)

Map a user message to OpenAI format.

Parameters

UserMessage|Message $message
array $input
Provider $provider

Return Value

void

in MapsMessages at line 67
protected void mapAssistantMessage(AssistantMessage|Message $message, array $input, Provider $provider)

Map an assistant message to OpenAI format.

Parameters

AssistantMessage|Message $message
array $input
Provider $provider

Return Value

void

in MapsMessages at line 126
protected array withoutStoredOnlyItems(array $blocks)

Remove file_search_call items, which the API resolves by id and so rejects when store is false.

Parameters

array $blocks

Return Value

array

in MapsMessages at line 139
protected array functionCallItem(ToolCall $toolCall)

Map a tool call to a function_call input item, keeping the item id only when OpenAI issued it and its reasoning survived.

Parameters

ToolCall $toolCall

Return Value

array

in MapsMessages at line 154
protected void mapToolResultMessage(ToolResultMessage|Message $message, array $input)

Map a tool result message to OpenAI format.

Parameters

ToolResultMessage|Message $message
array $input

Return Value

void

in MapsTools at line 30
protected array mapTools(array $tools, Provider $provider, bool $stateless = false)

Map the given tools to OpenAI function definitions.

Parameters

array $tools
Provider $provider
bool $stateless

Return Value

array

in MapsTools at line 63
protected void guardStatelessToolSearch(Provider $provider, bool $stateless)

Ensure hosted tool search is not used while response storage is disabled.

Parameters

Provider $provider
bool $stateless

Return Value

void

at line 174
protected array mapTool(Tool $tool, bool $defer = false)

Map a regular tool to an Azure OpenAI function definition.

Parameters

Tool $tool
bool $defer

Return Value

array

in MapsTools at line 108
protected array mapProviderTool(ProviderTool $tool, Provider $provider)

Map a provider tool to an OpenAI provider tool definition.

Parameters

ProviderTool $tool
Provider $provider

Return Value

array

in MapsTools at line 121
protected array mapCodeExecutionTool(CodeExecution $tool, Provider $provider)

Map a code execution tool to an OpenAI code interpreter definition.

Parameters

CodeExecution $tool
Provider $provider

Return Value

array

in MapsTools at line 136
protected array mapFileSearchTool(FileSearch $tool, Provider $provider)

Map a file search tool to an OpenAI file search definition.

Parameters

FileSearch $tool
Provider $provider

Return Value

array

in MapsTools at line 151
protected array mapWebSearchTool(WebSearch $tool, Provider $provider)

Map a web search tool to an OpenAI web search definition.

Parameters

WebSearch $tool
Provider $provider

Return Value

array

protected array parseIndexedEmbeddings(array $records, int $expectedCount, string $providerName)

Return embedding values in input order without accepting ambiguous indices.

Parameters

array $records
int $expectedCount
string $providerName

Return Value

array

protected Generator parseServerSentEvents(StreamInterface $streamBody)

Parse an SSE stream body into decoded JSON data objects.

Parameters

StreamInterface $streamBody

Return Value

Generator

protected string readLine(StreamInterface $streamBody)

Read one line without waiting for a larger body buffer to fill.

Parameters

StreamInterface $streamBody

Return Value

string

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

protected void validateTextResponse(array $data)

Validate the OpenAI response data.

Parameters

array $data

Return Value

void

Exceptions

AiException

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

Parse the OpenAI response data into a single step response.

Parameters

array $data
Provider $provider
bool $structured

Return Value

StepResponse

protected string extractText(array $output)

Extract the text content from the output array.

Parameters

array $output

Return Value

string

in ParsesTextResponses at line 102
protected string extractReasoning(array $output)

Extract the reasoning text from the output array.

Parameters

array $output

Return Value

string

in ParsesTextResponses at line 117
protected Collection extractCitations(array $output)

Extract citations from the output array.

Parameters

array $output

Return Value

Collection

in ParsesTextResponses at line 150
protected array extractReplayBlocks(array $output)

Extract the ordered response output for full-history replay.

Parameters

array $output

Return Value

array

in ParsesTextResponses at line 160
protected array extractProviderToolCalls(array $output)

Extract the provider-hosted tool items from the output array.

Parameters

array $output

Return Value

array

in ParsesTextResponses at line 173
protected TextUsage extractUsage(array $data)

Extract usage data from the response.

Parameters

array $data

Return Value

TextUsage

in ParsesTextResponses at line 189
protected ImageUsage extractImageUsage(array $data)

Extract usage data from an image generation response.

Parameters

array $data

Return Value

ImageUsage

in ParsesTextResponses at line 205
protected FinishReason extractFinishReason(array $data)

Extract and map the finish reason from the response.

Parameters

array $data

Return Value

FinishReason

in ParsesTextResponses at line 237
protected array mapToolCallsWithReasoning(array $output)

Map tool calls with their associated reasoning blocks.

Parameters

array $output

Return Value

array

at line 57
__construct(Dispatcher $events)

Create an Azure OpenAI gateway instance.

Parameters

Dispatcher $events

at line 64
EmbeddingsResponse generateEmbeddings(EmbeddingProvider $provider, string $model, array $inputs, int $dimensions, int $timeout = 30, array $providerOptions = [])

Generate embeddings for the given inputs.

Parameters

EmbeddingProvider $provider
string $model
array $inputs
int $dimensions
int $timeout
array $providerOptions

Return Value

EmbeddingsResponse

at line 99
ImageResponse generateImage(ImageProvider $provider, string $model, string $prompt, array $attachments = [], string|null $size = null, string|null $quality = null, int|null $timeout = null, array $providerOptions = [])

Generate an image.

Parameters

ImageProvider $provider
string $model
string $prompt
array $attachments
string|null $size
string|null $quality
int|null $timeout
array $providerOptions

Return Value

ImageResponse

at line 140
protected Response sendImageEditRequest(ImageProvider $provider, string $model, string $prompt, array $attachments, string|null $size, string|null $quality, int|null $timeout, array $providerOptions = [])

Send an image edit request with attachments.

Parameters

ImageProvider $provider
string $model
string $prompt
array $attachments
string|null $size
string|null $quality
int|null $timeout
array $providerOptions

Return Value

Response