Skip to content

Cohere rerank on Bedrock - api_version missing in the sdk  #613

@Othmane796

Description

@Othmane796

SDK Version (required)
Provide the version you are using. To get the version, run the following python snippet

import cohere

print(cohere.__version__) # 5.6.1

version is : 5.13.1

Describe the bug
A clear and concise description of what the bug is.

I'm unable to call the cohere rerank model through the AWS Bedrock client.
Going through the cohere v2 client it works fine:

co_cohere = cohere.ClientV2()
top_n = 3
query = "What is the capital of the United States?"
docs = ["text1", .."textN"]

response = co_cohere.rerank(
    model="rerank-v3.5",
    query=query,
    documents=docs,
    top_n=top_n,
)
# works

However following the documentation here and also reading the code in this sdk If I run :

co_bedrock = cohere.BedrockClient(aws_region="eu-central-1")

response_bedrock = co_bedrock.rerank(
    model="cohere.rerank-v3-5:0",
    query=query,
    documents=docs,
    top_n=top_n, 
)

I get the following error:

BadRequestError: status_code: 400, body: {'meta': {'billed_units': {'input_tokens': -1.0, 'output_tokens': -1.0}, 'tokens': {'input_tokens': -1.0, 'output_tokens': -1.0}}, 'message': 'Malformed input request: #/api_version: 1 is not greater or equal to 2, please reformat your input and try again.'}

I tried adding 'api_version' as parameter in both the .rerank() and .BedrockClient() functions but it doesn't work.

Good news is I'm able to go through the aws boto3 sdk and make it work with this:

import boto3
import json
bedrock_client = boto3.client(service_name="bedrock-runtime", region_name="eu-central-1")
json_params = {
    "query": query,
    "documents": docs,
    "top_n": top_n,
    "api_version": 2
}

result_bedrock = bedrock_client.invoke_model(
    modelId="cohere.rerank-v3-5:0",
    body=json.dumps(json_params),
    contentType="application/json",
    accept="application/json",
)
raw = result_bedrock["body"].read() # works

Question: Is what I did the expected solution ? What's the recommended way to use cohere though AWS Bedrock ?

Screenshots
If applicable, add screenshots to help explain your problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions