Skip to content

Commit 64eb929

Browse files
authored
feat(OpenAI): GPT5 support (#639)
* feat: add support for new gpt-5 properties on responses api * feat: add support for new gpt-5 properties on retrieve api * chore: fix tests
1 parent 4db0807 commit 64eb929

File tree

3 files changed

+76
-3
lines changed

3 files changed

+76
-3
lines changed

src/Responses/Responses/CreateResponse.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
* @phpstan-type ToolChoiceType 'none'|'auto'|'required'|FunctionToolChoiceType|HostedToolChoiceType
6565
* @phpstan-type ToolsType array<int, ComputerUseToolType|FileSearchToolType|FunctionToolType|WebSearchToolType|ImageGenerationToolType|RemoteMcpToolType|CodeInterpreterToolType>
6666
* @phpstan-type OutputType array<int, OutputComputerToolCallType|OutputFileSearchToolCallType|OutputFunctionToolCallType|OutputMessageType|OutputReasoningType|OutputWebSearchToolCallType|OutputMcpListToolsType|OutputMcpApprovalRequestType|OutputMcpCallType|OutputImageGenerationToolCallType|OutputCodeInterpreterToolCallType>
67-
* @phpstan-type CreateResponseType array{id: string, 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, model: string, output: OutputType, output_text: string|null, parallel_tool_calls: bool, previous_response_id: string|null, prompt: ReferencePromptObjectType|null, reasoning: ReasoningType|null, store: bool, temperature: float|null, text: ResponseFormatType, tool_choice: ToolChoiceType, tools: ToolsType, top_p: float|null, truncation: 'auto'|'disabled'|null, usage: UsageType|null, user: string|null, metadata: array<string, string>|null}
67+
* @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: OutputType, 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: ToolChoiceType, tools: ToolsType, 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}
6868
*
6969
* @implements ResponseContract<CreateResponseType>
7070
*/
@@ -89,29 +89,36 @@ final class CreateResponse implements ResponseContract, ResponseHasMetaInformati
8989
*/
9090
private function __construct(
9191
public readonly string $id,
92+
public readonly ?bool $background,
9293
public readonly string $object,
9394
public readonly int $createdAt,
9495
public readonly string $status,
9596
public readonly ?CreateResponseError $error,
9697
public readonly ?CreateResponseIncompleteDetails $incompleteDetails,
9798
public readonly array|string|null $instructions,
99+
public readonly ?int $maxToolCalls,
98100
public readonly ?int $maxOutputTokens,
99101
public readonly string $model,
100102
public readonly array $output,
101103
public readonly ?string $outputText,
102104
public readonly bool $parallelToolCalls,
103105
public readonly ?string $previousResponseId,
104106
public readonly ?ReferencePromptObject $prompt,
107+
public readonly ?string $promptCacheKey,
108+
public readonly ?string $safetyIdentifier,
109+
public readonly ?string $serviceTier,
105110
public readonly ?CreateResponseReasoning $reasoning,
106111
public readonly bool $store,
107112
public readonly ?float $temperature,
108113
public readonly CreateResponseFormat $text,
109114
public readonly string|FunctionToolChoice|HostedToolChoice $toolChoice,
110115
public readonly array $tools,
116+
public readonly ?int $topLogProbs,
111117
public readonly ?float $topP,
112118
public readonly ?string $truncation,
113119
public readonly ?CreateResponseUsage $usage,
114120
public readonly ?string $user,
121+
public readonly ?string $verbosity,
115122
public readonly array $metadata,
116123
private readonly MetaInformation $meta,
117124
) {}
@@ -172,6 +179,7 @@ public static function from(array $attributes, MetaInformation $meta): self
172179

