Skip to content

Commit b13f46f

Browse files
authored
fix(OpenAI): default missing optional keys to null in from() (#786)
1 parent 02b51fa commit b13f46f

4 files changed

Lines changed: 66 additions & 5 deletions

File tree

src/Responses/Responses/Output/OutputMcpCall.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ public static function from(array $attributes): self
6969
type: $attributes['type'],
7070
arguments: $attributes['arguments'],
7171
name: $attributes['name'],
72-
approvalRequestId: $attributes['approval_request_id'],
72+
approvalRequestId: $attributes['approval_request_id'] ?? null,
7373
error: $errorType,
74-
output: $attributes['output'],
74+
output: $attributes['output'] ?? null,
7575
);
7676
}
7777

src/Responses/Responses/Tool/WebSearchUserLocation.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ public static function from(array $attributes): self
4040
{
4141
return new self(
4242
type: $attributes['type'],
43-
city: $attributes['city'],
43+
city: $attributes['city'] ?? null,
4444
country: $attributes['country'],
45-
region: $attributes['region'],
46-
timezone: $attributes['timezone'],
45+
region: $attributes['region'] ?? null,
46+
timezone: $attributes['timezone'] ?? null,
4747
);
4848
}
4949

tests/Responses/Responses/Output/OutputMcpCall.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,25 @@
1919
->output->toBeNull();
2020
});
2121

22+
test('from without optional keys', function () {
23+
$attributes = outputMcpCall();
24+
25+
unset($attributes['approval_request_id'], $attributes['output']);
26+
27+
set_error_handler(static fn (int $errno, string $errstr): bool => throw new ErrorException($errstr), E_WARNING);
28+
29+
try {
30+
$response = OutputMcpCall::from($attributes);
31+
} finally {
32+
restore_error_handler();
33+
}
34+
35+
expect($response)
36+
->toBeInstanceOf(OutputMcpCall::class)
37+
->approvalRequestId->toBeNull()
38+
->output->toBeNull();
39+
});
40+
2241
test('from error as http object', function () {
2342
$response = OutputMcpCall::from(outputMcpErrorCallObject());
2443

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
use OpenAI\Responses\Responses\Tool\WebSearchUserLocation;
4+
5+
test('from', function () {
6+
$response = WebSearchUserLocation::from([
7+
'type' => 'approximate',
8+
'city' => 'San Francisco',
9+
'country' => 'US',
10+
'region' => 'California',
11+
'timezone' => 'America/Los_Angeles',
12+
]);
13+
14+
expect($response)
15+
->toBeInstanceOf(WebSearchUserLocation::class)
16+
->type->toBe('approximate')
17+
->city->toBe('San Francisco')
18+
->country->toBe('US')
19+
->region->toBe('California')
20+
->timezone->toBe('America/Los_Angeles');
21+
});
22+
23+
test('from without optional keys', function () {
24+
set_error_handler(static fn (int $errno, string $errstr): bool => throw new ErrorException($errstr), E_WARNING);
25+
26+
try {
27+
$response = WebSearchUserLocation::from([
28+
'type' => 'approximate',
29+
'country' => 'US',
30+
]);
31+
} finally {
32+
restore_error_handler();
33+
}
34+
35+
expect($response)
36+
->toBeInstanceOf(WebSearchUserLocation::class)
37+
->type->toBe('approximate')
38+
->country->toBe('US')
39+
->city->toBeNull()
40+
->region->toBeNull()
41+
->timezone->toBeNull();
42+
});

0 commit comments

Comments
 (0)