Propagate context vars in all classes/methods

- Any direct usage of ThreadPoolExecutor or asyncio.run_in_executor needs manual handling of context vars
This commit is contained in:
Nuno Campos
2023-12-29 12:34:03 -08:00
parent 70e5d05952
commit eb5e250188
39 changed files with 338 additions and 368 deletions

View File

@@ -1,10 +1,10 @@
from __future__ import annotations
import asyncio
from abc import ABC, abstractmethod
from functools import partial
from typing import TYPE_CHECKING, Any, Sequence
from langchain_core.runnables.config import run_in_executor
if TYPE_CHECKING:
from langchain_core.documents import Document
@@ -69,6 +69,6 @@ class BaseDocumentTransformer(ABC):
Returns:
A list of transformed Documents.
"""
return await asyncio.get_running_loop().run_in_executor(
None, partial(self.transform_documents, **kwargs), documents
return await run_in_executor(
None, self.transform_documents, documents, **kwargs
)