Skip to content

Commit 652c3d0

Browse files
Address review: classify 403 as auth, wrap non-APIError failures, cover mid-stream errors
- Map HTTP 403 (Anthropic PermissionDeniedError / Gemini ClientError) to ChatClientInvalidAuthException, matching Mistral's 401/403 handling. - Replace the provider-specific except chain with a catch-all that routes every non-framework exception through a _wrap_*_error helper, so transport failures and credential-refresh errors no longer escape ChatClientException. - Re-raise AgentFrameworkException (e.g. ContentError) untouched. - Extend tests to cover 403, a non-APIError fallback, and a failure raised partway through iterating the stream (generator yields then raises).
1 parent 8e129fd commit 652c3d0

4 files changed

Lines changed: 119 additions & 75 deletions

File tree

‎python/packages/anthropic/agent_framework_anthropic/_chat_client.py‎

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from agent_framework._tools import SHELL_TOOL_KIND_VALUE, normalize_tools
3333
from agent_framework._types import _get_data_bytes_as_str # type: ignore
3434
from agent_framework.exceptions import (
35+
AgentFrameworkException,
3536
ChatClientException,
3637
ChatClientInvalidAuthException,
3738
ChatClientInvalidRequestException,
@@ -41,6 +42,7 @@
4142
from anthropic import AsyncAnthropic, AsyncAnthropicFoundry
4243
from anthropic import AuthenticationError as AnthropicAuthenticationError
4344
from anthropic import BadRequestError as AnthropicBadRequestError
45+
from anthropic import PermissionDeniedError as AnthropicPermissionDeniedError
4446
from anthropic.lib.bedrock import AsyncAnthropicBedrock
4547
from anthropic.lib.vertex import AsyncAnthropicVertex
4648
from anthropic.types.beta import (
@@ -95,6 +97,23 @@
9597
AnthropicAsyncClient = AsyncAnthropic | AsyncAnthropicBedrock | AsyncAnthropicFoundry | AsyncAnthropicVertex
9698

9799

100+
def _wrap_anthropic_error(ex: Exception) -> ChatClientException:
101+
"""Translate a raw anthropic-sdk failure into the framework's ChatClientException hierarchy.
102+
103+
``APIError`` instances are classified by HTTP status (401/403 -> auth, other 4xx ->
104+
invalid request), matching the Mistral client. Anything else - connection errors,
105+
timeouts, unexpected SDK exceptions - is still wrapped as a generic ``ChatClientException``
106+
so callers catching that base type never see a raw provider exception leak through.
107+
"""
108+
if isinstance(ex, AnthropicAPIError):
109+
status = getattr(ex, "status_code", None)
110+
if isinstance(ex, (AnthropicAuthenticationError, AnthropicPermissionDeniedError)) or status in (401, 403):
111+
return ChatClientInvalidAuthException(f"Anthropic authentication failed: {ex}", inner_exception=ex)
112+
if isinstance(ex, AnthropicBadRequestError) or (isinstance(status, int) and 400 <= status < 500):
113+
return ChatClientInvalidRequestException(f"Invalid Anthropic request: {ex}", inner_exception=ex)
114+
return ChatClientException(f"Anthropic chat request failed: {ex}", inner_exception=ex)
115+
116+
98117
# region Anthropic Chat Options TypedDict
99118

100119

@@ -566,16 +585,10 @@ async def _stream() -> AsyncIterable[ChatResponseUpdate]:
566585
parsed_chunk = self._process_stream_event(chunk, emitted_usage)
567586
if parsed_chunk:
568587
yield parsed_chunk
569-
except AnthropicAuthenticationError as ex:
570-
raise ChatClientInvalidAuthException(
571-
f"Anthropic authentication failed: {ex}", inner_exception=ex
572-
) from ex
573-
except AnthropicBadRequestError as ex:
574-
raise ChatClientInvalidRequestException(
575-
f"Invalid Anthropic request: {ex}", inner_exception=ex
576-
) from ex
577-
except AnthropicAPIError as ex:
578-
raise ChatClientException(f"Anthropic chat request failed: {ex}", inner_exception=ex) from ex
588+
except AgentFrameworkException:
589+
raise
590+
except Exception as ex:
591+
raise _wrap_anthropic_error(ex) from ex
579592

580593
return self._build_response_stream(_stream(), response_format=options.get("response_format"))
581594

@@ -584,14 +597,10 @@ async def _get_response() -> ChatResponse:
584597
mark_feature_used(FeatureIndex.ANTHROPIC)
585598
try:
586599
message = await self.anthropic_client.beta.messages.create(**run_options, stream=False)
587-
except AnthropicAuthenticationError as ex:
588-
raise ChatClientInvalidAuthException(
589-
f"Anthropic authentication failed: {ex}", inner_exception=ex
590-
) from ex
591-
except AnthropicBadRequestError as ex:
592-
raise ChatClientInvalidRequestException(f"Invalid Anthropic request: {ex}", inner_exception=ex) from ex
593-
except AnthropicAPIError as ex:
594-
raise ChatClientException(f"Anthropic chat request failed: {ex}", inner_exception=ex) from ex
600+
except AgentFrameworkException:
601+
raise
602+
except Exception as ex:
603+
raise _wrap_anthropic_error(ex) from ex
595604
return self._process_message(message, options)
596605

597606
return _get_response()

‎python/packages/anthropic/tests/test_anthropic_client.py‎

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@
3131
ChatClientInvalidRequestException,
3232
)
3333
from agent_framework.observability import ChatTelemetryLayer
34-
from agent_framework_anthropic import AnthropicChatOptions, AnthropicClient, RawAnthropicClient
35-
from agent_framework_anthropic._chat_client import AnthropicSettings
36-
from agent_framework_anthropic._feature_usage import FeatureIndex
3734
from anthropic.types.beta import (
3835
BetaMessage,
3936
BetaMessageDeltaUsage,
@@ -43,6 +40,10 @@
4340
)
4441
from pydantic import BaseModel, Field
4542

43+
from agent_framework_anthropic import AnthropicChatOptions, AnthropicClient, RawAnthropicClient
44+
from agent_framework_anthropic._chat_client import AnthropicSettings
45+
from agent_framework_anthropic._feature_usage import FeatureIndex
46+
4647
# Test constants
4748
VALID_PNG_BASE64 = b"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=="
4849

@@ -1817,36 +1818,27 @@ def _anthropic_status_error(
18171818

18181819

18191820
@pytest.mark.parametrize(
1820-
("sdk_exception", "status_code", "expected_exception"),
1821+
("sdk_exception", "expected_exception"),
18211822
[
1822-
(
1823-
anthropic_sdk.AuthenticationError,
1824-
401,
1825-
ChatClientInvalidAuthException,
1826-
),
1827-
(
1828-
anthropic_sdk.BadRequestError,
1829-
400,
1830-
ChatClientInvalidRequestException,
1831-
),
1832-
(
1833-
anthropic_sdk.InternalServerError,
1834-
500,
1835-
ChatClientException,
1836-
),
1823+
(_anthropic_status_error(anthropic_sdk.AuthenticationError, 401, "boom"), ChatClientInvalidAuthException),
1824+
(_anthropic_status_error(anthropic_sdk.PermissionDeniedError, 403, "boom"), ChatClientInvalidAuthException),
1825+
(_anthropic_status_error(anthropic_sdk.BadRequestError, 400, "boom"), ChatClientInvalidRequestException),
1826+
(_anthropic_status_error(anthropic_sdk.InternalServerError, 500, "boom"), ChatClientException),
1827+
# Not an anthropic APIError at all (connection reset, timeout, unexpected SDK bug):
1828+
# must still be wrapped so ``except ChatClientException`` callers never see it raw.
1829+
(RuntimeError("connection reset"), ChatClientException),
18371830
],
18381831
)
18391832
async def test_inner_get_response_wraps_sdk_errors(
18401833
mock_anthropic_client: MagicMock,
1841-
sdk_exception: type[anthropic_sdk.APIStatusError],
1842-
status_code: int,
1834+
sdk_exception: Exception,
18431835
expected_exception: type[Exception],
18441836
) -> None:
18451837
"""Non-streaming _inner_get_response must translate raw Anthropic SDK errors into
18461838
the framework's ChatClientException hierarchy, matching every other provider
18471839
(OpenAI, Mistral, Ollama, Bedrock)."""
18481840
client = create_test_anthropic_client(mock_anthropic_client)
1849-
mock_anthropic_client.beta.messages.create.side_effect = _anthropic_status_error(sdk_exception, status_code, "boom")
1841+
mock_anthropic_client.beta.messages.create.side_effect = sdk_exception
18501842

18511843
messages = [Message(role="user", contents=["Hi"])]
18521844
chat_options = ChatOptions(max_tokens=10)
@@ -1858,16 +1850,32 @@ async def test_inner_get_response_wraps_sdk_errors(
18581850

18591851

18601852
async def test_inner_get_response_streaming_wraps_sdk_errors(mock_anthropic_client: MagicMock) -> None:
1861-
"""Streaming _inner_get_response must translate raw Anthropic SDK errors into
1862-
the framework's ChatClientException hierarchy too, not just the non-streaming path."""
1853+
"""Streaming _inner_get_response must translate raw Anthropic SDK errors into the
1854+
framework's ChatClientException hierarchy too, both when the create() call fails and
1855+
when the failure happens partway through iterating the stream."""
18631856
client = create_test_anthropic_client(mock_anthropic_client)
1857+
messages = [Message(role="user", contents=["Hi"])]
1858+
chat_options = ChatOptions(max_tokens=10)
1859+
1860+
# 1. Failure raised by the create() call itself.
18641861
mock_anthropic_client.beta.messages.create.side_effect = _anthropic_status_error(
18651862
anthropic_sdk.AuthenticationError, 401, "invalid api key"
18661863
)
1864+
with pytest.raises(ChatClientInvalidAuthException, match="Anthropic"):
1865+
async for _ in client._inner_get_response( # type: ignore[attr-defined] # ty: ignore[not-iterable]
1866+
messages=messages, options=chat_options, stream=True
1867+
):
1868+
pass
18671869

1868-
messages = [Message(role="user", contents=["Hi"])]
1869-
chat_options = ChatOptions(max_tokens=10)
1870+
# 2. Failure raised mid-stream, after at least one event has been yielded.
1871+
async def _raise_after_first_event() -> Any:
1872+
event = MagicMock()
1873+
event.type = "message_stop"
1874+
yield event
1875+
raise _anthropic_status_error(anthropic_sdk.PermissionDeniedError, 403, "permission denied")
18701876

1877+
mock_anthropic_client.beta.messages.create.side_effect = None
1878+
mock_anthropic_client.beta.messages.create.return_value = _raise_after_first_event()
18711879
with pytest.raises(ChatClientInvalidAuthException, match="Anthropic"):
18721880
async for _ in client._inner_get_response( # type: ignore[attr-defined] # ty: ignore[not-iterable]
18731881
messages=messages, options=chat_options, stream=True

‎python/packages/gemini/agent_framework_gemini/_chat_client.py‎

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from agent_framework._telemetry import get_user_agent, mark_feature_used
3434
from agent_framework._types import _get_data_bytes # type: ignore[reportPrivateUsage]
3535
from agent_framework.exceptions import (
36+
AgentFrameworkException,
3637
ChatClientException,
3738
ChatClientInvalidAuthException,
3839
ChatClientInvalidRequestException,
@@ -43,7 +44,6 @@
4344
from google.auth.credentials import Credentials
4445
from google.genai import types
4546
from google.genai.errors import APIError as GenAIAPIError
46-
from google.genai.errors import ClientError as GenAIClientError
4747
from pydantic import BaseModel
4848

4949
from ._feature_usage import FeatureIndex
@@ -77,6 +77,24 @@
7777
ResponseModelT = TypeVar("ResponseModelT", bound=BaseModel | None, default=None)
7878

7979

80+
def _wrap_gemini_error(ex: Exception) -> ChatClientException:
81+
"""Translate a raw google-genai failure into the framework's ChatClientException hierarchy.
82+
83+
google-genai ``APIError`` instances are classified by HTTP status (401/403 -> auth, other
84+
4xx -> invalid request), matching the Mistral client. Anything else - transport errors,
85+
Vertex credential-refresh failures, unexpected SDK exceptions - is still wrapped as a
86+
generic ``ChatClientException`` so callers catching that base type never see a raw
87+
provider exception leak through.
88+
"""
89+
if isinstance(ex, GenAIAPIError):
90+
code = getattr(ex, "code", None)
91+
if code in (401, 403):
92+
return ChatClientInvalidAuthException(f"Gemini authentication failed: {ex}", inner_exception=ex)
93+
if isinstance(code, int) and 400 <= code < 500:
94+
return ChatClientInvalidRequestException(f"Invalid Gemini request: {ex}", inner_exception=ex)
95+
return ChatClientException(f"Gemini chat request failed: {ex}", inner_exception=ex)
96+
97+
8098
# region Options & Settings
8199

82100

@@ -572,35 +590,27 @@ async def _stream() -> AsyncIterable[ChatResponseUpdate]:
572590
config=config,
573591
):
574592
yield self._process_chunk(chunk)
575-
except GenAIClientError as ex:
576-
if ex.code == 401:
577-
raise ChatClientInvalidAuthException(
578-
f"Gemini authentication failed: {ex}", inner_exception=ex
579-
) from ex
580-
raise ChatClientInvalidRequestException(f"Invalid Gemini request: {ex}", inner_exception=ex) from ex
581-
except GenAIAPIError as ex:
582-
raise ChatClientException(f"Gemini chat request failed: {ex}", inner_exception=ex) from ex
593+
except AgentFrameworkException:
594+
raise
595+
except Exception as ex:
596+
raise _wrap_gemini_error(ex) from ex
583597

584598
return self._build_response_stream(_stream(), response_format=options.get("response_format"))
585599

586600
async def _get_response() -> ChatResponse:
587601
validated = await self._validate_options(options)
588602
model, contents, config = self._prepare_request(messages, validated)
589603
mark_feature_used(FeatureIndex.GEMINI)
604+
generate_content = cast(
605+
Callable[..., Awaitable[types.GenerateContentResponse]],
606+
cast(Any, self._genai_client.aio.models).generate_content,
607+
)
590608
try:
591-
raw = await self._genai_client.aio.models.generate_content(
592-
model=model,
593-
contents=contents,
594-
config=config, # type: ignore[arg-type]
595-
)
596-
except GenAIClientError as ex:
597-
if ex.code == 401:
598-
raise ChatClientInvalidAuthException(
599-
f"Gemini authentication failed: {ex}", inner_exception=ex
600-
) from ex
601-
raise ChatClientInvalidRequestException(f"Invalid Gemini request: {ex}", inner_exception=ex) from ex
602-
except GenAIAPIError as ex:
603-
raise ChatClientException(f"Gemini chat request failed: {ex}", inner_exception=ex) from ex
609+
raw = await generate_content(model=model, contents=contents, config=config)
610+
except AgentFrameworkException:
611+
raise
612+
except Exception as ex:
613+
raise _wrap_gemini_error(ex) from ex
604614
return self._process_generate_response(raw, response_format=validated.get("response_format"))
605615

606616
return _get_response()

‎python/packages/gemini/tests/test_gemini_client.py‎

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@
1717
ChatClientInvalidAuthException,
1818
ChatClientInvalidRequestException,
1919
)
20-
from agent_framework_gemini import GeminiChatClient, GeminiChatOptions, RawGeminiChatClient, ThinkingConfig
21-
from agent_framework_gemini._feature_usage import FeatureIndex
2220
from google.genai import errors as genai_errors
2321
from google.genai import types
2422
from pydantic import BaseModel
2523
from typing_extensions import NotRequired, TypedDict
2624

25+
from agent_framework_gemini import GeminiChatClient, GeminiChatOptions, RawGeminiChatClient, ThinkingConfig
26+
from agent_framework_gemini._feature_usage import FeatureIndex
27+
2728

2829
def _has_gemini_integration_credentials() -> bool:
2930
"""Return whether integration credentials for either Gemini API or Vertex AI appear to be configured."""
@@ -387,12 +388,16 @@ async def test_get_response_returns_text() -> None:
387388
("sdk_exception", "expected_exception"),
388389
[
389390
(genai_errors.ClientError(401, {"error": {"message": "invalid api key"}}), ChatClientInvalidAuthException),
391+
(genai_errors.ClientError(403, {"error": {"message": "permission denied"}}), ChatClientInvalidAuthException),
390392
(genai_errors.ClientError(400, {"error": {"message": "bad request"}}), ChatClientInvalidRequestException),
391393
(genai_errors.ServerError(500, {"error": {"message": "server error"}}), ChatClientException),
394+
# Not a google-genai APIError at all (transport failure, credential refresh, ...):
395+
# must still be wrapped so ``except ChatClientException`` callers never see it raw.
396+
(RuntimeError("connection reset"), ChatClientException),
392397
],
393398
)
394399
async def test_get_response_wraps_sdk_errors(
395-
sdk_exception: genai_errors.APIError, expected_exception: type[Exception]
400+
sdk_exception: Exception, expected_exception: type[Exception]
396401
) -> None:
397402
"""Non-streaming get_response must translate raw google-genai SDK errors into the
398403
framework's ChatClientException hierarchy, matching every other provider
@@ -406,18 +411,30 @@ async def test_get_response_wraps_sdk_errors(
406411

407412
async def test_get_response_streaming_wraps_sdk_errors() -> None:
408413
"""Streaming get_response must translate raw google-genai SDK errors into the
409-
framework's ChatClientException hierarchy too, not just the non-streaming path."""
414+
framework's ChatClientException hierarchy too, both when the call itself fails
415+
and when the failure happens partway through iterating the stream."""
416+
# 1. Failure raised by the generate_content_stream call itself.
410417
client, mock = _make_gemini_client()
411418
mock.aio.models.generate_content_stream = AsyncMock(
412419
side_effect=genai_errors.ClientError(401, {"error": {"message": "invalid api key"}})
413420
)
421+
with pytest.raises(ChatClientInvalidAuthException, match="Gemini"):
422+
async for _ in client.get_response(
423+
messages=[Message(role="user", contents=[Content.from_text("Hi")])], stream=True
424+
):
425+
pass
414426

415-
stream = client.get_response(
416-
messages=[Message(role="user", contents=[Content.from_text("Hi")])],
417-
stream=True,
418-
)
427+
# 2. Failure raised mid-stream, after at least one chunk has been yielded.
428+
async def _raise_after_first_chunk(**_: Any):
429+
yield _make_response([_make_part(text="partial")])
430+
raise genai_errors.ClientError(403, {"error": {"message": "permission denied"}})
431+
432+
client, mock = _make_gemini_client()
433+
mock.aio.models.generate_content_stream = AsyncMock(return_value=_raise_after_first_chunk())
419434
with pytest.raises(ChatClientInvalidAuthException, match="Gemini"):
420-
async for _ in stream:
435+
async for _ in client.get_response(
436+
messages=[Message(role="user", contents=[Content.from_text("Hi")])], stream=True
437+
):
421438
pass
422439

423440

0 commit comments

Comments
 (0)