Skip to content

Commit 6bfcaa6

Browse files
committed
feature #543 [Platform][OpenAI] Add AuthenticationException for better error handling (vitalii-kyktov, vitality)
This PR was merged into the main branch. Discussion ---------- [Platform][OpenAI] Add AuthenticationException for better error handling | Q | A | ------------- | --- | Branch? | main | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #528 | License | MIT 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 Commits ------- 193b56a [Platform][OpenAI] remove incomplete `@author` tags from AuthenticationException 7da46cb [Platform][OpenAI] simplify authentication error test structure 56881ba [Platform][OpenAI] Add AuthenticationException for better error handling
2 parents e7be456 + 193b56a commit 6bfcaa6

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\AI\Platform\Exception;
13+
14+
/**
15+
* Exception thrown when API authentication fails.
16+
*
17+
* @author Vitalii Kyktov <[email protected]>
18+
* @author Dmytro Liashko <[email protected]>
19+
*/
20+
class AuthenticationException extends RuntimeException
21+
{
22+
}

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)