mirror of
https://github.com/hwchase17/langchain.git
synced 2026-06-09 10:17:00 +00:00
fix(mistralai): handle HTTP errors in async embed documents (#33187)
The async embed function does not properly handle HTTP errors. For instance with large batches, Mistral AI returns `Too many inputs in request, split into more batches.` in a 400 error. This leads to a KeyError in `response.json()["data"]` l.288 This PR fixes the issue by: - calling `response.raise_for_status()` before returning - adding a retry similarly to what is done in the synchronous counterpart `embed_documents` I also added an integration test, but willing to move it to unit tests if more relevant.
This commit is contained in:
@@ -267,20 +267,29 @@ class MistralAIEmbeddings(BaseModel, Embeddings):
|
||||
|
||||
Returns:
|
||||
List of embeddings, one for each text.
|
||||
|
||||
"""
|
||||
try:
|
||||
|
||||
@retry(
|
||||
retry=retry_if_exception_type(
|
||||
(httpx.TimeoutException, httpx.HTTPStatusError)
|
||||
),
|
||||
wait=wait_fixed(self.wait_time),
|
||||
stop=stop_after_attempt(self.max_retries),
|
||||
)
|
||||
async def _aembed_batch(batch: list[str]) -> Response:
|
||||
response = await self.async_client.post(
|
||||
url="/embeddings",
|
||||
json={
|
||||
"model": self.model,
|
||||
"input": batch,
|
||||
},
|
||||
)
|
||||
response.raise_for_status()
|
||||
return response
|
||||
|
||||
batch_responses = await asyncio.gather(
|
||||
*[
|
||||
self.async_client.post(
|
||||
url="/embeddings",
|
||||
json={
|
||||
"model": self.model,
|
||||
"input": batch,
|
||||
},
|
||||
)
|
||||
for batch in self._get_batches(texts)
|
||||
]
|
||||
*[_aembed_batch(batch) for batch in self._get_batches(texts)]
|
||||
)
|
||||
return [
|
||||
list(map(float, embedding_obj["embedding"]))
|
||||
|
||||
Reference in New Issue
Block a user