Skip to content

Commit b0a6a76

Browse files
committed
Add error handling for missing models in Anthropic bridge
1 parent 5e1d943 commit b0a6a76

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ public function convert(RawHttpResult|RawResultInterface $result, array $options
4949

5050
$data = $result->getData();
5151

52+
if (isset($data['type']) && 'error' === $data['type']) {
53+
$type = $data['error']['type'] ?? 'Unknown';
54+
$message = $data['error']['message'] ?? 'An unknown error occurred.';
55+
throw new RuntimeException(\sprintf('API Error [%s]: "%s"', $type, $message));
56+
}
57+
5258
if (!isset($data['content']) || [] === $data['content']) {
5359
throw new RuntimeException('Response does not contain any content.');
5460
}

src/platform/tests/Bridge/Anthropic/ResultConverterTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\AI\Platform\Bridge\Anthropic\ResultConverter;
16+
use Symfony\AI\Platform\Exception\RuntimeException;
1617
use Symfony\AI\Platform\Result\RawHttpResult;
1718
use Symfony\AI\Platform\Result\ToolCallResult;
1819
use Symfony\Component\HttpClient\MockHttpClient;
1920
use Symfony\Component\HttpClient\Response\JsonMockResponse;
21+
use Symfony\Component\HttpClient\Response\MockResponse;
2022

2123
final class ResultConverterTest extends TestCase
2224
{
@@ -42,4 +44,34 @@ public function testConvertThrowsExceptionWhenContentIsToolUseAndLacksText()
4244
$this->assertSame('xxx_tool', $result->getContent()[0]->getName());
4345
$this->assertSame(['action' => 'get_data'], $result->getContent()[0]->getArguments());
4446
}
47+
48+
public function testModelNotFoundError()
49+
{
50+
$httpClient = new MockHttpClient([
51+
new MockResponse('{"type":"error","error":{"type":"not_found_error","message":"model: claude-3-5-sonnet-20241022"}}'),
52+
]);
53+
54+
$response = $httpClient->request('POST', 'https://api.anthropic.com/v1/messages');
55+
$converter = new ResultConverter();
56+
57+
$this->expectException(RuntimeException::class);
58+
$this->expectExceptionMessage('API Error [not_found_error]: "model: claude-3-5-sonnet-20241022"');
59+
60+
$converter->convert(new RawHttpResult($response));
61+
}
62+
63+
public function testUnknownError()
64+
{
65+
$httpClient = new MockHttpClient([
66+
new MockResponse('{"type":"error"}'),
67+
]);
68+
69+
$response = $httpClient->request('POST', 'https://api.anthropic.com/v1/messages');
70+
$converter = new ResultConverter();
71+
72+
$this->expectException(RuntimeException::class);
73+
$this->expectExceptionMessage('API Error [Unknown]: "An unknown error occurred."');
74+
75+
$converter->convert(new RawHttpResult($response));
76+
}
4577
}

0 commit comments

Comments
 (0)