Skip to content

feat: auto-detect aiohttp transport for async client#739

Open
fern-support wants to merge 2 commits intomainfrom
fer-8644-aiohttp-autodetect
Open

feat: auto-detect aiohttp transport for async client#739
fern-support wants to merge 2 commits intomainfrom
fer-8644-aiohttp-autodetect

Conversation

@fern-support
Copy link
Collaborator

@fern-support fern-support commented Mar 12, 2026

Summary

  • When httpx_aiohttp is installed, the async client automatically uses it as the transport layer, providing native async DNS resolution via aiodns (c-ares) instead of httpx's blocking threadpool-based getaddrinfo. This prevents ConnectError failures under high async concurrency.
  • Users opt in with pip install cohere[aiohttp] — no code changes required. Users who want plain httpx can pass httpx_client=DefaultAsyncHttpxClient().
  • Adds DefaultAioHttpClient and DefaultAsyncHttpxClient convenience classes exported from the top-level cohere package.

Changes

File Change
pyproject.toml Added aiohttp and httpx_aiohttp as optional deps + [aiohttp] extra
src/cohere/_default_clients.py New — DefaultAioHttpClient, DefaultAsyncHttpxClient, COHERE_DEFAULT_TIMEOUT
src/cohere/base_client.py Added _make_default_async_client(), updated AsyncBaseCohere.__init__
src/cohere/__init__.py Exported new convenience classes
tests/test_aiohttp_autodetect.py New — unit tests for auto-detect logic and convenience classes

Test plan

  • pip install -e . — verify httpx_aiohttp is NOT installed, async client uses plain httpx.AsyncClient
  • pip install -e ".[aiohttp]" — verify async client auto-uses HttpxAiohttpClient
  • Pass explicit httpx_client=DefaultAsyncHttpxClient() with aiohttp installed — verify plain httpx is used
  • Pass httpx_client=DefaultAioHttpClient() without aiohttp installed — verify clear RuntimeError
  • Run pytest tests/test_aiohttp_autodetect.py

🤖 Generated with Claude Code


Note

Medium Risk
Changes the default async HTTP transport selection when httpx_aiohttp is installed, which can subtly affect request behavior and dependency resolution. Most logic is isolated and covered by new unit tests, but it impacts all async client users who install the new extra.

Overview
Async client transport auto-detection: the SDK now prefers httpx_aiohttp.HttpxAiohttpClient for AsyncBaseCohere when httpx_aiohttp is available, falling back to plain httpx.AsyncClient otherwise (via new _make_default_async_client).

New opt-in extra + convenience clients: adds optional deps aiohttp/httpx_aiohttp behind a new cohere[aiohttp] extra, and introduces DefaultAioHttpClient (errors with install hint when extra missing) plus DefaultAsyncHttpxClient (applies default timeout/redirect settings), both exported from cohere.

Updates poetry.lock to reflect the new optional dependency group and refreshed lock metadata, and adds tests validating the autodetect and default-client behaviors.

Written by Cursor Bugbot for commit 1b08620. This will update automatically on new commits. Configure here.

When httpx_aiohttp is installed, the async client automatically uses it
as the transport layer, providing native async DNS resolution via aiodns
instead of httpx's blocking threadpool-based getaddrinfo. This prevents
ConnectError failures under high async concurrency.

Users opt in with `pip install cohere[aiohttp]` — no code changes needed.
Users who want plain httpx can pass `httpx_client=DefaultAsyncHttpxClient()`.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

"Dataset": ".types",
"DatasetPart": ".types",
"DefaultAioHttpClient": "._default_clients",
"DefaultAsyncHttpxClient": "._default_clients",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New entries break alphabetical ordering in lookup dict

Low Severity

"DefaultAioHttpClient" and "DefaultAsyncHttpxClient" are inserted between "DatasetPart" and "DatasetType" in the _dynamic_imports dict, splitting the "Dataset*" group and breaking the alphabetical sort convention maintained throughout the file. Alphabetically, "Default" sorts after "Debug" (since 'b' < 'f'), so these entries belong after "DebugV2ChatStreamResponse". The same misordering occurs in __all__, where the entries are placed before the "Debug*" entries instead of after them.

Additional Locations (1)
Fix in Cursor Fix in Web

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant