Client construction and customization
An async service client is built by combining a service model with session state, request configuration, credentials, endpoint data, and event handlers. The result keeps botocore's service-facing behavior while making client creation and service calls awaitable. The client also exposes async pagination, waiters, request transport, and resource cleanup.
Client construction
Client construction starts at aiobotocore.session.AioSession.create_client and ends with a service-specific client whose methods come from the service model. The session resolves shared defaults and credentials first. A client creator then turns those inputs into the model, signer, endpoint, serializer, parser, and event emitter that the client needs.
The public context manager delays the async creation step until entry. This gives callers a safe lifetime boundary for the client's network resources:
flowchart LR
A[Session create_client arguments] --> B[Session defaults and credentials]
B --> C[Service model and endpoint data]
C --> D[Client arguments]
D --> E[Generated async service client]
E --> F[Async close on context exit]
The main inputs have distinct roles:
| Input group | Purpose |
|---|---|
| Service and API version | Select the service description and operation set. |
| Region and endpoint | Select where requests go. A custom endpoint bypasses endpoint discovery registration. |
| Config and verification settings | Control request behavior, transport limits, TLS checks, proxies, and the async HTTP backend. |
| Credentials or unsigned mode | Supply request signing material or disable signing. |
| Session components | Supply data loading, event, parser, exception, retry, and user-agent behavior. |
References: aiobotocore/session.py• L187-336 aiobotocore/client.py• L42-126
Configuration and endpoint assembly
Configuration is merged at the session boundary before it reaches client construction. Explicit client configuration takes priority over the session's default client configuration. The final configuration also carries aiobotocore-specific choices such as the HTTP session class, connector settings, and optional loader-cache warm-up.
Endpoint assembly uses both legacy botocore endpoint routing and service endpoint rules when the service provides them. The endpoint data determines the URL and signing context. Request-specific endpoint rules can later add headers, properties, or updated signing information.
AioConfig validates transport options against the selected backend. The standard backend uses aiohttp primitives. The optional httpx backend uses anyio-compatible primitives, so clients created with it can run under asyncio or Trio. Both paths preserve the same service client contract.
The assembled endpoint owns the HTTP session. It applies TLS and proxy settings, connection limits, timeouts, and connector options. It creates pooled sessions as needed and closes them when the client context ends.
References: aiobotocore/session.py• L205-303 aiobotocore/args.py• L14-104 aiobotocore/config.py• L43-152 aiobotocore/endpoint.py• L322-371 aiobotocore/httpsession.py• L70-136
Event hook customization
Event hooks form the customization layer shared by the session and each client. An event name describes a stage of service data loading, parameter preparation, request creation, sending, response parsing, retry handling, or completion. Handlers can observe a stage, change its inputs, return a replacement response, or add service-specific behavior.
The async emitter keeps botocore's hierarchical event lookup. It checks the most specific event name first and then broader prefixes. It awaits coroutine handlers, so customization can perform async work without blocking the event loop. A handler that returns a value can affect stages that use the first non-empty response.
When a client is created, the system adds handlers for features such as retries, S3 endpoint behavior, endpoint discovery, and service-specific signing helpers. Session-level handlers remain available through the client event emitter, which lets applications and integrations customize the same lifecycle.
References: aiobotocore/hooks.py• L48-93 aiobotocore/client.py• L143-288 aiobotocore/session.py• L127-142 aiobotocore/endpoint.py• L95-112
Operation request lifecycle
An operation method is generated from the service model and exposed on the client with the same operation naming that botocore users expect. A call accepts service parameters and moves through async hooks before it reaches the network.
The request path combines user input with model rules, endpoint selection, signing, checksums, retries, and response parsing. The client reports errors through botocore-compatible exception classes after the response has been parsed.
The operation flow is:
sequenceDiagram
participant Caller
participant Client
participant Hooks
participant Endpoint
participant Service
Caller->>Client: await operation(**parameters)
Client->>Hooks: provide parameters and build request
Hooks-->>Client: updated parameters or context
Client->>Client: resolve endpoint and signing context
Client->>Hooks: before-call
Client->>Endpoint: send prepared request
Endpoint->>Hooks: before-send
Endpoint->>Service: async HTTP request
Service-->>Endpoint: HTTP response
Endpoint->>Hooks: response and parse hooks
Endpoint-->>Client: parsed response or transport error
Client->>Hooks: after-call or after-call-error
Client-->>Caller: result or botocore-compatible exception
Retries remain part of this lifecycle. Standard and legacy retry modes use botocore-compatible decisions. Adaptive retry mode uses async rate control. Delay between attempts uses the active async runtime rather than blocking the event loop.
References: aiobotocore/client.py• L330-571 aiobotocore/endpoint.py• L114-251 aiobotocore/endpoint.py• L272-308 aiobotocore/client.py• L143-219
Pagination and waiter methods
The client adds higher-level operation methods that use the same async operation path. A paginator turns a pageable operation into an async page stream. It follows service tokens, supports result searches, and can combine pages into one result while preserving a resume token.
A waiter turns repeated operation calls into an async state check. It applies service-defined acceptors until it reaches success, a terminal failure, or its attempt limit. The selected HTTP backend determines whether the waiter uses asyncio or anyio sleep primitives.
These helpers are available only when the service model supplies the matching data. Unsupported paginator or waiter names fail early with botocore-style operation errors or clear value errors.
References: aiobotocore/client.py• L573-671 aiobotocore/paginate.py• L13-168 aiobotocore/waiter.py• L25-170 tests/test_client.py• L7-36
Change checklist
When these files or sections are changed, remember to consider these:
- Changes to
AioConfigtransport fields must stay aligned with endpoint creation and with the validation rules of both HTTP backends. - Changes to client argument assembly must preserve the link between endpoint data, signing context, credentials, and the final client metadata.
- New event stages or handler return values must preserve the event names and response rules expected by botocore handlers and integrations.
- Changes to the generated client base must be checked against paginator, waiter, retry, and context-manager behavior because those helpers call client operations and endpoint cleanup.
- Changes to endpoint lifetime or HTTP session selection must also cover nested credential clients and the session's async primitive choice.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Only one explicit access key or secret key is supplied | Client creation raises a partial-credentials error instead of silently using another source. | Supply both explicit values or omit both and let the session load credentials. |
| An unsupported connector option is used with the httpx backend | Configuration validation rejects the client before transport creation. | Use options supported by the selected HTTP session class. |
| A custom endpoint URL is supplied for a service that supports discovery | Endpoint discovery handlers are not registered. | Use a discovered endpoint when discovery behavior is required. |
| A pageable operation is requested from a service model without pagination data | get_paginator raises OperationNotPageableError. | Check can_paginate before creating a paginator. |
| A waiter name is absent from the service waiter model | get_waiter raises a value error. | Use a waiter name supplied by the service model. |
| The client context is exited while a streaming response still needs data | The underlying HTTP session is closed and the stream cannot continue. | Consume or close streaming results before leaving the client context. |
Key files
| File | Why to read it |
|---|---|
| aiobotocore/session.py | Shows how public client arguments become session-resolved client inputs and how the client context is managed. |
| aiobotocore/client.py | Shows client generation, feature handler registration, operation calls, and high-level helpers. |
| aiobotocore/args.py | Shows the boundary where configuration, credentials, endpoint data, signing, serialization, and parsing become client arguments. |
| aiobotocore/config.py | Defines the async client configuration contract and backend-specific transport validation. |
| aiobotocore/endpoint.py | Shows the async endpoint lifecycle, response conversion, retries, and transport handoff. |
| aiobotocore/hooks.py | Defines async event dispatch and botocore handler adaptation. |
| aiobotocore/paginate.py | Shows how pageable operations become async page streams. |
| aiobotocore/waiter.py | Shows how service waiter models become async state checks. |