Get startedGet started for free

Implementing model fallbacks

In production AI applications, relying on a single model can lead to service interruptions. This exercise implements a fallback from Claude to Nova Text Lite when encountering issues. This implementation ensures continuous service while handling model-specific requirements.

The json and boto3 libraries, and the bedrock client, are preloaded.

This exercise is part of the course

Introduction to Amazon Bedrock

View Course

Exercise instructions

  • Wrap the primary Claude model call in a try block.
  • In the except block, handle errors using ClientError and call the Nova Text Lite model.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

def process_with_fallback(prompt):
    # Handle errors with a try-except block
    ____:
        response = bedrock.invoke_model(
          modelId="anthropic.claude-3-5-sonnet-20240620-v1:0",
          body=json.dumps({"anthropic_version": "bedrock-2023-05-31", "max_tokens": 100,
                           "messages": [{"role": "user", "content": [{"type": "text", "text": prompt}]}]}))
        return json.loads(response["body"].read().decode())["content"][0]["text"]
    ____:
        fallback = bedrock.invoke_model(
            modelId="amazon.nova-lite-v1:0",
            body=json.dumps({"messages": [{"role": "user", "content": [{"text": prompt}]}]}))
        return json.loads(fallback["body"].read().decode())["output"]["message"]["content"][0]["text"]

print(process_with_fallback("Explain AWS Lambda"))
Edit and Run Code