Skip to content

Commit bdd4616

Browse files
committed
[Platform][OpenRouter] Add status code checks for result converter
1 parent 36dfc1f commit bdd4616

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/platform/src/Bridge/OpenRouter/Completions/ResultConverter.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\AI\Platform\Exception\AuthenticationException;
1515
use Symfony\AI\Platform\Exception\BadRequestException;
16+
use Symfony\AI\Platform\Exception\RateLimitExceededException;
1617
use Symfony\AI\Platform\Exception\RuntimeException;
1718
use Symfony\AI\Platform\Model;
1819
use Symfony\AI\Platform\Result\RawResultInterface;
@@ -45,6 +46,11 @@ public function convert(RawResultInterface $result, array $options = []): Result
4546
throw new BadRequestException($errorMessage);
4647
}
4748

49+
if (429 === $response->getStatusCode()) {
50+
$errorMessage = json_decode($response->getContent(false), true)['error']['message'] ?? 'Bad Request';
51+
throw new RateLimitExceededException($errorMessage);
52+
}
53+
4854
if (!isset($data['choices'][0]['message'])) {
4955
throw new RuntimeException('Response does not contain message.');
5056
}

src/platform/src/Bridge/OpenRouter/Embeddings/ResultConverter.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
namespace Symfony\AI\Platform\Bridge\OpenRouter\Embeddings;
1313

1414
use Symfony\AI\Platform\Bridge\OpenRouter\Embeddings;
15+
use Symfony\AI\Platform\Exception\AuthenticationException;
16+
use Symfony\AI\Platform\Exception\BadRequestException;
17+
use Symfony\AI\Platform\Exception\RateLimitExceededException;
1518
use Symfony\AI\Platform\Exception\RuntimeException;
1619
use Symfony\AI\Platform\Model;
1720
use Symfony\AI\Platform\Result\RawResultInterface;
@@ -31,7 +34,24 @@ public function supports(Model $model): bool
3134

3235
public function convert(RawResultInterface $result, array $options = []): VectorResult
3336
{
37+
$response = $result->getObject();
3438
$data = $result->getData();
39+
40+
if (401 === $response->getStatusCode()) {
41+
$errorMessage = json_decode($response->getContent(false), true)['error']['message'];
42+
throw new AuthenticationException($errorMessage);
43+
}
44+
45+
if (400 === $response->getStatusCode() || 404 === $response->getStatusCode()) {
46+
$errorMessage = json_decode($response->getContent(false), true)['error']['message'] ?? 'Bad Request';
47+
throw new BadRequestException($errorMessage);
48+
}
49+
50+
if (429 === $response->getStatusCode()) {
51+
$errorMessage = json_decode($response->getContent(false), true)['error']['message'] ?? 'Bad Request';
52+
throw new RateLimitExceededException($errorMessage);
53+
}
54+
3555
if (!isset($data['data'][0]['embedding'])) {
3656
throw new RuntimeException('Response does not contain data.');
3757
}

0 commit comments

Comments
 (0)