Paginators and waiters
Paginators and waiters provide async ways to manage service operations that need more than one request. A paginator walks through a result set page by page, while a waiter polls until a modeled condition reaches a final state. Both keep botocore-compatible service rules while allowing the caller to await network work without blocking the event loop.
This page is a child of Asynchronous service features. Event streams covers the separate case where a service sends a long-lived stream of events.
Service interaction patterns
Pagination handles a large result set as a sequence of service responses. Waiting handles a changing remote resource as a sequence of status checks.
Pagination
The paginator starts with the caller's operation arguments and uses the service's modeled continuation data to request later pages. The caller can consume complete pages or extract items from selected response fields.
Pagination supports these caller-facing forms:
- Page iteration:
async foryields each service response as it arrives. - Item search: a JMESPath expression selects values from each page and yields them as an async stream.
- Full result handling: all pages are combined into one result, with continuation data retained when a limit stops the scan.
References: aiobotocore/client.py• L573-645 aiobotocore/paginate.py• L13-19 aiobotocore/paginate.py• L101-109
Waiting
The waiter starts a modeled service operation and checks its response against ordered acceptors. An acceptor is a rule that maps a response to a state such as success, failure, or continued waiting.
The caller receives the final operation response when the success state matches. A waiter raises WaiterError when the service reports a terminal failure or when polling reaches its attempt limit without success.
References: aiobotocore/client.py• L647-671 aiobotocore/waiter.py• L25-85 aiobotocore/waiter.py• L96-158
Pagination lifecycle
The pagination lifecycle turns one logical list operation into a controlled async sequence of requests. Each yielded page advances the sequence only after the caller asks for the next item.
Page iteration
When the caller requests a page, the paginator awaits the underlying client operation. It then yields the service response and uses the response's next token to prepare the next request.
The sequence ends when the service returns no next token. A configured item limit can end it earlier, either at a page boundary or inside a page. The paginator preserves the position needed to resume from that limit.
flowchart TD
start[Start with operation arguments] --> request[Await service request]
request --> yield[Yield current page]
yield --> token{Next token available?}
token -->|No| done[Finish iteration]
token -->|Yes| limit{Item limit reached?}
limit -->|Yes| resume[Save resume position]
limit -->|No| next[Build next request]
next --> request
resume --> done
References: aiobotocore/paginate.py• L21-99 tests/test_basic_s3.py• L87-117 tests/test_basic_s3.py• L133-148
Result aggregation
Full-result handling combines values from each modeled result field into one response. Lists are appended across pages, scalar values are combined according to the pagination model, and fields that are not page aggregates are kept with the result.
The combined result can include a NextToken when a limit stops the operation before all pages are read. This lets a later pagination call continue from the saved position rather than start again.
References: aiobotocore/paginate.py• L121-168 tests/test_basic_s3.py• L328-359
Continuation state
Pagination configuration can limit the number of returned items or set a starting token. A starting token may identify both a service page and an offset within that page, so repeated calls can move through a result set in small bounded portions.
The paginator rejects a response that repeats the same next token. Without this guard, a service response could cause an endless request loop.
References: aiobotocore/paginate.py• L24-36 aiobotocore/paginate.py• L61-99 tests/test_basic_s3.py• L362-401
Waiter lifecycle
The waiter lifecycle turns a long-running service action into a bounded polling workflow. Each poll is an awaited operation call, and the waiter evaluates the new response before deciding whether to return, raise, or sleep.
Polling cycle
The waiter loads its default delay and maximum attempt count from the service model. A call-specific WaiterConfig can replace either value for one wait operation.
Each cycle follows this order:
- Call the modeled service operation and await its response.
- Test the response against the acceptors in model order.
- Return on success or raise on terminal failure.
- Raise if the maximum attempt count is reached.
- Sleep for the configured delay before the next poll.
sequenceDiagram
participant Caller
participant Waiter
participant Service
Caller->>Waiter: await wait(arguments)
loop Until final state or limit
Waiter->>Service: Poll operation
Service-->>Waiter: Response or service error
Waiter->>Waiter: Match response to acceptor
alt Success
Waiter-->>Caller: Return response
else Failure or invalid error
Waiter-->>Caller: Raise WaiterError
else Waiting and attempts remain
Waiter->>Waiter: Await configured delay
end
end
References: aiobotocore/waiter.py• L101-159 tests/test_waiter.py• L63-72 tests/test_dynamodb.py• L114-131
Completion states
A success acceptor ends the workflow and returns the matching operation result. A failure acceptor ends it with a WaiterError that includes the last response and the model's explanation.
If no acceptor matches an error response that the waiter recognizes, the waiter also raises WaiterError. If polling only reaches the attempt limit, the error reports that the maximum was exceeded and includes the last response.
References: aiobotocore/waiter.py• L111-158 tests/test_dynamodb.py• L102-110
Delay scheduling
The delay occurs only after a non-final poll and before the next operation call. It uses an async sleep primitive, so waiting gives control back to the active async runtime instead of blocking other work.
The waiter selects the sleep primitive from the client's HTTP session. The default client path uses asyncio; the HTTPX path uses an anyio sleep that also supports Trio-based execution.
References: aiobotocore/waiter.py• L49-56 aiobotocore/waiter.py• L96-99 aiobotocore/waiter.py• L146-159 aiobotocore/waiter.py• L162-170 aiobotocore/_async_primitives.py• L8-16
Operation result handling
Paginators and waiters build on the same async client operation surface. This means each page or poll follows the normal request path and retains the service result shape expected by botocore callers.
Page results
Page iteration returns parsed service responses rather than coroutine objects. Search iteration then applies its expression to each returned page, while full-result handling returns one parsed structure after all requested pages have been consumed.
References: aiobotocore/paginate.py• L101-109 aiobotocore/paginate.py• L121-168 tests/test_basic_s3.py• L120-148
Poll results
Waiter polling treats service errors as response data long enough for the waiter model to classify them. The operation wrapper converts a ClientError into its response for acceptor matching, while normal successful calls return their parsed result.
This separation allows a modeled condition such as “resource not found yet” to mean “keep waiting” when the waiter defines it that way. An unhandled valid waiter error remains a terminal WaiterError rather than being returned as a successful operation result.
References: aiobotocore/waiter.py• L88-94 aiobotocore/waiter.py• L111-133 aiobotocore/client.py• L443-451
Change checklist
When these files or sections are changed, remember to consider these:
- Changes to a pagination model's input token, output token, result fields, or limits must remain consistent with page iteration, full-result aggregation, and resume tokens.
- Changes to waiter acceptors must preserve the mapping between service responses and success, failure, and waiting states.
- Changes to client feature selection must keep paginator and waiter behavior aligned with the client's async runtime and HTTP session.
- Changes to operation error handling must preserve the waiter path that classifies service errors before deciding whether to continue or fail.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Caller requests a paginator for an operation without pagination support | OperationNotPageableError is raised before iteration starts | Check client.can_paginate() before get_paginator() when support is not known |
| Service returns the same next token twice | Pagination raises PaginationError instead of issuing an endless sequence of requests | Keep the repeated-token check tied to token advancement |
| Waiter name is absent or invalid for the service | ValueError is raised when the waiter is requested | Use a waiter name listed by the service model |
Polling reaches MaxAttempts without a success state | WaiterError is raised with the last response | Set a suitable attempt limit and handle the terminal error |
| Waiter matches a terminal failure or an unhandled valid service error | WaiterError is raised instead of returning a misleading success | Keep failure acceptors and error classification aligned with the service model |
Key files
| File | Why to read it |
|---|---|
| aiobotocore/client.py | Shows how clients expose paginator and waiter factories and choose the async variant for the active HTTP session. |
| aiobotocore/paginate.py | Defines page iteration, item search, result aggregation, item limits, and resume behavior. |
| aiobotocore/waiter.py | Defines polling, acceptor state changes, async delays, result return, and waiter errors. |
| tests/test_basic_s3.py | Demonstrates page iteration, item search, full-result limits, and continuation across page boundaries. |
| tests/test_dynamodb.py | Demonstrates successful resource waiting, delay-based polling, and maximum-attempt failure. |