Skip to content

Pagination Not Working Correctly in boto3 Bedrock Agent Runtime retrieve Operation (Pagination with MaxItems Only)

0

Description: I'm working with AWS Bedrock Agent Runtime in a Lambda function using the boto3 Python SDK. I am attempting to use pagination with the retrieve operation to fetch results from a knowledge base using the function Boto3 Retrieve.

I tried with the following inputs.

page_iterator = paginator.paginate(
                knowledgeBaseId=knowledge_base_id,
                retrievalQuery={'text': query},
                PaginationConfig={
                    'MaxItems': 30,
                    'StartingToken': next_token    
                },
                retrievalConfiguration={
                    'vectorSearchConfiguration': {
                        'overrideSearchType': 'SEMANTIC',
                        'numberOfResults': 100
                    }
                }
            )

Response: Only one page with 30 results.

page_iterator = paginator.paginate(
                knowledgeBaseId=knowledge_base_id,
                retrievalQuery={'text': query},
                PaginationConfig={
                    'MaxItems': 5,
                    'StartingToken': next_token    
                },
                retrievalConfiguration={
                    'vectorSearchConfiguration': {
                        'overrideSearchType': 'SEMANTIC',
                        'numberOfResults': 100
                    }
                }
            )

Response: Only one page with 5 results.

Problem: The retrieve operation works with the MaxItems parameter to limit the total number of results returned in one page. However, I am unable to paginate results correctly with the NextToken. After the first page is retrieved, the NextToken value returned in the response is None, and pagination stops, preventing further pages from being fetched.

Expected Behavior: The function should fetch results across multiple pages based on the NextToken, and the process should continue until there are no more pages of results.

What I have tried: I have correctly set up pagination with the get_paginator function from boto3 and used the paginate method with the PaginationConfig containing the StartingToken and MaxItems parameters. I print out the NextToken after each page request and notice that after the first page, the NextToken is always None, even though there should be additional results (based on the MaxItems set). I confirmed that the retrievalResults are being fetched correctly, but the pagination stops after one request.

Key Points: The issue seems to be with the pagination mechanism; specifically, the NextToken is None after the first page, despite there being more pages to retrieve. The results are correctly fetched using the MaxItems parameter, but pagination does not continue past the first page.

Question: Is there a known issue with pagination in the retrieve operation of Bedrock Agent Runtime? Is the NextToken expected to be returned even when using the MaxItems parameter? What changes should I make to ensure proper pagination across multiple pages?

2 Answers
0

Based on the information provided, it appears that there might be a misunderstanding about how pagination works with the Bedrock Agent Runtime's retrieve operation.

The retrieve operation in the Bedrock Agent Runtime service doesn't actually support pagination in the same way that many other AWS services do. Instead, it allows you to specify the number of results you want to retrieve in a single call using the 'numberOfResults' parameter within the 'vectorSearchConfiguration'.

When you use the retrieve operation, you typically specify the number of results you want to get back in a single call. This is different from pagination where you would get a subset of results and then use a token to get the next set.

Here's how you would typically use the retrieve operation:

  1. You make a call to the retrieve method, specifying the number of results you want.
  2. The service returns up to that number of results in a single response.
  3. There is no 'NextToken' or pagination mechanism because all requested results are returned in that single call.

If you need more results than what can be returned in a single call, you would need to make multiple separate calls to the retrieve method, potentially adjusting your query or other parameters to get different results.

The 'MaxItems' parameter you're using is likely part of the boto3 paginator configuration, but it doesn't apply to this particular operation in the way you're expecting. The Bedrock Agent Runtime's retrieve operation is designed to return all requested results in one go, rather than paginating across multiple calls.

To get the behavior you're looking for, you would need to:

  1. Determine the maximum number of results you can retrieve in a single call.
  2. Make multiple calls to the retrieve method if you need more results than can be returned in one call.
  3. Potentially adjust your query or other parameters between calls to get different sets of results.

If you need to process large numbers of results, you might need to implement your own pagination logic at the application level, making multiple retrieve calls and managing the results yourself.
Sources
Amazon Bedrock Knowledge Bases now supports hybrid search | AWS Machine Learning Blog

answered 2 years ago

0

Here I am not using the direct "retrieve" operation from the Bedrock Runtime Agent, it's actually "retreive" with Paginators which is available on a client instance via the "get_paginator" method.

answered 2 years 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.