Skip to content

Commit e388422

Browse files
authored
feat(OpenAI): Responses\CreateResponseType optional values are not handled properly (#728)
* fix(CreateResponse): make properties optional in CreateResponseType definition to reflect the object properties. This should surface the issues in the from() method implementation * fix(CreateResponse): make maxOutputTokens, previousResponseId, temperature, and topP truly optional in CreateResponseType * fix(RetrieveResponse): make properties optional in RetrieveResponseType definition and make it mirror CreateRsponse again.
1 parent 7a990e4 commit e388422

2 files changed

Lines changed: 19 additions & 17 deletions

File tree

src/Responses/Responses/CreateResponse.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
* @phpstan-import-type ResponseToolObjectTypes from ToolObjects
5050
*
5151
* @phpstan-type InstructionsType array<int, mixed>|string|null
52-
* @phpstan-type CreateResponseType array{id: string, background?: bool|null, object: 'response', created_at: int, status: 'completed'|'failed'|'in_progress'|'incomplete', error: ErrorType|null, incomplete_details: IncompleteDetailsType|null, instructions: InstructionsType, max_output_tokens: int|null, max_tool_calls?: int|null, model: string, output: ResponseOutputObjectTypes, output_text: string|null, parallel_tool_calls: bool, previous_response_id: string|null, prompt: ReferencePromptObjectType|null, prompt_cache_key?: string|null, reasoning: ReasoningType|null, safety_identifier?: string|null, service_tier?: string|null, store?: bool|null, temperature: float|null, text?: ResponseFormatType|null, tool_choice: ResponseToolChoiceTypes, tools: ResponseToolObjectTypes, top_logprobs?: int|null, top_p: float|null, truncation: 'auto'|'disabled'|null, usage: UsageType|null, user: string|null, verbosity: string|null, metadata: array<string, string>|null}
52+
* @phpstan-type CreateResponseType array{id: string, background?: bool|null, object: 'response', created_at: int, status: 'completed'|'failed'|'in_progress'|'incomplete', error: ErrorType|null, incomplete_details?: IncompleteDetailsType|null, instructions: InstructionsType, max_output_tokens?: int|null, max_tool_calls?: int|null, model: string, output: ResponseOutputObjectTypes, output_text: string|null, parallel_tool_calls: bool, previous_response_id?: string|null, prompt: ReferencePromptObjectType|null, prompt_cache_key?: string|null, reasoning?: ReasoningType|null, safety_identifier?: string|null, service_tier?: string|null, store?: bool|null, temperature?: float|null, text?: ResponseFormatType|null, tool_choice: ResponseToolChoiceTypes, tools: ResponseToolObjectTypes, top_logprobs?: int|null, top_p?: float|null, truncation?: 'auto'|'disabled'|null, usage?: UsageType|null, user?: string|null, verbosity?: string|null, metadata?: array<string, string>|null}
5353
*
5454
* @implements ResponseContract<CreateResponseType>
5555
*/
@@ -131,12 +131,12 @@ public static function from(array $attributes, MetaInformation $meta): self
131131
: null,
132132
instructions: $attributes['instructions'] ?? null,
133133
maxToolCalls: $attributes['max_tool_calls'] ?? null,
134-
maxOutputTokens: $attributes['max_output_tokens'],
134+
maxOutputTokens: $attributes['max_output_tokens'] ?? null,
135135
model: $attributes['model'],
136136
output: $output,
137137
outputText: OutputText::parse($output),
138138
parallelToolCalls: $attributes['parallel_tool_calls'],
139-
previousResponseId: $attributes['previous_response_id'],
139+
previousResponseId: $attributes['previous_response_id'] ?? null,
140140
prompt: isset($attributes['prompt'])
141141
? ReferencePromptObject::from($attributes['prompt'])
142142
: null,
@@ -147,14 +147,14 @@ public static function from(array $attributes, MetaInformation $meta): self
147147
? CreateResponseReasoning::from($attributes['reasoning'])
148148
: null,
149149
store: $attributes['store'] ?? true,
150-
temperature: $attributes['temperature'],
150+
temperature: $attributes['temperature'] ?? null,
151151
text: isset($attributes['text'])
152152
? CreateResponseFormat::from($attributes['text'])
153153
: null,
154154
toolChoice: $toolChoice,
155155
tools: $tools,
156156
topLogProbs: $attributes['top_logprobs'] ?? null,
157-
topP: $attributes['top_p'],
157+
topP: $attributes['top_p'] ?? null,
158158
truncation: $attributes['truncation'] ?? null,
159159
usage: isset($attributes['usage'])
160160
? CreateResponseUsage::from($attributes['usage'])

src/Responses/Responses/RetrieveResponse.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
* @phpstan-import-type ResponseToolObjectTypes from ToolObjects
5050
*
5151
* @phpstan-type InstructionsType array<int, mixed>|string|null
52-
* @phpstan-type RetrieveResponseType array{id: string, background?: bool|null, object: 'response', created_at: int, status: 'completed'|'failed'|'in_progress'|'incomplete', error: ErrorType|null, incomplete_details: IncompleteDetailsType|null, instructions: InstructionsType, max_output_tokens: int|null, max_tool_calls?: int|null, model: string, output: ResponseOutputObjectTypes, output_text: string|null, parallel_tool_calls: bool, previous_response_id: string|null, prompt: ReferencePromptObjectType|null, prompt_cache_key?: string|null, reasoning: ReasoningType|null, safety_identifier?: string|null, service_tier?: string|null, store: bool, temperature: float|null, text: ResponseFormatType, tool_choice: ResponseToolChoiceTypes, tools: ResponseToolObjectTypes, top_logprobs?: int|null, top_p: float|null, truncation: 'auto'|'disabled'|null, usage: UsageType|null, user: string|null, verbosity: string|null, metadata: array<string, string>|null}
52+
* @phpstan-type RetrieveResponseType array{id: string, background?: bool|null, object: 'response', created_at: int, status: 'completed'|'failed'|'in_progress'|'incomplete', error: ErrorType|null, incomplete_details?: IncompleteDetailsType|null, instructions: InstructionsType, max_output_tokens?: int|null, max_tool_calls?: int|null, model: string, output: ResponseOutputObjectTypes, output_text: string|null, parallel_tool_calls: bool, previous_response_id?: string|null, prompt: ReferencePromptObjectType|null, prompt_cache_key?: string|null, reasoning?: ReasoningType|null, safety_identifier?: string|null, service_tier?: string|null, store?: bool|null, temperature?: float|null, text?: ResponseFormatType|null, tool_choice: ResponseToolChoiceTypes, tools: ResponseToolObjectTypes, top_logprobs?: int|null, top_p?: float|null, truncation?: 'auto'|'disabled'|null, usage?: UsageType|null, user?: string|null, verbosity?: string|null, metadata?: array<string, string>|null}
5353
*
5454
* @implements ResponseContract<RetrieveResponseType>
5555
*/
@@ -68,7 +68,7 @@ final class RetrieveResponse implements ResponseContract, ResponseHasMetaInforma
6868
* @param 'completed'|'failed'|'in_progress'|'incomplete' $status
6969
* @param array<int, mixed>|string|null $instructions
7070
* @param array<int, OutputMessage|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall|OutputImageGenerationToolCall|OutputCodeInterpreterToolCall|OutputLocalShellCall|OutputCustomToolCall> $output
71-
* @param array<int, ComputerUseTool|FileSearchTool|FunctionTool|WebSearchTool|ImageGenerationTool|RemoteMcpTool|CodeInterpreterTool> $tools
71+
* @param array<int, ComputerUseTool|FileSearchTool|FunctionTool|WebSearchTool|ImageGenerationTool|RemoteMcpTool|ImageGenerationTool|CodeInterpreterTool> $tools
7272
* @param 'auto'|'disabled'|null $truncation
7373
* @param array<string, string> $metadata
7474
*/
@@ -95,7 +95,7 @@ private function __construct(
9595
public readonly ?CreateResponseReasoning $reasoning,
9696
public readonly bool $store,
9797
public readonly ?float $temperature,
98-
public readonly CreateResponseFormat $text,
98+
public readonly ?CreateResponseFormat $text,
9999
public readonly string|FunctionToolChoice|HostedToolChoice $toolChoice,
100100
public readonly array $tools,
101101
public readonly ?int $topLogProbs,
@@ -129,14 +129,14 @@ public static function from(array $attributes, MetaInformation $meta): self
129129
incompleteDetails: isset($attributes['incomplete_details'])
130130
? CreateResponseIncompleteDetails::from($attributes['incomplete_details'])
131131
: null,
132-
instructions: $attributes['instructions'],
132+
instructions: $attributes['instructions'] ?? null,
133133
maxToolCalls: $attributes['max_tool_calls'] ?? null,
134-
maxOutputTokens: $attributes['max_output_tokens'],
134+
maxOutputTokens: $attributes['max_output_tokens'] ?? null,
135135
model: $attributes['model'],
136136
output: $output,
137137
outputText: OutputText::parse($output),
138138
parallelToolCalls: $attributes['parallel_tool_calls'],
139-
previousResponseId: $attributes['previous_response_id'],
139+
previousResponseId: $attributes['previous_response_id'] ?? null,
140140
prompt: isset($attributes['prompt'])
141141
? ReferencePromptObject::from($attributes['prompt'])
142142
: null,
@@ -146,14 +146,16 @@ public static function from(array $attributes, MetaInformation $meta): self
146146
reasoning: isset($attributes['reasoning'])
147147
? CreateResponseReasoning::from($attributes['reasoning'])
148148
: null,
149-
store: $attributes['store'],
150-
temperature: $attributes['temperature'],
151-
text: CreateResponseFormat::from($attributes['text']),
149+
store: $attributes['store'] ?? true,
150+
temperature: $attributes['temperature'] ?? null,
151+
text: isset($attributes['text'])
152+
? CreateResponseFormat::from($attributes['text'])
153+
: null,
152154
toolChoice: $toolChoice,
153155
tools: $tools,
154156
topLogProbs: $attributes['top_logprobs'] ?? null,
155-
topP: $attributes['top_p'],
156-
truncation: $attributes['truncation'],
157+
topP: $attributes['top_p'] ?? null,
158+
truncation: $attributes['truncation'] ?? null,
157159
usage: isset($attributes['usage'])
158160
? CreateResponseUsage::from($attributes['usage'])
159161
: null,
@@ -198,7 +200,7 @@ public function toArray(): array
198200
'reasoning' => $this->reasoning?->toArray(),
199201
'store' => $this->store,
200202
'temperature' => $this->temperature,
201-
'text' => $this->text->toArray(),
203+
'text' => $this->text?->toArray(),
202204
'tool_choice' => is_string($this->toolChoice)
203205
? $this->toolChoice
204206
: $this->toolChoice->toArray(),

0 commit comments

Comments
 (0)