1- import logging
1+ import asyncio
22import sys
33import 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-
153131def 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+
166162async 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