Skip to content

Commit 0776107

Browse files
committed
Add API key validation to Whisper ModelClient
- Added validation to ensure API key starts with 'sk-' prefix - Follows the same pattern as other OpenAI ModelClients (GPT, DallE, Embeddings) - Added comprehensive tests for API key validation
1 parent 5b411aa commit 0776107

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,48 @@
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
{
2960
$client = new ModelClient(new MockHttpClient(), 'sk-test-key');

0 commit comments

Comments
 (0)