Introducing a new way to review your PRs.

Read the announcement

aio-libs / aiobotocore

asyncio support for botocore library using aiohttp

View on GitHub

18 documents · Updated Nov 20, 2026

docs / session-and-client-api

Session and client API

The asynchronous session and client API gives applications a low-level, boto-compatible way to call AWS services without blocking the event loop. An AioSession holds shared service and credential state, while each service client turns that state into an async operation surface and manages the connections used by that client.

API mental model

The API has two main objects with separate roles. The session is the application-level starting point; the client is the short-lived service handle used for requests.

ObjectPurposeMain result
AioSessionSelect shared settings, credentials, service data, and event behaviorA usable service client
AioBaseClientExpose one service's operations and request helpersAsync service responses

An application normally creates one session for a group of related clients. It then creates a client for each service and region that it needs. This keeps shared state in one place while keeping each client's endpoint and connection lifetime explicit.

Session surface

Session creation

aiobotocore.session.get_session() is the normal entry point for a new session. The returned session starts with botocore-compatible settings and aiobotocore's async event behavior, then accepts application settings before client creation.

The session can also provide service data and service models. These describe the operations, request shapes, endpoints, and optional helpers available to a service. Applications usually reach this data through client creation rather than loading it directly.

References: aiobotocore/session.py• L44-59 aiobotocore/session.py• L121-142 aiobotocore/session.py• L356-360

Shared session state

The session is the shared coordination point for values that affect more than one client. Credential lookup is lazy: the first client that needs credentials loads them, and later clients can use the session's stored credential object unless they receive explicit client credentials.

Session-level configuration also influences the async runtime used by credential lookup and service clients. A default client configuration can select the HTTP backend, while per-client configuration can refine that choice for one client.

Shared concernEffect on client creation
CredentialsSupplies signing credentials when the client does not receive explicit credentials
Default client configurationSupplies defaults such as the HTTP backend and connection behavior
Service data loaderSupplies the model and endpoint data for the requested service
Event systemLets session and client hooks observe or adjust the client lifecycle
User-agent settingsAdds aiobotocore and botocore identity to requests

References: aiobotocore/session.py• L61-65 aiobotocore/session.py• L78-83 aiobotocore/session.py• L107-119 aiobotocore/session.py• L205-225 aiobotocore/session.py• L247-319

Client surface

Client construction

AioSession.create_client() is the bridge from shared session state to a service-specific client. It accepts the service name plus optional region, API version, endpoint, TLS verification, credentials, and client configuration. The session resolves the final values, loads the service description, and returns an async context manager for the client.

The client is ready to use inside the context. Its generated methods match the operations in the selected service model, so application code passes service-shaped keyword arguments and awaits the response.

For the detailed creation rules and customization points, see Client construction and customization.

References: aiobotocore/session.py• L187-204 aiobotocore/session.py• L215-245 aiobotocore/session.py• L307-336 aiobotocore/client.py• L42-126

Service operations

Each client exposes the selected service's operations as async methods. A call validates and shapes the input, resolves the endpoint, applies authentication and request features, sends the request, and returns parsed service data or raises a service or transport error.

The client also provides helpers for service features that span several calls. A paginator represents a pageable operation, and a waiter represents a service condition that the application can wait for. These helpers are available only when the service model defines the requested feature.

References: aiobotocore/client.py• L330-451 aiobotocore/client.py• L573-645 aiobotocore/client.py• L647-671

Client resource lifetime

The client owns the endpoint connections used by its requests. Use async with around create_client() so the client enters its async HTTP resources before the first call and closes them when the block ends.

This lifetime rule matters when a session creates more than one client. The session supplies shared state, but each client still has a distinct usable request handle that must leave its context before the application discards it.

References: aiobotocore/session.py• L31-42 aiobotocore/client.py• L349-351 aiobotocore/client.py• L673-678

Session-to-client flow

The following flow shows how application settings and shared session state become an async service client.

flowchart LR
    A[Application] --> B[get_session()]
    B --> C[AioSession]
    C --> D[create_client(service, settings)]
    D --> E[Resolve region, config, and credentials]
    E --> F[Load service model and endpoint data]
    F --> G[Create service client]
    G --> H[Enter async context]
    H --> I[Await service operation]
    I --> J[Parsed response or error]
    H --> K[Close client resources]

The session performs setup before the client is entered. The client then owns the request path until its context closes, while the session can remain available for another client.

Async runtime model

The API supports an asyncio HTTP path by default and can use an HTTPX-based path through client configuration. The selected HTTP session determines the async primitive set used by related work such as credential retrieval and pagination, so a custom backend must match the runtime used by the application.

The default configuration uses AIOHTTPSession. HttpxSession selects the AnyIO path, which supports asyncio and other AnyIO-compatible runtimes. Backend-specific connection options are validated by AioConfig before client construction.

For session setup and cleanup rules, see Session lifecycle. For backend and client option choices, see Client construction and customization.

References: aiobotocore/_async_primitives.py• L8-16 aiobotocore/config.py• L43-88 aiobotocore/session.py• L61-65 aiobotocore/client.py• L600-607

Typical usage

The normal usage pattern creates a session once and scopes each client with an async context manager.

from aiobotocore.session import get_session

session = get_session()

async with session.create_client("s3", region_name="us-west-2") as client:
    response = await client.list_buckets()

Credential discovery uses the normal botocore sources when the application does not pass credentials to the client. An application can instead pass explicit credentials or set them on the session when it must control the source.

References: aiobotocore/session.py• L107-119 aiobotocore/session.py• L249-276 docs/tutorial.rst• L27-55

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the session's credential and default-config rules aligned with the values passed into nested clients created for credential or endpoint work.
  • Keep the configured HTTP backend aligned with the async primitive selection used by clients, paginators, and credential providers.
  • Keep the async context-manager contract aligned between create_client(), client entry and exit, and the underlying endpoint session.
  • Keep service model loading changes aligned with the availability of generated operations, paginators, waiters, and endpoint data.

Important failure modes

TriggerConsequenceGuard
Only one of the explicit access key or secret key is suppliedClient creation raises a partial-credentials errorSupply both values or use session credential discovery
A client is used without entering its async contextIts HTTP session is not ready for normal useUse async with session.create_client(...)
A requested operation has no paginator modelget_paginator() raises OperationNotPageableErrorCheck whether the operation can paginate before requesting a paginator
A requested waiter is absent or has an invalid nameget_waiter() raises ValueErrorUse a waiter defined by the selected service model
Backend-incompatible connection options are passed to AioConfigConfiguration validation raises before requests startUse options supported by the selected HTTP backend

Key files

FileWhy to read it
aiobotocore/session.pyDefines session creation, shared state, credential access, and the session-to-client boundary
aiobotocore/client.pyDefines the async client contract, service operation calls, paginators, waiters, and resource cleanup
aiobotocore/config.pyDefines async client configuration and the supported HTTP backend choices
aiobotocore/_async_primitives.pyShows how the HTTP backend selects the async runtime primitives
docs/tutorial.rstGives the shortest end-to-end example of session and client use