Last active
June 8, 2026 04:06
-
-
Save nyrahul/aa21a9759421b52f245e32545452fb1e to your computer and use it in GitHub Desktop.
check access error with AWS Bedrock Anthropic model
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You need to fulfill the requirements.txt