Skip to content

Bedrock using Python SDK (boto3) doesn't work

0

I run this code import os import boto3 import json from botocore.exceptions import ClientError

def ask_bedrock_question(question, model_id): """ Ask a question to AWS Bedrock and get a response """ bedrock_runtime = boto3.client( service_name='bedrock-runtime', region_name='eu-west-3' )

# Use the correct request format for different model families
if "anthropic" in model_id:
    body = {
        "anthropic_version": "bedrock-2023-05-31",
        "max_tokens": 1000,
        "messages": [
            {
                "role": "user",
                "content": question
            }
        ]
    }
elif "amazon.nova" in model_id:
    body = {
        "messages": [
            {
                "role": "user",
                "content": [{"text": question}]
            }
        ],
        "inferenceConfig": {
            "max_new_tokens": 1000
        }
    }
elif "amazon.titan-text" in model_id:
    body = {
        "inputText": question,
        "textGenerationConfig": {
            "maxTokenCount": 1000,
            "temperature": 0.7
        }
    }
else:
    # Generic format for other models
    body = {
        "prompt": question,
        "max_tokens": 1000,
        "temperature": 0.7
    }

try:
    response = bedrock_runtime.invoke_model(
        body=json.dumps(body),
        modelId=model_id,
        accept="application/json",
        contentType="application/json"
    )

    response_body = json.loads(response.get('body').read())

    # Parse response based on model type
    if "anthropic" in model_id:
        answer = response_body['content'][0]['text']
    elif "amazon.nova" in model_id:
        answer = response_body['output']['message']['content'][0]['text']
    elif "amazon.titan-text" in model_id:
        answer = response_body['results'][0]['outputText']
    else:
        # Try to find text in common response fields
        answer = (response_body.get('completion') or
                  response_body.get('text') or
                  response_body.get('generated_text') or
                  str(response_body))

    return answer

except Exception as e:
    return f"Error: {str(e)}"

def list_inference_profiles(): """List available inference profiles""" try: bedrock = boto3.client('bedrock', region_name='us-east-1') response = bedrock.list_inference_profiles()

    print("=== Available Inference Profiles ===")
    profiles = []
    for profile in response.get('inferenceProfileSummaries', []):
        profile_info = {
            'id': profile['inferenceProfileId'],
            'name': profile.get('inferenceProfileName', 'N/A'),
            'models': profile.get('models', [])
        }
        profiles.append(profile_info)
        print(f"Profile: {profile_info['id']}")
        print(f"  Name: {profile_info['name']}")
        if profile_info['models']:
            print(f"  Models: {[m.get('modelId', 'N/A') for m in profile_info['models']]}")
        print()

    return profiles

except Exception as e:
    print(f"Error listing inference profiles: {e}")
    return []

def test_models_and_profiles(): """Test both direct model IDs and inference profiles"""

# First try older, stable Claude models that should work with direct IDs
stable_models = [
    "anthropic.claude-3-haiku-20240307-v1:0",
    "anthropic.claude-3-5-sonnet-20240620-v1:0",
    "anthropic.claude-3-5-haiku-20241022-v1:0",
]

# Test Amazon models (usually more reliable)
amazon_models = [
    "amazon.nova-lite-v1:0",
    "amazon.nova-micro-v1:0",
    "amazon.titan-text-express-v1",
    "amazon.titan-text-lite-v1",
]

# Test other models
other_models = [
    "cohere.command-r-v1:0",
    "meta.llama3-2-3b-instruct-v1:0",
    "meta.llama3-1-8b-instruct-v1:0",
]

all_models = stable_models + amazon_models + other_models
question = "Hello, how are you?"

print("=== Testing Direct Model IDs ===")
for model_id in all_models:
    print(f"\n🧪 Testing: {model_id}")
    result = ask_bedrock_question(question, model_id)

    if not result.startswith("Error:"):
        print(f"✅ SUCCESS with {model_id}")
        print(f"Response: {result[:150]}...")
        return model_id, "direct"
    else:
        print(f"❌ Failed: {result[:100]}...")

print("\n=== Testing Inference Profiles ===")
profiles = list_inference_profiles()

for profile in profiles:
    profile_id = profile['id']
    print(f"\n🧪 Testing profile: {profile_id}")
    result = ask_bedrock_question(question, profile_id)

    if not result.startswith("Error:"):
        print(f"✅ SUCCESS with profile {profile_id}")
        print(f"Response: {result[:150]}...")
        return profile_id, "profile"
    else:
        print(f"❌ Failed: {result[:100]}...")

return None, None

if name == "main": working_model, model_type = test_models_and_profiles()

if working_model:
    print(f"\n🎉 SUCCESS! Use this {model_type}: {working_model}")

    # Test with your original question
    question = "What are the benefits of cloud computing?"
    answer = ask_bedrock_question(question, working_model)
    print(f"\nFinal test:")
    print(f"Question: {question}")
    print(f"Answer: {answer}")

    # Provide the working code
    print(f"\n📝 Your working code:")
    print(f'model_id = "{working_model}"')
    print(f'answer = ask_bedrock_question("Your question here", model_id)')

else:
    print("\n❌ No models or profiles worked.")
    print("\n🔧 Try these steps:")
    print("1. Wait 30 minutes and try again")
    print("2. Try a different region: us-west-2 or eu-west-1")
    print("3. Check your AWS account Bedrock quotas")
    print("4. Contact AWS Support")

And getting this output:

<summarized> ❌ No models or profiles worked. </summarized>

Region is fine, AWS creds are fine. In fact, the user is an admin user.

What am I doing wrong, what am I overlooking?

asked a year ago368 views

2 Answers
0

Based on your code and the error you're experiencing, there are a few issues that could be causing the problem:

  1. API Usage: You're using invoke_model with different request body formats, but the code might not be correctly formatted for each model. For Amazon Bedrock, it's recommended to use the newer converse API for most models, especially when working with conversation-style interactions.

  2. Region Availability: You're using eu-west-3 for the bedrock-runtime client but us-east-1 for listing inference profiles. Not all Bedrock models are available in all regions. Make sure the models you're trying to use are available in eu-west-3.

  3. Request Format: The request format for different models varies significantly. For example, the Anthropic Claude models require specific formatting with the anthropic_version parameter.

  4. Error Handling: Your code catches exceptions but doesn't provide detailed error information that might help diagnose the issue.

Here's a simplified approach using the recommended converse API for most modern models:

import boto3
from botocore.exceptions import ClientError

# Create a Bedrock Runtime client
client = boto3.client("bedrock-runtime", region_name="eu-west-3")

# Set the model ID (try one of these models)
model_id = "anthropic.claude-3-haiku-20240307-v1:0"  # or another model ID

# Define your question
user_message = "Hello, how are you?"

# Create conversation format
conversation = [
{
"role": "user",
"content": [{"text": user_message}],
}
]

try:
# Send the message using the converse API
response = client.converse(
modelId=model_id,
messages=conversation,
inferenceConfig={"maxTokens": 500, "temperature": 0.5},
)

# Extract and print the response text
response_text = response["output"]["message"]["content"][0]["text"]
print(response_text)

except ClientError as e:
print(f"ERROR: Can't invoke '{model_id}'. Reason: {e}")

If this still doesn't work, check:

answered a year ago

0

My requirements.txt:

torch==2.6.0 transformers==4.51.3 timm==1.0.15 PyMuPDF==1.25.5 Pillow==11.2.1 pytesseract opencv-python boto3>=1.34.0 botocore>=1.34.0 python-dotenv==1.1.0

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.