Open chrislrobert opened 1 month ago
Actually, my fix (and, I presume, the original async code) still has a pretty fatal flaw: because all of these with
blocks close the client, you can only make one call in a chain, then you trigger this on any subsequent calls:
ValueError("HTTP transport has already been closed. You may check if you're calling a function outside of the
async with
of your client creation, or if you calledawait close()
on your client already.")
Rather than having to re-establish the connection with each call, I have tried keeping it open and closing it on deletion of the AzureSearch
object. But all of this makes me uncomfortable, and I wonder what @baskaryan in PR #22075 had in mind re: the management of the async Azure search client. It's all still feeling quite messy and not-quite-complete...
I met the same issue lol.
` async def asemantic_hybrid_search_with_score_and_rerank( self, query: str, k: int = 4, *, filters: Optional[str] = None, **kwargs: Any ) -> List[Tuple[Document, float, float]]: """Return docs most similar to query with a hybrid query.
Args:
query: Text to look up documents similar to.
k: Number of Documents to return. Defaults to 4.
filters: Filtering expression.
Returns:
List of Documents most similar to the query and score for each
"""
from azure.search.documents.models import VectorizedQuery
vector = await self._aembed_query(query)
async with self.async_client as async_client:
results = await async_client.search(
search_text=query,
vector_queries=[
VectorizedQuery(
vector=np.array(vector, dtype=np.float32).tolist(),
k_nearest_neighbors=k,
fields=FIELDS_CONTENT_VECTOR,
)
],
filter=filters,
query_type="semantic",
semantic_configuration_name=self.semantic_configuration_name,
query_caption="extractive",
query_answer="extractive",
top=k,
**kwargs,
)`
I am also running into the same error message trying to call ainvoke on the AzureSearchVectorStoreRetriever, which is also using the methods described above. The synchronous call is working fine, but the async call causes the error "'AsyncSearchItemPaged' object is not iterable" during retrieval phase.
@JuliaZamaitat, you can override the class to remove the with
blocks and make other adjustments as-needed. @thedavgar has made the changes in #24921, so you can use that as a guide until it's been approved and included in a release. Hopefully it won't be long before the async support is working well!
Hi, I encountered the same issue. I've read about this issue and the PR #24921 that was created to fix it. However, I can't find the solution code in the latest release of langchain-community==0.2.12
here. Is it expected that the latest version still contains this issue, or did I miss something?
@prawiraelang I was confused too, as the changes were merged into master
Aug. 13. However, the current release of langchain-community
was on Aug. 12. So, I guess we just have to wait till they issue a new release...
@prawiraelang I was confused too, as the changes were merged into
master
Aug. 13. However, the current release oflangchain-community
was on Aug. 12. So, I guess we just have to wait till they issue a new release...
Thanks @chrislrobert for your response. Let's wait then. Hopefully, the fix will be released soon.
Checked other resources
Example Code
Error Message and Stack Trace (if applicable)
TypeError("'AsyncSearchItemPaged' object is not iterable")Traceback (most recent call last):
File "/Users/crobert/Code/Higher Bar AI/almitra-pilot-be/venv/lib/python3.10/site-packages/langchain/chains/base.py", line 208, in ainvoke await self._acall(inputs, run_manager=run_manager)
File "/Users/crobert/Code/Higher Bar AI/almitra-pilot-be/venv/lib/python3.10/site-packages/langchain/chains/conversational_retrieval/base.py", line 212, in _acall docs = await self._aget_docs(new_question, inputs, run_manager=_run_manager)
File "/Users/crobert/Code/Higher Bar AI/almitra-pilot-be/venv/lib/python3.10/site-packages/langchain/chains/conversational_retrieval/base.py", line 410, in _aget_docs docs = await self.retriever.ainvoke(
File "/Users/crobert/Code/Higher Bar AI/almitra-pilot-be/venv/lib/python3.10/site-packages/langchain_core/retrievers.py", line 280, in ainvoke raise e
File "/Users/crobert/Code/Higher Bar AI/almitra-pilot-be/venv/lib/python3.10/site-packages/langchain_core/retrievers.py", line 273, in ainvoke result = await self._aget_relevant_documents(
File "/Users/crobert/Code/Higher Bar AI/almitra-pilot-be/venv/lib/python3.10/site-packages/langchain_community/vectorstores/azuresearch.py", line 1590, in _aget_relevant_documents await self.vectorstore.asimilarity_search_with_relevance_scores(
File "/Users/crobert/Code/Higher Bar AI/almitra-pilot-be/venv/lib/python3.10/site-packages/langchain_community/vectorstores/azuresearch.py", line 663, in asimilarity_search_with_relevance_scores result = await self.avector_search_with_score(query, k=k, **kwargs)
File "/Users/crobert/Code/Higher Bar AI/almitra-pilot-be/venv/lib/python3.10/site-packages/langchain_community/vectorstores/azuresearch.py", line 750, in avector_search_with_score return _results_to_documents(results)
File "/Users/crobert/Code/Higher Bar AI/almitra-pilot-be/venv/lib/python3.10/site-packages/langchain_community/vectorstores/azuresearch.py", line 1623, in _results_to_documents docs = [
TypeError: 'AsyncSearchItemPaged' object is not iterable
Description
This commit for issue #24064 caused a regression in async support. After that commit,
avector_search_with_score()
calls_asimple_search()
, which usesasync with self.async_client
, and then tries to call_results_to_documents()
with the results — but that triggers a "TypeError: 'AsyncSearchItemPaged' object is not iterable" because it usesAsyncSearchItemPaged
on a closed HTTP connection (because the connection closed at the end of the_asimple_search()
with
block.The original async PR #22075 seemed to have the right idea: the async results need to be handled within the
with
block. Looking at that code, it looks like it should probably work. However, if I roll back to 0.2.7, I run into the "KeyError('content_vector')" that triggered issue #24064. For the moment, I've gotten things running by overriding AzureSearch as follows:System Info
System Information
Package Information