1818 AsyncCallbackManagerForLLMRun ,
1919 CallbackManagerForLLMRun ,
2020)
21- from langchain_core .exceptions import ContextOverflowError , OutputParserException
21+ from langchain_core .exceptions import (
22+ ContextOverflowError ,
23+ ModelAPIError ,
24+ ModelAuthenticationError ,
25+ ModelConnectionError ,
26+ ModelInvalidRequestError ,
27+ ModelNotFoundError ,
28+ ModelPermissionDeniedError ,
29+ ModelRateLimitError ,
30+ ModelTimeoutError ,
31+ OutputParserException ,
32+ )
2233from langchain_core .language_models import (
2334 LanguageModelInput ,
2435 ModelProfile ,
@@ -910,6 +921,46 @@ class AnthropicContextOverflowError(anthropic.BadRequestError, ContextOverflowEr
910921 """BadRequestError raised when input exceeds Anthropic's context limit."""
911922
912923
924+ class AnthropicAuthenticationError (
925+ anthropic .AuthenticationError , ModelAuthenticationError
926+ ):
927+ """Anthropic authentication error classified as a LangChain model error."""
928+
929+
930+ class AnthropicPermissionDeniedError (
931+ anthropic .PermissionDeniedError , ModelPermissionDeniedError
932+ ):
933+ """Anthropic permission error classified as a LangChain model error."""
934+
935+
936+ class AnthropicInvalidRequestError (anthropic .BadRequestError , ModelInvalidRequestError ):
937+ """Anthropic bad-request error classified as a LangChain model error."""
938+
939+
940+ class AnthropicModelNotFoundError (anthropic .NotFoundError , ModelNotFoundError ):
941+ """Anthropic not-found error classified as a LangChain model error."""
942+
943+
944+ class AnthropicRateLimitError (anthropic .RateLimitError , ModelRateLimitError ):
945+ """Anthropic rate-limit error classified as a LangChain model error."""
946+
947+
948+ class AnthropicAPIError (anthropic .InternalServerError , ModelAPIError ):
949+ """Anthropic server error classified as a LangChain model error."""
950+
951+
952+ class AnthropicOverloadedError (anthropic .OverloadedError , ModelAPIError ):
953+ """Anthropic overloaded error (HTTP 529) classified as a LangChain model error."""
954+
955+
956+ class AnthropicConnectionError (anthropic .APIConnectionError , ModelConnectionError ):
957+ """Anthropic connection error classified as a LangChain model error."""
958+
959+
960+ class AnthropicTimeoutError (anthropic .APITimeoutError , ModelTimeoutError ):
961+ """Anthropic timeout error classified as a LangChain model error."""
962+
963+
913964def _raise_if_authentication_error (e : TypeError ) -> None :
914965 """Re-raise anthropic SDK's missing-credentials `TypeError` with guidance."""
915966 if "Could not resolve authentication method" in str (e ):
@@ -933,7 +984,42 @@ def _handle_anthropic_bad_request(e: anthropic.BadRequestError) -> None:
933984 if ("messages: at least one message is required" ) in e .message :
934985 message = "Received only system message(s). "
935986 warnings .warn (message , stacklevel = 2 )
936- raise e
987+ raise AnthropicInvalidRequestError (
988+ message = e .message , response = e .response , body = e .body
989+ ) from e
990+
991+
992+ def _handle_anthropic_api_error (e : anthropic .APIError ) -> None :
993+ """Re-raise an Anthropic SDK error as its LangChain-classified equivalent."""
994+ if isinstance (e , anthropic .AuthenticationError ):
995+ raise AnthropicAuthenticationError (
996+ message = e .message , response = e .response , body = e .body
997+ ) from e
998+ if isinstance (e , anthropic .PermissionDeniedError ):
999+ raise AnthropicPermissionDeniedError (
1000+ message = e .message , response = e .response , body = e .body
1001+ ) from e
1002+ if isinstance (e , anthropic .NotFoundError ):
1003+ raise AnthropicModelNotFoundError (
1004+ message = e .message , response = e .response , body = e .body
1005+ ) from e
1006+ if isinstance (e , anthropic .RateLimitError ):
1007+ raise AnthropicRateLimitError (
1008+ message = e .message , response = e .response , body = e .body
1009+ ) from e
1010+ if isinstance (e , anthropic .OverloadedError ):
1011+ raise AnthropicOverloadedError (
1012+ message = e .message , response = e .response , body = e .body
1013+ ) from e
1014+ if isinstance (e , anthropic .InternalServerError ):
1015+ raise AnthropicAPIError (
1016+ message = e .message , response = e .response , body = e .body
1017+ ) from e
1018+ # `APITimeoutError` subclasses `APIConnectionError`, so check it first.
1019+ if isinstance (e , anthropic .APITimeoutError ):
1020+ raise AnthropicTimeoutError (e .request ) from e
1021+ if isinstance (e , anthropic .APIConnectionError ):
1022+ raise AnthropicConnectionError (message = e .message , request = e .request ) from e
9371023 raise
9381024
9391025
@@ -1681,6 +1767,8 @@ def _stream(
16811767 yield chunk
16821768 except anthropic .BadRequestError as e :
16831769 _handle_anthropic_bad_request (e )
1770+ except anthropic .APIError as e :
1771+ _handle_anthropic_api_error (e )
16841772
16851773 async def _astream (
16861774 self ,
@@ -1718,6 +1806,8 @@ async def _astream(
17181806 yield chunk
17191807 except anthropic .BadRequestError as e :
17201808 _handle_anthropic_bad_request (e )
1809+ except anthropic .APIError as e :
1810+ _handle_anthropic_api_error (e )
17211811
17221812 def _make_message_chunk_from_anthropic_event (
17231813 self ,
@@ -2011,6 +2101,8 @@ def _generate(
20112101 data = self ._create (payload )
20122102 except anthropic .BadRequestError as e :
20132103 _handle_anthropic_bad_request (e )
2104+ except anthropic .APIError as e :
2105+ _handle_anthropic_api_error (e )
20142106 return self ._format_output (data , ** kwargs )
20152107
20162108 async def _agenerate (
@@ -2025,6 +2117,8 @@ async def _agenerate(
20252117 data = await self ._acreate (payload )
20262118 except anthropic .BadRequestError as e :
20272119 _handle_anthropic_bad_request (e )
2120+ except anthropic .APIError as e :
2121+ _handle_anthropic_api_error (e )
20282122 return self ._format_output (data , ** kwargs )
20292123
20302124 def _get_llm_for_structured_output_when_thinking_is_enabled (
0 commit comments