Skip to content

Commit 92c2b4a

Browse files
committed
add anthropic rate limit exception
1 parent cbb466a commit 92c2b4a

File tree

3 files changed

+108
-1
lines changed

3 files changed

+108
-1
lines changed

src/platform/src/Bridge/Anthropic/ResultConverter.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\AI\Platform\Bridge\Anthropic;
1313

14+
use Symfony\AI\Platform\Exception\RateExceededException;
1415
use Symfony\AI\Platform\Exception\RuntimeException;
1516
use Symfony\AI\Platform\Model;
1617
use Symfony\AI\Platform\Result\RawHttpResult;
@@ -38,8 +39,16 @@ public function supports(Model $model): bool
3839

3940
public function convert(RawHttpResult|RawResultInterface $result, array $options = []): ResultInterface
4041
{
42+
$response = $result->getObject();
43+
44+
if (429 === $response->getStatusCode()) {
45+
$retryAfter = $response->getHeaders(false)['retry-after'][0] ?? null;
46+
$retryAfterValue = $retryAfter ? (float) $retryAfter : null;
47+
throw new RateExceededException('Rate limit exceeded', $retryAfterValue);
48+
}
49+
4150
if ($options['stream'] ?? false) {
42-
return new StreamResult($this->convertStream($result->getObject()));
51+
return new StreamResult($this->convertStream($response));
4352
}
4453

4554
$data = $result->getData();
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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+
* @author Floran Pagliai <floran.pagliai@gmail.com>
16+
*/
17+
final class RateExceededException extends \RuntimeException implements ExceptionInterface
18+
{
19+
public function __construct(
20+
string $message,
21+
public readonly ?float $retryAfter = null,
22+
) {
23+
parent::__construct($message);
24+
}
25+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Tests\Bridge\Anthropic;
13+
14+
use PHPUnit\Framework\Attributes\CoversClass;
15+
use PHPUnit\Framework\Attributes\Small;
16+
use PHPUnit\Framework\TestCase;
17+
use Symfony\AI\Platform\Bridge\Anthropic\ResultConverter;
18+
use Symfony\AI\Platform\Exception\RateExceededException;
19+
use Symfony\AI\Platform\Result\RawHttpResult;
20+
use Symfony\Component\HttpClient\MockHttpClient;
21+
use Symfony\Component\HttpClient\Response\MockResponse;
22+
23+
#[CoversClass(ResultConverter::class)]
24+
#[Small]
25+
final class ResultConverterRateLimitTest extends TestCase
26+
{
27+
public function testRateLimitExceededThrowsException()
28+
{
29+
$httpClient = new MockHttpClient([
30+
new MockResponse('{"type":"error","error":{"type":"rate_limit_error","message":"This request would exceed the rate limit for your organization"}}', [
31+
'http_code' => 429,
32+
'response_headers' => [
33+
'retry-after' => '60',
34+
],
35+
]),
36+
]);
37+
38+
$httpResponse = $httpClient->request('POST', 'https://api.anthropic.com/v1/messages');
39+
$handler = new ResultConverter();
40+
41+
$this->expectException(RateExceededException::class);
42+
$this->expectExceptionMessage('Rate limit exceeded');
43+
44+
try {
45+
$handler->convert(new RawHttpResult($httpResponse));
46+
} catch (RateExceededException $e) {
47+
$this->assertSame(60.0, $e->retryAfter);
48+
throw $e;
49+
}
50+
}
51+
52+
public function testRateLimitExceededWithoutRetryAfter()
53+
{
54+
$httpClient = new MockHttpClient([
55+
new MockResponse('{"type":"error","error":{"type":"rate_limit_error","message":"This request would exceed the rate limit for your organization"}}', [
56+
'http_code' => 429,
57+
]),
58+
]);
59+
60+
$httpResponse = $httpClient->request('POST', 'https://api.anthropic.com/v1/messages');
61+
$handler = new ResultConverter();
62+
63+
$this->expectException(RateExceededException::class);
64+
$this->expectExceptionMessage('Rate limit exceeded');
65+
66+
try {
67+
$handler->convert(new RawHttpResult($httpResponse));
68+
} catch (RateExceededException $e) {
69+
$this->assertNull($e->retryAfter);
70+
throw $e;
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)