Error Handling
Primarily, errors happen when the library is unable to connect to the API (from network connection problems).
Catching Errors
from bedrock_anthropic import AnthropicBedrock
anthropic = AnthropicBedrock(
access_key=os.getenv("AWS_ACCESS_KEY"),
secret_key=os.getenv("AWS_SECRET_KEY"),
region=os.getenv("AWS_REGION")
)
try:
completion = anthropic.Completion.create(
model="anthropic.claude-v2",
prompt="Why is the sky blue?",
max_tokens_to_sample=300
)
except anthropic_bedrock.APIConnectionError as e:
print("The server could not be reached")
except anthropic_bedrock.RateLimitError as e:
print("A 429 status code was received; we should back off a bit.")
except anthropic_bedrock.APIStatusError as e:
print("Another non-200-range status code was received")
Retries
from bedrock_anthropic import AnthropicBedrock
anthropic = AnthropicBedrock(
# The defult is 10
max_retries=3,
access_key=os.getenv("AWS_ACCESS_KEY"),
secret_key=os.getenv("AWS_SECRET_KEY"),
region=os.getenv("AWS_REGION")
)
Error Codes
Status Code | Error Type |
---|---|
400 | BadRequestError |
401 | AuthenticationError |
403 | PermissionDeniedError |
404 | NotFoundError |
422 | UnprocessableEntityError |
429 | RateLimitError |
>=500 | InternalServerError |
N/A | APIConnectionError |