Skip to content

Commit bcd6c06

Browse files
fix(OpenAI): List items for a conversation. file_id is null and file_data is not present in the API response (#715)
* fix: List items for a conversation. `file_id` is null and `file_data` is not present in the API response. * update * add tests `php -d memory_limit=512M ./vendor/bin/pest --colors=always` * single_blank_line_at_eof
1 parent a482c7a commit bcd6c06

3 files changed

Lines changed: 91 additions & 5 deletions

File tree

src/Responses/Responses/Input/InputMessageContentInputFile.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use OpenAI\Testing\Responses\Concerns\Fakeable;
1010

1111
/**
12-
* @phpstan-type ContentInputFileType array{type: 'input_file', file_data: string, file_id: string, filename: string}
12+
* @phpstan-type ContentInputFileType array{type: 'input_file', file_data: ?string, file_id: ?string, filename: string}
1313
*
1414
* @implements ResponseContract<ContentInputFileType>
1515
*/
@@ -27,8 +27,8 @@ final class InputMessageContentInputFile implements ResponseContract
2727
*/
2828
private function __construct(
2929
public readonly string $type,
30-
public readonly string $fileData,
31-
public readonly string $fileId,
30+
public readonly ?string $fileData,
31+
public readonly ?string $fileId,
3232
public readonly string $filename,
3333
) {}
3434

@@ -39,8 +39,8 @@ public static function from(array $attributes): self
3939
{
4040
return new self(
4141
type: $attributes['type'],
42-
fileData: $attributes['file_data'],
43-
fileId: $attributes['file_id'],
42+
fileData: $attributes['file_data'] ?? null,
43+
fileId: $attributes['file_id'] ?? null,
4444
filename: $attributes['filename'],
4545
);
4646
}

tests/Fixtures/Responses.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,31 @@ function mcpApprovalResponseItem(): array
307307
];
308308
}
309309

310+
/**
311+
* @return array<string, mixed>
312+
*/
313+
function InputMessageContentInputFileItem(): array
314+
{
315+
return [
316+
'type' => 'input_file',
317+
'file_data' => 'data:application/pdf;base64,JVBERi0xLjUKJY8KMTgxIDAgb2JqCjw8IC9GaWx0ZXIgL0ZsYXRlRGVjb2RlIC9MZW5ndGggMT...',
318+
'file_id' => 'file_0d1b41da7cb4903a0069235a5089408196a847a8f3e7b4fb1c',
319+
'filename' => 'file.pdf',
320+
];
321+
}
322+
323+
/**
324+
* @return array<string, mixed>
325+
*/
326+
function InputMessageContentInputFileItemMissingOrNull(): array
327+
{
328+
return [
329+
'type' => 'input_file',
330+
'file_id' => null,
331+
'filename' => 'file.pdf',
332+
];
333+
}
334+
310335
/**
311336
* @return array<string, mixed>
312337
*/
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
use OpenAI\Responses\Responses\Input\InputMessageContentInputFile;
4+
5+
test('from', function () {
6+
$response = InputMessageContentInputFile::from(InputMessageContentInputFileItem());
7+
8+
expect($response)
9+
->toBeInstanceOf(InputMessageContentInputFile::class)
10+
->type->toBe('input_file')
11+
->fileData->toBe('data:application/pdf;base64,JVBERi0xLjUKJY8KMTgxIDAgb2JqCjw8IC9GaWx0ZXIgL0ZsYXRlRGVjb2RlIC9MZW5ndGggMT...')
12+
->fileId->toBe('file_0d1b41da7cb4903a0069235a5089408196a847a8f3e7b4fb1c')
13+
->filename->toBe('file.pdf');
14+
});
15+
16+
it('is array accessible', function () {
17+
$response = InputMessageContentInputFile::from(InputMessageContentInputFileItem());
18+
19+
expect($response['file_id'])->toBe('file_0d1b41da7cb4903a0069235a5089408196a847a8f3e7b4fb1c');
20+
});
21+
22+
it('to array', function () {
23+
$response = InputMessageContentInputFile::from(InputMessageContentInputFileItem());
24+
25+
expect($response->toArray())
26+
->toBe([
27+
'type' => 'input_file',
28+
'file_data' => 'data:application/pdf;base64,JVBERi0xLjUKJY8KMTgxIDAgb2JqCjw8IC9GaWx0ZXIgL0ZsYXRlRGVjb2RlIC9MZW5ndGggMT...',
29+
'file_id' => 'file_0d1b41da7cb4903a0069235a5089408196a847a8f3e7b4fb1c',
30+
'filename' => 'file.pdf',
31+
]);
32+
});
33+
34+
test('from (missing or null)', function () {
35+
$response = InputMessageContentInputFile::from(InputMessageContentInputFileItemMissingOrNull());
36+
37+
expect($response)
38+
->toBeInstanceOf(InputMessageContentInputFile::class)
39+
->type->toBe('input_file')
40+
->fileData->toBeNull()
41+
->fileId->toBeNull()
42+
->filename->toBe('file.pdf');
43+
});
44+
45+
it('is array accessible (missing or null)', function () {
46+
$response = InputMessageContentInputFile::from(InputMessageContentInputFileItemMissingOrNull());
47+
48+
expect($response['file_id'])->toBeNull();
49+
});
50+
51+
it('to array (missing or null)', function () {
52+
$response = InputMessageContentInputFile::from(InputMessageContentInputFileItemMissingOrNull());
53+
54+
expect($response->toArray())
55+
->toBe([
56+
'type' => 'input_file',
57+
'file_data' => null,
58+
'file_id' => null,
59+
'filename' => 'file.pdf',
60+
]);
61+
});

0 commit comments

Comments
 (0)