Python
Error Handling

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 CodeError Type
400BadRequestError
401AuthenticationError
403PermissionDeniedError
404NotFoundError
422UnprocessableEntityError
429RateLimitError
>=500InternalServerError
N/AAPIConnectionError