173180
return new self(
174181
id: $attributes['id'],
182+
background: $attributes['background'] ?? null,
175183
object: $attributes['object'],
176184
createdAt: $attributes['created_at'],
177185
status: $attributes['status'],
@@ -182,6 +190,7 @@ public static function from(array $attributes, MetaInformation $meta): self
182190
? CreateResponseIncompleteDetails::from($attributes['incomplete_details'])
183191
: null,
184192
instructions: $attributes['instructions'],
193+
maxToolCalls: $attributes['max_tool_calls'] ?? null,
185194
maxOutputTokens: $attributes['max_output_tokens'],
186195
model: $attributes['model'],
187196
output: $output,
@@ -191,6 +200,9 @@ public static function from(array $attributes, MetaInformation $meta): self
191200
prompt: isset($attributes['prompt'])
192201
? ReferencePromptObject::from($attributes['prompt'])
193202
: null,
203+
promptCacheKey: $attributes['prompt_cache_key'] ?? null,
204+
safetyIdentifier: $attributes['safety_identifier'] ?? null,
205+
serviceTier: $attributes['service_tier'] ?? null,
194206
reasoning: isset($attributes['reasoning'])
195207
? CreateResponseReasoning::from($attributes['reasoning'])
196208
: null,
@@ -199,12 +211,14 @@ public static function from(array $attributes, MetaInformation $meta): self
199211
text: CreateResponseFormat::from($attributes['text']),
200212
toolChoice: $toolChoice,
201213
tools: $tools,
214+
topLogProbs: $attributes['top_logprobs'] ?? null,
202215
topP: $attributes['top_p'],
203216
truncation: $attributes['truncation'],
204217
usage: isset($attributes['usage'])
205218
? CreateResponseUsage::from($attributes['usage'])
206219
: null,
207220
user: $attributes['user'] ?? null,
221+
verbosity: $attributes['verbosity'] ?? null,
208222
metadata: $attributes['metadata'] ?? [],
209223
meta: $meta,
210224
);
@@ -219,12 +233,14 @@ public function toArray(): array
219233
// @phpstan-ignore-next-line
220234
return [
221235
'id' => $this->id,
236+
'background' => $this->background,
222237
'object' => $this->object,
223238
'created_at' => $this->createdAt,
224239
'status' => $this->status,
225240
'error' => $this->error?->toArray(),
226241
'incomplete_details' => $this->incompleteDetails?->toArray(),
227242
'instructions' => $this->instructions,
243+
'max_tool_calls' => $this->maxToolCalls,
228244
'max_output_tokens' => $this->maxOutputTokens,
229245
'metadata' => $this->metadata ?? [],
230246
'model' => $this->model,
@@ -235,6 +251,9 @@ public function toArray(): array
235251
'parallel_tool_calls' => $this->parallelToolCalls,
236252
'previous_response_id' => $this->previousResponseId,
237253
'prompt' => $this->prompt?->toArray(),
254+
'prompt_cache_key' => $this->promptCacheKey,
255+
'safety_identifier' => $this->safetyIdentifier,
256+
'service_tier' => $this->serviceTier,
238257
'reasoning' => $this->reasoning?->toArray(),
239258
'store' => $this->store,
240259
'temperature' => $this->temperature,
@@ -246,10 +265,12 @@ public function toArray(): array
246265
fn (ComputerUseTool|FileSearchTool|FunctionTool|WebSearchTool|ImageGenerationTool|RemoteMcpTool|CodeInterpreterTool $tool): array => $tool->toArray(),
247266
$this->tools
248267
),
268+
'top_logprobs' => $this->topLogProbs,
249269
'top_p' => $this->topP,
250270
'truncation' => $this->truncation,
251271
'usage' => $this->usage?->toArray(),
252272
'user' => $this->user,
273+
'verbosity' => $this->verbosity,
253274
'output_text' => $this->outputText,
254275
];
255276
}

