Skip to content

Commit 289bd03

Browse files
committed
fix(vectorize): build the Cohere async client per event loop
The async client was created once at init, and its HTTP connection pool is bound to the event loop it first ran on. A vectorizer that outlives a loop, such as one used across two asyncio.run() calls, failed on the next loop with 'Event loop is closed'. The client is now built on first use and rebuilt when the running loop changes; a user-supplied async_httpx_client belongs to the caller's loop and is kept. Also treats httpx_client=None as no sync client rather than one, documents httpx_client and async_httpx_client on __init__ where the API docs render, and trims the unit tests.
1 parent bba6c44 commit 289bd03

2 files changed

Lines changed: 108 additions & 64 deletions

File tree

‎redisvl/utils/vectorize/text/cohere.py‎

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import asyncio
12
import os
23
import warnings
4+
from collections.abc import Callable
35
from typing import TYPE_CHECKING, Any
46

57
from pydantic import ConfigDict
@@ -99,6 +101,11 @@ def __init__(
99101
to Cohere's corresponding embedding types. Defaults to 'float32'.
100102
cache (Optional[EmbeddingsCache]): Optional EmbeddingsCache instance to cache embeddings for
101103
better performance with repeated texts. Defaults to None.
104+
**kwargs: Passed to the Cohere ``Client`` and ``AsyncClient``, for
105+
example ``timeout`` or ``base_url``. A custom ``httpx_client`` is
106+
synchronous, so it goes to the sync client only and the async
107+
methods fall back to it; pass ``async_httpx_client`` (an
108+
``httpx.AsyncClient``) to give the async client its own.
102109
103110
Raises:
104111
ImportError: If the cohere library is not installed.
@@ -123,10 +130,8 @@ def _initialize_client(self, api_config: dict[str, Any] | None, **kwargs):
123130
124131
Args:
125132
api_config: Dictionary with API configuration options
126-
**kwargs: Additional arguments to pass to the Cohere clients.
127-
A custom ``httpx_client`` is synchronous, so it goes to the
128-
sync client only, and async calls fall back to it. Pass
129-
``async_httpx_client`` to give the async client its own.
133+
**kwargs: Additional arguments to pass to the Cohere clients; see
134+
``__init__`` for ``httpx_client`` and ``async_httpx_client``.
130135
131136
Raises:
132137
ImportError: If the cohere library is not installed
@@ -158,15 +163,37 @@ def _initialize_client(self, api_config: dict[str, Any] | None, **kwargs):
158163

159164
# A user-supplied httpx_client is sync and can't be awaited, so without
160165
# an async one the async methods keep falling back to the sync client.
161-
self._aclient = None
162-
if "httpx_client" not in kwargs or async_httpx_client is not None:
166+
self._aclient_factory: Callable[[], Any] | None = None
167+
self._aclient: Any = None
168+
self._aclient_loop: asyncio.AbstractEventLoop | None = None
169+
# A user-supplied async httpx client is bound to the caller's loop, so
170+
# it is created once and never rebuilt.
171+
self._aclient_rebuild_on_new_loop = async_httpx_client is None
172+
if kwargs.get("httpx_client") is None or async_httpx_client is not None:
163173
async_kwargs = dict(kwargs)
164174
if async_httpx_client is not None:
165175
async_kwargs["httpx_client"] = async_httpx_client
166-
self._aclient = AsyncClient(
176+
self._aclient_factory = lambda: AsyncClient(
167177
api_key=api_key, client_name="redisvl", **async_kwargs
168178
)
169179

180+
def _get_aclient(self) -> Any:
181+
"""Return the async Cohere client, or ``None`` if async is disabled.
182+
183+
The client's HTTP connection pool is tied to the event loop it was
184+
created on, so a vectorizer that outlives a loop gets a fresh client
185+
on the next one rather than ``Event loop is closed`` errors.
186+
"""
187+
if self._aclient_factory is None:
188+
return None
189+
loop = asyncio.get_running_loop()
190+
if self._aclient is None or (
191+
self._aclient_rebuild_on_new_loop and self._aclient_loop is not loop
192+
):
193+
self._aclient = self._aclient_factory()
194+
self._aclient_loop = loop
195+
return self._aclient
196+
170197
def _set_model_dims(self) -> int:
171198
"""
172199
Determine the dimensionality of the embedding model by making a test call.
@@ -366,7 +393,8 @@ async def _aembed(self, content: str, **kwargs) -> list[float | int]:
366393
TypeError: If content is not a string or input_type is not provided
367394
ValueError: If embedding fails
368395
"""
369-
if self._aclient is None:
396+
aclient = self._get_aclient()
397+
if aclient is None:
370398
return await super()._aembed(content, **kwargs)
371399

372400
if not isinstance(content, str):
@@ -375,7 +403,7 @@ async def _aembed(self, content: str, **kwargs) -> list[float | int]:
375403
input_type, embedding_types = self._prepare_request(kwargs)
376404

377405
try:
378-
response = await self._aclient.embed(
406+
response = await aclient.embed(
379407
texts=[content],
380408
model=self.model,
381409
input_type=input_type,
@@ -409,11 +437,10 @@ async def _aembed_many(
409437
TypeError: If contents is not a list of strings or input_type is not provided
410438
ValueError: If embedding fails
411439
"""
412-
if self._aclient is None:
440+
aclient = self._get_aclient()
441+
if aclient is None:
413442
return await super()._aembed_many(contents, batch_size=batch_size, **kwargs)
414-
return await self._aembed_many_with(
415-
self._aclient, contents, batch_size, **kwargs
416-
)
443+
return await self._aembed_many_with(aclient, contents, batch_size, **kwargs)
417444

418445
@retry(
419446
wait=wait_random_exponential(min=1, max=60),

‎tests/unit/test_cohere_vectorizer.py‎

Lines changed: 68 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import logging
1+
import asyncio
22
import sys
33
import types
44

@@ -63,26 +63,23 @@ def vectorizer(fake_cohere_module):
6363
return CohereTextVectorizer(api_config={"api_key": "test-key"}, timeout=30)
6464

6565

66-
def test_init_creates_sync_and_async_clients(fake_cohere_module, vectorizer):
66+
def test_init_creates_sync_client_and_defers_async(fake_cohere_module, vectorizer):
6767
sync_client = fake_cohere_module.Client.instances[-1]
68-
async_client = fake_cohere_module.AsyncClient.instances[-1]
6968

7069
assert vectorizer.dims == 3
71-
for client in (sync_client, async_client):
72-
assert client.api_key == "test-key"
73-
assert client.client_name == "redisvl"
74-
assert client.kwargs == {"timeout": 30}
70+
assert sync_client.api_key == "test-key"
71+
assert sync_client.client_name == "redisvl"
72+
assert sync_client.kwargs == {"timeout": 30}
73+
# The async client is built on first use, inside a running event loop.
74+
assert fake_cohere_module.AsyncClient.instances == []
7575

7676

77-
async def test_aembed_uses_async_client(fake_cohere_module, vectorizer, caplog):
78-
sync_client = fake_cohere_module.Client.instances[-1]
79-
async_client = fake_cohere_module.AsyncClient.instances[-1]
80-
sync_calls_before = len(sync_client.embed_calls)
81-
82-
with caplog.at_level(logging.WARNING):
83-
result = await vectorizer.aembed("hello async", input_type="search_query")
77+
async def test_aembed_uses_async_client(fake_cohere_module, vectorizer):
78+
result = await vectorizer.aembed("hello async", input_type="search_query")
8479

80+
async_client = fake_cohere_module.AsyncClient.instances[-1]
8581
assert result == _embedding_for("hello async")
82+
assert async_client.kwargs == {"timeout": 30}
8683
assert async_client.embed_calls == [
8784
{
8885
"texts": ["hello async"],
@@ -91,39 +88,33 @@ async def test_aembed_uses_async_client(fake_cohere_module, vectorizer, caplog):
9188
"embedding_types": ["float"],
9289
}
9390
]
94-
assert len(sync_client.embed_calls) == sync_calls_before
95-
assert "Falling back to sync" not in caplog.text
9691

9792

98-
async def test_aembed_many_batches_through_async_client(
99-
fake_cohere_module, vectorizer, caplog
100-
):
101-
sync_client = fake_cohere_module.Client.instances[-1]
102-
async_client = fake_cohere_module.AsyncClient.instances[-1]
103-
sync_calls_before = len(sync_client.embed_calls)
93+
async def test_aembed_many_batches_through_async_client(fake_cohere_module, vectorizer):
10494
texts = ["a", "bb", "ccc", "dddd", "eeeee"]
10595

106-
with caplog.at_level(logging.WARNING):
107-
result = await vectorizer.aembed_many(
108-
texts, batch_size=2, input_type="search_document"
109-
)
96+
result = await vectorizer.aembed_many(
97+
texts, batch_size=2, input_type="search_document"
98+
)
11099

100+
async_client = fake_cohere_module.AsyncClient.instances[-1]
111101
assert result == [_embedding_for(text) for text in texts]
112102
assert [call["texts"] for call in async_client.embed_calls] == [
113103
["a", "bb"],
114104
["ccc", "dddd"],
115105
["eeeee"],
116106
]
117-
assert len(sync_client.embed_calls) == sync_calls_before
118-
assert "Falling back to sync" not in caplog.text
119107

120108

121-
async def test_aembed_maps_dtype_to_embedding_types(fake_cohere_module):
109+
async def test_aembed_dtype_wins_over_embedding_types(fake_cohere_module):
122110
vectorizer = CohereTextVectorizer(api_config={"api_key": "test-key"}, dtype="int8")
123-
async_client = fake_cohere_module.AsyncClient.instances[-1]
124111

125-
await vectorizer.aembed("hello", input_type="search_query")
112+
with pytest.warns(UserWarning, match="embedding_types"):
113+
await vectorizer.aembed(
114+
"hello", input_type="search_query", embedding_types=["float"]
115+
)
126116

117+
async_client = fake_cohere_module.AsyncClient.instances[-1]
127118
assert async_client.embed_calls[-1]["embedding_types"] == ["int8"]
128119

129120

@@ -137,32 +128,37 @@ async def test_aembed_many_rejects_non_list(vectorizer):
137128
await vectorizer.aembed_many("not a list", input_type="search_document")
138129

139130

140-
async def test_aembed_ignores_embedding_types_with_warning(
141-
fake_cohere_module, vectorizer
142-
):
143-
async_client = fake_cohere_module.AsyncClient.instances[-1]
144-
145-
with pytest.warns(UserWarning, match="embedding_types"):
146-
await vectorizer.aembed(
147-
"hello", input_type="search_query", embedding_types=["int8"]
148-
)
149-
150-
assert async_client.embed_calls[-1]["embedding_types"] == ["float"]
151-
152-
153131
def test_embed_and_embed_many_still_use_sync_client(fake_cohere_module, vectorizer):
154132
sync_client = fake_cohere_module.Client.instances[-1]
155-
async_client = fake_cohere_module.AsyncClient.instances[-1]
156133

157134
assert vectorizer.embed("hi", input_type="search_query") == _embedding_for("hi")
158135
assert vectorizer.embed_many(["x", "yy"], input_type="search_document") == [
159136
_embedding_for("x"),
160137
_embedding_for("yy"),
161138
]
162-
assert async_client.embed_calls == []
163139
assert sync_client.embed_calls[-1]["texts"] == ["x", "yy"]
164140

165141

142+
def test_async_client_is_rebuilt_for_a_new_event_loop(fake_cohere_module, vectorizer):
143+
# A vectorizer that outlives an event loop must not reuse a client whose
144+
# connection pool is bound to the closed loop.
145+
asyncio.run(vectorizer.aembed("hello", input_type="search_query"))
146+
asyncio.run(vectorizer.aembed("again", input_type="search_query"))
147+
148+
first, second = fake_cohere_module.AsyncClient.instances
149+
assert first is not second
150+
assert [call["texts"] for call in second.embed_calls] == [["again"]]
151+
152+
153+
async def test_async_client_is_reused_within_one_event_loop(
154+
fake_cohere_module, vectorizer
155+
):
156+
await vectorizer.aembed("one", input_type="search_query")
157+
await vectorizer.aembed_many(["two"], input_type="search_document")
158+
159+
assert len(fake_cohere_module.AsyncClient.instances) == 1
160+
161+
166162
async def test_sync_httpx_client_keeps_async_on_sync_fallback(fake_cohere_module):
167163
sync_httpx = object()
168164
vectorizer = CohereTextVectorizer(
@@ -171,28 +167,49 @@ async def test_sync_httpx_client_keeps_async_on_sync_fallback(fake_cohere_module
171167
sync_client = fake_cohere_module.Client.instances[-1]
172168

173169
assert sync_client.kwargs == {"httpx_client": sync_httpx}
174-
assert fake_cohere_module.AsyncClient.instances == []
175170
assert await vectorizer.aembed(
176171
"hello", input_type="search_query"
177172
) == _embedding_for("hello")
178173
assert await vectorizer.aembed_many(["a", "bb"], input_type="search_document") == [
179174
_embedding_for("a"),
180175
_embedding_for("bb"),
181176
]
177+
assert fake_cohere_module.AsyncClient.instances == []
182178
assert sync_client.embed_calls[-1]["texts"] == ["a", "bb"]
183179

184180

185-
async def test_async_httpx_client_goes_to_async_client_only(fake_cohere_module):
181+
async def test_httpx_client_none_still_uses_async_client(fake_cohere_module):
182+
# Config dicts often carry httpx_client=None; that is not a sync client.
183+
vectorizer = CohereTextVectorizer(
184+
api_config={"api_key": "test-key"}, httpx_client=None
185+
)
186+
187+
await vectorizer.aembed("hello", input_type="search_query")
188+
189+
async_client = fake_cohere_module.AsyncClient.instances[-1]
190+
assert async_client.kwargs == {"httpx_client": None}
191+
assert async_client.embed_calls[-1]["texts"] == ["hello"]
192+
193+
194+
def test_async_httpx_client_goes_to_async_client_only_and_is_kept(
195+
fake_cohere_module,
196+
):
186197
sync_httpx, async_httpx = object(), object()
187198
vectorizer = CohereTextVectorizer(
188199
api_config={"api_key": "test-key"},
189200
httpx_client=sync_httpx,
190201
async_httpx_client=async_httpx,
191202
)
192203
sync_client = fake_cohere_module.Client.instances[-1]
193-
async_client = fake_cohere_module.AsyncClient.instances[-1]
194204

195205
assert sync_client.kwargs == {"httpx_client": sync_httpx}
206+
asyncio.run(vectorizer.aembed("hello", input_type="search_query"))
207+
asyncio.run(vectorizer.aembed("again", input_type="search_query"))
208+
209+
# The caller owns this client's loop, so it is never swapped out.
210+
(async_client,) = fake_cohere_module.AsyncClient.instances
196211
assert async_client.kwargs == {"httpx_client": async_httpx}
197-
await vectorizer.aembed("hello", input_type="search_query")
198-
assert async_client.embed_calls[-1]["texts"] == ["hello"]
212+
assert [call["texts"] for call in async_client.embed_calls] == [
213+
["hello"],
214+
["again"],
215+
]

0 commit comments

Comments
 (0)