Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core, tests: more tolerant _aget_relevant_documents function #28462

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion libs/core/langchain_core/retrievers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from typing import TYPE_CHECKING, Any, Optional

from pydantic import ConfigDict
from typing_extensions import TypedDict
from typing_extensions import Self, TypedDict

from langchain_core._api import deprecated
from langchain_core.documents import Document
Expand Down Expand Up @@ -180,6 +180,18 @@ def __init_subclass__(cls, **kwargs: Any) -> None:
cls._aget_relevant_documents = aswap # type: ignore[assignment]
parameters = signature(cls._get_relevant_documents).parameters
cls._new_arg_supported = parameters.get("run_manager") is not None
if (
not cls._new_arg_supported
and cls._aget_relevant_documents == BaseRetriever._aget_relevant_documents
):
# we need to tolerate no run_manager in _aget_relevant_documents signature
async def _aget_relevant_documents(
self: Self, query: str
) -> list[Document]:
return await run_in_executor(None, self._get_relevant_documents, query) # type: ignore

cls._aget_relevant_documents = _aget_relevant_documents # type: ignore[assignment]

# If a V1 retriever broke the interface and expects additional arguments
cls._expects_other_args = (
len(set(parameters.keys()) - {"self", "query", "run_manager"}) > 0
Expand Down
Empty file.
5 changes: 1 addition & 4 deletions libs/standard-tests/tests/unit_tests/test_basic_retriever.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from typing import Any, Type

from langchain_core.callbacks import CallbackManagerForRetrieverRun
from langchain_core.documents import Document
from langchain_core.retrievers import BaseRetriever

Expand All @@ -11,9 +10,7 @@ class ParrotRetriever(BaseRetriever):
parrot_name: str
k: int = 3

def _get_relevant_documents(
self, query: str, *, run_manager: CallbackManagerForRetrieverRun, **kwargs: Any
) -> list[Document]:
def _get_relevant_documents(self, query: str, **kwargs: Any) -> list[Document]:
k = kwargs.get("k", self.k)
return [Document(page_content=f"{self.parrot_name} says: {query}")] * k

Expand Down
Loading