src/Responses/Responses/RetrieveResponse.php

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use OpenAI\Responses\Responses\Output\OutputMcpCall;
1919
use OpenAI\Responses\Responses\Output\OutputMcpListTools;
2020
use OpenAI\Responses\Responses\Output\OutputMessage;
21+
use OpenAI\Responses\Responses\Output\OutputMessageContentOutputText;
2122
use OpenAI\Responses\Responses\Output\OutputReasoning;
2223
use OpenAI\Responses\Responses\Output\OutputWebSearchToolCall;
2324
use OpenAI\Responses\Responses\Tool\CodeInterpreterTool;
@@ -64,7 +65,7 @@
6465
* @phpstan-type ToolChoiceType 'none'|'auto'|'required'|FunctionToolChoiceType|HostedToolChoiceType
6566
* @phpstan-type ToolsType array<int, ComputerUseToolType|FileSearchToolType|FunctionToolType|WebSearchToolType|ImageGenerationToolType|RemoteMcpToolType|CodeInterpreterToolType>
6667
* @phpstan-type OutputType array<int, OutputComputerToolCallType|OutputFileSearchToolCallType|OutputFunctionToolCallType|OutputMessageType|OutputReasoningType|OutputWebSearchToolCallType|OutputMcpListToolsType|OutputMcpApprovalRequestType|OutputMcpCallType|OutputImageGenerationToolCallType|OutputCodeInterpreterToolCallType>
67-
* @phpstan-type RetrieveResponseType array{id: string, 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, model: string, output: OutputType, parallel_tool_calls: bool, previous_response_id: string|null, prompt: ReferencePromptObjectType|null, reasoning: ReasoningType|null, store: bool, temperature: float|null, text: ResponseFormatType, tool_choice: ToolChoiceType, tools: ToolsType, top_p: float|null, truncation: 'auto'|'disabled'|null, usage: UsageType|null, user: string|null, metadata: array<string, string>|null}
68+
* @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: OutputType, 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: ToolChoiceType, tools: ToolsType, 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}
6869
*
6970
* @implements ResponseContract<RetrieveResponseType>
7071
*/
@@ -89,28 +90,36 @@ final class RetrieveResponse implements ResponseContract, ResponseHasMetaInforma
8990
*/
9091
private function __construct(
9192
public readonly string $id,
93+
public readonly ?bool $background,
9294
public readonly string $object,
9395
public readonly int $createdAt,
9496
public readonly string $status,
9597
public readonly ?CreateResponseError $error,
9698
public readonly ?CreateResponseIncompleteDetails $incompleteDetails,
9799
public readonly array|string|null $instructions,
100+
public readonly ?int $maxToolCalls,
98101
public readonly ?int $maxOutputTokens,
99102
public readonly string $model,
100103
public readonly array $output,
104+
public readonly ?string $outputText,
101105
public readonly bool $parallelToolCalls,
102106
public readonly ?string $previousResponseId,
103107
public readonly ?ReferencePromptObject $prompt,
108+
public readonly ?string $promptCacheKey,
109+
public readonly ?string $safetyIdentifier,
110+
public readonly ?string $serviceTier,
104111
public readonly ?CreateResponseReasoning $reasoning,
105112
public readonly bool $store,
106113
public readonly ?float $temperature,
107114
public readonly CreateResponseFormat $text,
108115
public readonly string|FunctionToolChoice|HostedToolChoice $toolChoice,
109116
public readonly array $tools,
117+
public readonly ?int $topLogProbs,
110118
public readonly ?float $topP,
111119
public readonly ?string $truncation,
112120
public readonly ?CreateResponseUsage $usage,
113121
public readonly ?string $user,
122+
public readonly ?string $verbosity,
114123
public array $metadata,
115124
private readonly MetaInformation $meta,
116125
) {}
@@ -157,8 +166,21 @@ public static function from(array $attributes, MetaInformation $meta): self
157166
$attributes['tools'],
158167
);
159168

