Skip to content

Commit 52b43da

Browse files
floranpagliaichr-hertel
authored andcommitted
[Platform] Implement RateLimitExceededException handling across AI platforms
1 parent b2f0973 commit 52b43da

File tree

7 files changed

+284
-3
lines changed

7 files changed

+284
-3
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\RateLimitExceededException;
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 RateLimitExceededException($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();

src/platform/src/Bridge/Gemini/Gemini/ResultConverter.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\AI\Platform\Bridge\Gemini\Gemini;
1313

1414
use Symfony\AI\Platform\Bridge\Gemini\Gemini;
15+
use Symfony\AI\Platform\Exception\RateLimitExceededException;
1516
use Symfony\AI\Platform\Exception\RuntimeException;
1617
use Symfony\AI\Platform\Model;
1718
use Symfony\AI\Platform\Result\ChoiceResult;
@@ -42,8 +43,14 @@ 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 (429 === $response->getStatusCode()) {
49+
throw new RateLimitExceededException();
50+
}
51+
4552
if ($options['stream'] ?? false) {
46-
return new StreamResult($this->convertStream($result->getObject()));
53+
return new StreamResult($this->convertStream($response));
4754
}
4855

4956
$data = $result->getData();

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\AI\Platform\Bridge\OpenAi\Gpt;
1515
use Symfony\AI\Platform\Exception\AuthenticationException;
1616
use Symfony\AI\Platform\Exception\ContentFilterException;
17+
use Symfony\AI\Platform\Exception\RateLimitExceededException;
1718
use Symfony\AI\Platform\Exception\RuntimeException;
1819
use Symfony\AI\Platform\Model;
1920
use Symfony\AI\Platform\Result\ChoiceResult;
@@ -50,8 +51,21 @@ public function convert(RawResultInterface|RawHttpResult $result, array $options
5051
throw new AuthenticationException($errorMessage);
5152
}
5253

54+
if (429 === $response->getStatusCode()) {
55+
$headers = $response->getHeaders(false);
56+
$resetTime = null;
57+
58+
if (isset($headers['x-ratelimit-reset-requests'][0])) {
59+
$resetTime = self::parseResetTime($headers['x-ratelimit-reset-requests'][0]);
60+
} elseif (isset($headers['x-ratelimit-reset-tokens'][0])) {
61+
$resetTime = self::parseResetTime($headers['x-ratelimit-reset-tokens'][0]);
62+
}
63+
64+
throw new RateLimitExceededException($resetTime);
65+
}
66+
5367
if ($options['stream'] ?? false) {
54-
return new StreamResult($this->convertStream($result->getObject()));
68+
return new StreamResult($this->convertStream($response));
5569
}
5670

5771
$data = $result->getData();
@@ -194,4 +208,25 @@ private function convertToolCall(array $toolCall): ToolCall
194208

195209
return new ToolCall($toolCall['id'], $toolCall['function']['name'], $arguments);
196210
}
211+
212+
/**
213+
* Converts OpenAI's reset time format (e.g. "1s", "6m0s", "2m30s") into seconds.
214+
*
215+
* Supported formats:
216+
* - "1s"
217+
* - "6m0s"
218+
* - "2m30s"
219+
*/
220+
private static function parseResetTime(string $resetTime): float
221+
{
222+
$seconds = 0;
223+
224+
if (preg_match('/^(?:(\d+)m)?(?:(\d+)s)?$/', $resetTime, $matches)) {
225+
$minutes = isset($matches[1]) ? (int) $matches[1] : 0;
226+
$secs = isset($matches[2]) ? (int) $matches[2] : 0;
227+
$seconds = ($minutes * 60) + $secs;
228+
}
229+
230+
return (float) $seconds;
231+
}
197232
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
* @author Floran Pagliai <[email protected]>
16+
*/
17+
final class RateLimitExceededException extends RuntimeException
18+
{
19+
public function __construct(
20+
private readonly ?float $retryAfter = null,
21+
) {
22+
parent::__construct('Rate limit exceeded.');
23+
}
24+
25+
public function getRetryAfter(): ?float
26+
{
27+
return $this->retryAfter;
28+
}
29+
}
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 <[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\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\RateLimitExceededException;
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(RateLimitExceededException::class);
42+
$this->expectExceptionMessage('Rate limit exceeded');
43+
44+
try {
45+
$handler->convert(new RawHttpResult($httpResponse));
46+
} catch (RateLimitExceededException $e) {
47+
$this->assertSame(60.0, $e->getRetryAfter());
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(RateLimitExceededException::class);
64+
$this->expectExceptionMessage('Rate limit exceeded');
65+
66+
try {
67+
$handler->convert(new RawHttpResult($httpResponse));
68+
} catch (RateLimitExceededException $e) {
69+
$this->assertNull($e->getRetryAfter());
70+
throw $e;
71+
}
72+
}
73+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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\Tests\Bridge\Gemini\Gemini;
13+
14+
use PHPUnit\Framework\Attributes\CoversClass;
15+
use PHPUnit\Framework\Attributes\Small;
16+
use PHPUnit\Framework\TestCase;
17+
use Symfony\AI\Platform\Bridge\Gemini\Gemini\ResultConverter;
18+
use Symfony\AI\Platform\Exception\RateLimitExceededException;
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('{"error":{"code":429,"message":"Resource has been exhausted (e.g. check quota).","status":"RESOURCE_EXHAUSTED"}}', [
31+
'http_code' => 429,
32+
]),
33+
]);
34+
35+
$httpResponse = $httpClient->request('POST', 'https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent');
36+
$handler = new ResultConverter();
37+
38+
$this->expectException(RateLimitExceededException::class);
39+
$this->expectExceptionMessage('Rate limit exceeded.');
40+
41+
try {
42+
$handler->convert(new RawHttpResult($httpResponse));
43+
} catch (RateLimitExceededException $e) {
44+
$this->assertNull($e->getRetryAfter());
45+
throw $e;
46+
}
47+
}
48+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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\Tests\Bridge\OpenAi\Gpt;
13+
14+
use PHPUnit\Framework\Attributes\CoversClass;
15+
use PHPUnit\Framework\Attributes\Small;
16+
use PHPUnit\Framework\TestCase;
17+
use Symfony\AI\Platform\Bridge\OpenAi\Gpt\ResultConverter;
18+
use Symfony\AI\Platform\Exception\RateLimitExceededException;
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 testRateLimitExceededWithRequestsResetTime()
28+
{
29+
$httpClient = new MockHttpClient([
30+
new MockResponse('{"error":{"message":"Rate limit reached for requests","type":"rate_limit_error"}}', [
31+
'http_code' => 429,
32+
'response_headers' => [
33+
'x-ratelimit-limit-requests' => '60',
34+
'x-ratelimit-remaining-requests' => '0',
35+
'x-ratelimit-reset-requests' => '20s',
36+
],
37+
]),
38+
]);
39+
40+
$httpResponse = $httpClient->request('POST', 'https://api.openai.com/v1/chat/completions');
41+
$handler = new ResultConverter();
42+
43+
$this->expectException(RateLimitExceededException::class);
44+
$this->expectExceptionMessage('Rate limit exceeded.');
45+
46+
try {
47+
$handler->convert(new RawHttpResult($httpResponse));
48+
} catch (RateLimitExceededException $e) {
49+
$this->assertSame(20.0, $e->getRetryAfter());
50+
throw $e;
51+
}
52+
}
53+
54+
public function testRateLimitExceededWithTokensResetTime()
55+
{
56+
$httpClient = new MockHttpClient([
57+
new MockResponse('{"error":{"message":"Rate limit reached for tokens","type":"rate_limit_error"}}', [
58+
'http_code' => 429,
59+
'response_headers' => [
60+
'x-ratelimit-limit-tokens' => '150000',
61+
'x-ratelimit-remaining-tokens' => '0',
62+
'x-ratelimit-reset-tokens' => '2m30s',
63+
],
64+
]),
65+
]);
66+
67+
$httpResponse = $httpClient->request('POST', 'https://api.openai.com/v1/chat/completions');
68+
$handler = new ResultConverter();
69+
70+
$this->expectException(RateLimitExceededException::class);
71+
$this->expectExceptionMessage('Rate limit exceeded.');
72+
73+
try {
74+
$handler->convert(new RawHttpResult($httpResponse));
75+
} catch (RateLimitExceededException $e) {
76+
$this->assertSame(150.0, $e->getRetryAfter());
77+
throw $e;
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)