Skip to content

Commit eace7a5

Browse files
authored
fix(OpenAI): allow null ranking_options and max_num_results in FileSearchTool (#798)
1 parent bfb0ed9 commit eace7a5

2 files changed

Lines changed: 45 additions & 6 deletions

File tree

src/Responses/Responses/Tool/FileSearchTool.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* @phpstan-import-type ComparisonFilterType from FileSearchComparisonFilter
1414
* @phpstan-import-type CompoundFilterType from FileSearchCompoundFilter
1515
*
16-
* @phpstan-type FileSearchToolType array{type: 'file_search', vector_store_ids: array<int, string>, filters: ComparisonFilterType|CompoundFilterType|null, max_num_results: int, ranking_options: RankingOptionType}
16+
* @phpstan-type FileSearchToolType array{type: 'file_search', vector_store_ids: array<int, string>, filters: ComparisonFilterType|CompoundFilterType|null, max_num_results: int|null, ranking_options: RankingOptionType|null}
1717
*
1818
* @implements ResponseContract<FileSearchToolType>
1919
*/
@@ -34,8 +34,8 @@ private function __construct(
3434
public readonly string $type,
3535
public readonly array $vectorStoreIds,
3636
public readonly FileSearchComparisonFilter|FileSearchCompoundFilter|null $filters,
37-
public readonly int $maxNumResults,
38-
public readonly FileSearchRankingOption $rankingOptions,
37+
public readonly ?int $maxNumResults,
38+
public readonly ?FileSearchRankingOption $rankingOptions,
3939
) {}
4040

4141
/**
@@ -56,8 +56,8 @@ public static function from(array $attributes): self
5656
type: $attributes['type'],
5757
vectorStoreIds: $attributes['vector_store_ids'],
5858
filters: $filters,
59-
maxNumResults: $attributes['max_num_results'],
60-
rankingOptions: FileSearchRankingOption::from($attributes['ranking_options']),
59+
maxNumResults: $attributes['max_num_results'] ?? null,
60+
rankingOptions: isset($attributes['ranking_options']) ? FileSearchRankingOption::from($attributes['ranking_options']) : null,
6161
);
6262
}
6363

@@ -71,7 +71,7 @@ public function toArray(): array
7171
'vector_store_ids' => $this->vectorStoreIds,
7272
'filters' => $this->filters?->toArray(),
7373
'max_num_results' => $this->maxNumResults,
74-
'ranking_options' => $this->rankingOptions->toArray(),
74+
'ranking_options' => $this->rankingOptions?->toArray(),
7575
];
7676
}
7777
}

tests/Responses/Responses/Tool/FileSearchTool.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,45 @@
3232
->filters->toBeNull();
3333
});
3434

35+
test('from null ranking options', function () {
36+
$payload = toolFileSearch();
37+
$payload['ranking_options'] = null;
38+
$response = FileSearchTool::from($payload);
39+
40+
expect($response)
41+
->toBeInstanceOf(FileSearchTool::class)
42+
->rankingOptions->toBeNull();
43+
});
44+
45+
test('from null max num results', function () {
46+
$payload = toolFileSearch();
47+
$payload['max_num_results'] = null;
48+
$response = FileSearchTool::from($payload);
49+
50+
expect($response)
51+
->toBeInstanceOf(FileSearchTool::class)
52+
->maxNumResults->toBeNull();
53+
});
54+
55+
test('from without optional keys', function () {
56+
$attributes = toolFileSearch();
57+
58+
unset($attributes['max_num_results'], $attributes['ranking_options']);
59+
60+
set_error_handler(static fn (int $errno, string $errstr): bool => throw new ErrorException($errstr), E_WARNING);
61+
62+
try {
63+
$response = FileSearchTool::from($attributes);
64+
} finally {
65+
restore_error_handler();
66+
}
67+
68+
expect($response)
69+
->toBeInstanceOf(FileSearchTool::class)
70+
->maxNumResults->toBeNull()
71+
->rankingOptions->toBeNull();
72+
});
73+
3574
test('from complex nested filters', function () {
3675
$response = FileSearchTool::from(toolFileSearchNestedFilters());
3776

0 commit comments

Comments
 (0)