Skip to content

Commit 02ffc24

Browse files
committed
[Platform][OpenAI][Whisper] Validate API key to use OpenAI key format
1 parent f9be079 commit 02ffc24

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

src/platform/src/Bridge/OpenAi/Whisper/ModelClient.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ public function __construct(
3030
if ('' === $apiKey) {
3131
throw new InvalidArgumentException('The API key must not be empty.');
3232
}
33+
if (!str_starts_with($apiKey, 'sk-')) {
34+
throw new InvalidArgumentException('The API key must start with "sk-".');
35+
}
3336
}
3437

3538
public function supports(Model $model): bool

src/platform/tests/Bridge/OpenAi/Whisper/ModelClientTest.php

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,51 @@
1313

1414
use PHPUnit\Framework\Attributes\CoversClass;
1515
use PHPUnit\Framework\Attributes\Small;
16+
use PHPUnit\Framework\Attributes\TestWith;
1617
use PHPUnit\Framework\TestCase;
1718
use Symfony\AI\Platform\Bridge\OpenAi\Whisper;
1819
use Symfony\AI\Platform\Bridge\OpenAi\Whisper\ModelClient;
1920
use Symfony\AI\Platform\Bridge\OpenAi\Whisper\Task;
21+
use Symfony\AI\Platform\Exception\InvalidArgumentException;
2022
use Symfony\Component\HttpClient\MockHttpClient;
2123
use Symfony\Component\HttpClient\Response\MockResponse;
2224

2325
#[CoversClass(ModelClient::class)]
2426
#[Small]
2527
final class ModelClientTest extends TestCase
2628
{
29+
public function testItThrowsExceptionWhenApiKeyIsEmpty()
30+
{
31+
$this->expectException(InvalidArgumentException::class);
32+
$this->expectExceptionMessage('The API key must not be empty.');
33+
34+
new ModelClient(new MockHttpClient(), '');
35+
}
36+
37+
#[TestWith(['api-key-without-prefix'])]
38+
#[TestWith(['pk-api-key'])]
39+
#[TestWith(['SK-api-key'])]
40+
#[TestWith(['skapikey'])]
41+
#[TestWith(['sk api-key'])]
42+
#[TestWith(['sk'])]
43+
public function testItThrowsExceptionWhenApiKeyDoesNotStartWithSk(string $invalidApiKey)
44+
{
45+
$this->expectException(InvalidArgumentException::class);
46+
$this->expectExceptionMessage('The API key must start with "sk-".');
47+
48+
new ModelClient(new MockHttpClient(), $invalidApiKey);
49+
}
50+
51+
public function testItAcceptsValidApiKey()
52+
{
53+
$modelClient = new ModelClient(new MockHttpClient(), 'sk-valid-api-key');
54+
55+
$this->assertInstanceOf(ModelClient::class, $modelClient);
56+
}
57+
2758
public function testItSupportsWhisperModel()
2859
{
29-
$client = new ModelClient(new MockHttpClient(), 'test-key');
60+
$client = new ModelClient(new MockHttpClient(), 'sk-test-key');
3061
$this->assertTrue($client->supports(new Whisper()));
3162
}
3263

@@ -41,7 +72,7 @@ function ($method, $url): MockResponse {
4172
},
4273
]);
4374

44-
$client = new ModelClient($httpClient, 'test-key');
75+
$client = new ModelClient($httpClient, 'sk-test-key');
4576
$client->request(new Whisper(), ['file' => 'audio-data']);
4677

4778
$this->assertSame(1, $httpClient->getRequestsCount());
@@ -58,7 +89,7 @@ function ($method, $url): MockResponse {
5889
},
5990
]);
6091

61-
$client = new ModelClient($httpClient, 'test-key');
92+
$client = new ModelClient($httpClient, 'sk-test-key');
6293
$client->request(new Whisper(), ['file' => 'audio-data'], ['task' => Task::TRANSCRIPTION]);
6394

6495
$this->assertSame(1, $httpClient->getRequestsCount());
@@ -75,7 +106,7 @@ function ($method, $url): MockResponse {
75106
},
76107
]);
77108

78-
$client = new ModelClient($httpClient, 'test-key');
109+
$client = new ModelClient($httpClient, 'sk-test-key');
79110
$client->request(new Whisper(), ['file' => 'audio-data'], ['task' => Task::TRANSLATION]);
80111

81112
$this->assertSame(1, $httpClient->getRequestsCount());

0 commit comments

Comments
 (0)