Skip to content

Instantly share code, notes, and snippets.

@nyrahul
Last active June 8, 2026 04:06
Show Gist options
  • Select an option

  • Save nyrahul/aa21a9759421b52f245e32545452fb1e to your computer and use it in GitHub Desktop.

Select an option

Save nyrahul/aa21a9759421b52f245e32545452fb1e to your computer and use it in GitHub Desktop.
check access error with AWS Bedrock Anthropic model
# Use the native inference API to send a text message to Anthropic Claude.
# requirements
# boto3==1.42.25
# botocore==1.42.26
import boto3
import json
from botocore.exceptions import ClientError
# Create a Bedrock Runtime client in the AWS Region of your choice.
client = boto3.client("bedrock-runtime", region_name="us-east-1",aws_access_key_id="",
aws_secret_access_key="")
# Set the model ID, e.g., Claude 3 Haiku.
model_id = "us.anthropic.claude-haiku-4-5-20251001-v1:0"
# Define the prompt for the model.
prompt = "Describe the purpose of a 'hello world' program in one line."
# Format the request payload using the model's native structure.
native_request = {
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 512,
"temperature": 0.5,
"messages": [
{
"role": "user",
"content": [{"type": "text", "text": prompt}],
}
],
}
# Convert the native request to JSON.
request = json.dumps(native_request)
try:
# Invoke the model with the request.
response = client.invoke_model(modelId=model_id, body=request)
except (ClientError, Exception) as e:
print(f"ERROR: Can't invoke '{model_id}'. Reason: {e}")
exit(1)
# Decode the response body.
model_response = json.loads(response["body"].read())
# Extract and print the response text.
response_text = model_response["content"][0]["text"]
print(response_text)
@nyrahul

nyrahul commented Jun 8, 2026

Copy link
Copy Markdown
Author

You need to fulfill the requirements.txt

boto3==1.42.25
botocore==1.42.26

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment