boto / boto3

AWS SDK for Python
https://aws.amazon.com/sdk-for-python/
Apache License 2.0
9.07k stars 1.87k forks source link

Validation Exception: Invalid API version when calling InvokeModel [bedrock-runtime] #4218

Closed aaronlim0919 closed 3 months ago

aaronlim0919 commented 3 months ago

Describe the bug

I am using boto3 to connect bedrock-runtime and invoke model anthropic.claude-3-sonnet-20240229-v1:0. I've check the available models by using list_foundation_models(), the model id I've used is in the list. However, the error message occur when I execute the code:

botocore.errorfactory.ValidationException: An error occurred (ValidationException) when calling the InvokeModel operation: Invalid API version: claude-3-sonnet-20240229-v1:0

And this is my implementation:

bedrock_client = boto3.client('bedrock-runtime',
                       region_name = os.environ['AWS_REGION'],
                       aws_access_key_id = os.environ['AWS_ACCESS_KEY'],
                       aws_secret_access_key = os.environ['AWS_SECRET_ACCESS_KEY'])

messages =  {
                "anthropic_version":'claude-3-sonnet-20240229-v1:0',
                "max_tokens":2000,
                "system":"system prompt",
                "messages":[{
                    "role":"user",
                    "content":"user prompt"
                }],
                "temperature": 0,

            }

response = bedrock_client.invoke_model(
    body = json.dumps(messages),
    contentType = 'application/json',
    accept = 'application/json',
    modelId = 'anthropic.claude-3-sonnet-20240229-v1:0'
)

Expected Behavior

successfully receive the output of anthropic model

Current Behavior

Traceback (most recent call last):
  File "/Users/aaronlim/gofreight/meeting_note_taker/aws_bedrock_test.py", line 27, in <module>
    response = bedrock_client.invoke_model(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/aaronlim/miniconda3/lib/python3.11/site-packages/botocore/client.py", line 565, in _api_call
    return self._make_api_call(operation_name, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/aaronlim/miniconda3/lib/python3.11/site-packages/botocore/client.py", line 1017, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.errorfactory.ValidationException: An error occurred (ValidationException) when calling the InvokeModel operation: Invalid API version: claude-3-sonnet-20240229-v1:0

Reproduction Steps

As the implementation above

Possible Solution

No response

Additional Information/Context

No response

SDK version used

1.34.149 (both boto3 and botocore)

Environment details (OS name and version, etc.)

python 3.11.4, macOS

tim-finnigan commented 3 months ago

Thanks for reaching out, here is the documentation for invoke_model, and for using Anthropic specifically: https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-anthropic-claude-messages.html

As noted there, the anthropic_version value should be bedrock-2023-05-31:

anthropic_version – (Required) The anthropic version. The value must be bedrock-2023-05-31.

When changing that it worked for me. If you run into any issues let me know. For any documentation feedback on this please use the Provide feedback links at the bottom of the API documentation page, and any updates would get propagated to the SDK docs.

github-actions[bot] commented 3 months ago

This issue is now closed. Comments on closed issues are hard for our team to see. If you need more assistance, please open a new issue that references this one.