Skip to content

Commit 56881ba

Browse files
vitalii-kyktovdmytro-liashko-dev
authored andcommitted
[Platform][OpenAI] Add AuthenticationException for better error handling
When OpenAI API returns 401 (authentication error), users now get a clear AuthenticationException with the actual API error message instead of the confusing "Response does not contain choices" error. Changes: - Add AuthenticationException class for API authentication failures - Update OpenAI ResultConverter to check HTTP 401 status before processing - Add test coverage for authentication error scenarios Co-authored-by: Dmytro Liashko <[email protected]> Fixes #528
1 parent 51f7303 commit 56881ba

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

src/platform/src/Bridge/OpenAi/Gpt/ResultConverter.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\AI\Platform\Bridge\OpenAi\Gpt;
1313

1414
use Symfony\AI\Platform\Bridge\OpenAi\Gpt;
15+
use Symfony\AI\Platform\Exception\AuthenticationException;
1516
use Symfony\AI\Platform\Exception\ContentFilterException;
1617
use Symfony\AI\Platform\Exception\RuntimeException;
1718
use Symfony\AI\Platform\Model;
@@ -42,6 +43,13 @@ public function supports(Model $model): bool
4243

4344
public function convert(RawResultInterface|RawHttpResult $result, array $options = []): ResultInterface
4445
{
46+
$response = $result->getObject();
47+
48+
if (401 === $response->getStatusCode()) {
49+
$errorMessage = json_decode($response->getContent(false), true)['error']['message'];
50+
throw new AuthenticationException($errorMessage);
51+
}
52+
4553
if ($options['stream'] ?? false) {
4654
return new StreamResult($this->convertStream($result->getObject()));
4755
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace Symfony\AI\Platform\Exception;
11+
12+
/**
13+
* Exception thrown when API authentication fails.
14+
*
15+
* @author Vitalii Kyktov <[email protected]>
16+
* @author Dmytro Liashko <[email protected]>
17+
*/
18+
class AuthenticationException extends RuntimeException
19+
{
20+
}

src/platform/tests/Bridge/OpenAi/Gpt/ResultConverterTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use PHPUnit\Framework\Attributes\UsesClass;
1717
use PHPUnit\Framework\TestCase;
1818
use Symfony\AI\Platform\Bridge\OpenAi\Gpt\ResultConverter;
19+
use Symfony\AI\Platform\Exception\AuthenticationException;
1920
use Symfony\AI\Platform\Exception\ContentFilterException;
2021
use Symfony\AI\Platform\Exception\RuntimeException;
2122
use Symfony\AI\Platform\Result\ChoiceResult;
@@ -155,6 +156,23 @@ public function getResponse(): ResponseInterface
155156
$converter->convert(new RawHttpResult($httpResponse));
156157
}
157158

159+
public function testThrowsAuthenticationExceptionOnInvalidApiKey()
160+
{
161+
$converter = new ResultConverter();
162+
$httpResponse = self::createMock(ResponseInterface::class);
163+
$httpResponse->method('getStatusCode')->willReturn(401);
164+
$httpResponse->method('getContent')->willReturn(json_encode([
165+
'error' => [
166+
'message' => 'Invalid API key provided: sk-invalid'
167+
],
168+
]));
169+
170+
$this->expectException(AuthenticationException::class);
171+
$this->expectExceptionMessage('Invalid API key provided: sk-invalid');
172+
173+
$converter->convert(new RawHttpResult($httpResponse));
174+
}
175+
158176
public function testThrowsExceptionWhenNoChoices()
159177
{
160178
$converter = new ResultConverter();

0 commit comments

Comments
 (0)