Repository navigation
Conversation
CohereReranker built its AsyncClient once in __init__ and passed it the same kwargs as the sync client. Its connection pool was bound to the first event loop that used it, so a reranker that outlived that loop failed on the next one with "Event loop is closed", and a user-supplied sync httpx_client reached the AsyncClient, so arank raised "object Response can't be used in 'await' expression". Build the async client on first use via _get_aclient(), rebuild it when the running loop changes, and keep a user-supplied async_httpx_client once built. A sync httpx_client without an async one disables the async client and arank falls back to the sync client with a warning. This is the same handling CohereTextVectorizer got in redis#785.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
CohereRerankerbuilt itsAsyncClientonce in__init__and passed it the same kwargs as the sync client. Two bugs resulted:httpx_client(sync) was forwarded to theAsyncClient, soarankgot a syncResponseback and crashed onawait.Fix
_get_aclient()builds theAsyncClienton first use and rebuilds it when the running loop changes (same patternCohereTextVectorizergot in fix(vectorize): use Cohere's async client for aembed and aembed_many #785).async_httpx_clientis kept and reused across loops.httpx_clientwithout a matching async one disables the async path and falls back to the sync client with a warning.Test plan
pytest tests/unit/test_cohere_reranker.py— 181-line new suite covering lazy init, loop-change rebuild, sync-client fallback, and warning behaviour.🤖 Generated with Claude Code
Note
Medium Risk
Changes async HTTP client lifecycle and httpx kwargs handling for reranking; behavior is covered by new unit tests but affects production async rerank paths.
Overview
Fixes
CohereReranker.arankfailures from eagerly creatingAsyncClientin__init__: connection pools tied to a closed event loop caused "Event loop is closed", and forwarding a synchttpx_clientto the async client caused await errors on sync responses.CohereRerankernow lazily builds the async client via_get_aclient(), reusing it within one loop and rebuilding when the running loop changes (same approach asCohereTextVectorizer). Callers can passasync_httpx_clientfor a dedicated async HTTP client; if only a synchttpx_clientis supplied,aranklogs a warning and falls back torank.httpx_client=Nonestill enables the async path.Adds
tests/unit/test_cohere_reranker.pywith fakes covering lazy init, loop reuse/rebuild, sync fallback, andasync_httpx_clientwiring.Reviewed by Cursor Bugbot for commit 3e0646b. Bugbot is set up for automated code reviews on this repo. Configure here.