169+
// Remake the sdk only property output_text.
170+
$texts = [];
171+
foreach ($output as $item) {
172+
if ($item instanceof OutputMessage) {
173+
foreach ($item->content as $content) {
174+
if ($content instanceof OutputMessageContentOutputText) {
175+
$texts[] = $content->text;
176+
}
177+
}
178+
}
179+
}
180+
160181
return new self(
161182
id: $attributes['id'],
183+
background: $attributes['background'] ?? null,
162184
object: $attributes['object'],
163185
createdAt: $attributes['created_at'],
164186
status: $attributes['status'],
@@ -169,14 +191,19 @@ public static function from(array $attributes, MetaInformation $meta): self
169191
? CreateResponseIncompleteDetails::from($attributes['incomplete_details'])
170192
: null,
171193
instructions: $attributes['instructions'],
194+
maxToolCalls: $attributes['max_tool_calls'] ?? null,
172195
maxOutputTokens: $attributes['max_output_tokens'],
173196
model: $attributes['model'],
174197
output: $output,
198+
outputText: empty($texts) ? null : implode(' ', $texts),
175199
parallelToolCalls: $attributes['parallel_tool_calls'],
176200
previousResponseId: $attributes['previous_response_id'],
177201
prompt: isset($attributes['prompt'])
178202
? ReferencePromptObject::from($attributes['prompt'])
179203
: null,
204+
promptCacheKey: $attributes['prompt_cache_key'] ?? null,
205+
safetyIdentifier: $attributes['safety_identifier'] ?? null,
206+
serviceTier: $attributes['service_tier'] ?? null,
180207
reasoning: isset($attributes['reasoning'])
181208
? CreateResponseReasoning::from($attributes['reasoning'])
182209
: null,
@@ -185,12 +212,14 @@ public static function from(array $attributes, MetaInformation $meta): self
185212
text: CreateResponseFormat::from($attributes['text']),
186213
toolChoice: $toolChoice,
187214
tools: $tools,
215+
topLogProbs: $attributes['top_logprobs'] ?? null,
188216
topP: $attributes['top_p'],
189217
truncation: $attributes['truncation'],
190218
usage: isset($attributes['usage'])
191219
? CreateResponseUsage::from($attributes['usage'])
192220
: null,
193221
user: $attributes['user'] ?? null,
222+
verbosity: $attributes['verbosity'] ?? null,
194223
metadata: $attributes['metadata'] ?? [],
195224
meta: $meta,
196225
);
@@ -205,22 +234,28 @@ public function toArray(): array
205234
// @phpstan-ignore-next-line
206235
return [
207236
'id' => $this->id,
237+
'background' => $this->background,
208238
'object' => $this->object,
209239
'created_at' => $this->createdAt,
210240
'status' => $this->status,
211241
'error' => $this->error?->toArray(),
212242
'incomplete_details' => $this->incompleteDetails?->toArray(),
213243
'instructions' => $this->instructions,
244+
'max_tool_calls' => $this->maxToolCalls,
214245
'max_output_tokens' => $this->maxOutputTokens,
215246
'metadata' => $this->metadata ?? [],
216247
'model' => $this->model,
217248
'output' => array_map(
218-
fn (OutputMessage|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning|OutputMcpListTools|OutputMcpCall|OutputMcpApprovalRequest|OutputImageGenerationToolCall|OutputCodeInterpreterToolCall $output): array => $output->toArray(),
249+
fn (OutputMessage|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall|OutputImageGenerationToolCall|OutputCodeInterpreterToolCall $output): array => $output->toArray(),
219250
$this->output
220251
),
252+
'output_text' => $this->outputText,
221253
'parallel_tool_calls' => $this->parallelToolCalls,
222254
'previous_response_id' => $this->previousResponseId,
223255
'prompt' => $this->prompt?->toArray(),
256+
'prompt_cache_key' => $this->promptCacheKey,
257+
'safety_identifier' => $this->safetyIdentifier,
258+
'service_tier' => $this->serviceTier,
224259
'reasoning' => $this->reasoning?->toArray(),
225260
'store' => $this->store,
226261
'temperature' => $this->temperature,
@@ -232,10 +267,12 @@ public function toArray(): array
232267
fn (ComputerUseTool|FileSearchTool|FunctionTool|WebSearchTool|ImageGenerationTool|RemoteMcpTool|CodeInterpreterTool $tool): array => $tool->toArray(),
233268
$this->tools
234269
),
270+
'top_logprobs' => $this->topLogProbs,
235271
'top_p' => $this->topP,
236272
'truncation' => $this->truncation,
237273
'usage' => $this->usage?->toArray(),
238274
'user' => $this->user,
275+
'verbosity' => $this->verbosity,
239276
];
240277
}
241278
}

0 commit comments

Comments
 (0)