The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
fix: remove partial usage for retriever func + afunc (#34265)
Sydney Runkle34d31b8394ca
diff --git a/libs/core/langchain_core/tools/retriever.py b/libs/core/langchain_core/tools/retriever.pyindex 5677e97bd7..a830c964f8 100644--- a/libs/core/langchain_core/tools/retriever.py+++ b/libs/core/langchain_core/tools/retriever.py@@ -2,22 +2,21 @@from __future__ import annotations-from functools import partialfrom typing import TYPE_CHECKING, Literalfrom pydantic import BaseModel, Field+from langchain_core.callbacks import Callbacks+from langchain_core.documents import Documentfrom langchain_core.prompts import (BasePromptTemplate,PromptTemplate,aformat_document,format_document,)-from langchain_core.tools.simple import Tool+from langchain_core.tools.structured import StructuredToolif TYPE_CHECKING:- from langchain_core.callbacks import Callbacks- from langchain_core.documents import Documentfrom langchain_core.retrievers import BaseRetriever@@ -27,43 +26,6 @@ class RetrieverInput(BaseModel):query: str = Field(description="query to look up in retriever")-def _get_relevant_documents(- query: str,- retriever: BaseRetriever,- document_prompt: BasePromptTemplate,- document_separator: str,- callbacks: Callbacks = None,- response_format: Literal["content", "content_and_artifact"] = "content",-) -> str | tuple[str, list[Document]]:- docs = retriever.invoke(query, config={"callbacks": callbacks})- content = document_separator.join(- format_document(doc, document_prompt) for doc in docs- )- if response_format == "content_and_artifact":- return (content, docs)-- return content---async def _aget_relevant_documents(- query: str,- retriever: BaseRetriever,- document_prompt: BasePromptTemplate,- document_separator: str,- callbacks: Callbacks = None,- response_format: Literal["content", "content_and_artifact"] = "content",-) -> str | tuple[str, list[Document]]:- docs = await retriever.ainvoke(query, config={"callbacks": callbacks})- content = document_separator.join(- [await aformat_document(doc, document_prompt) for doc in docs]- )-- if response_format == "content_and_artifact":- return (content, docs)-- return content--def create_retriever_tool(retriever: BaseRetriever,name: str,@@ -72,7 +34,7 @@ def create_retriever_tool(document_prompt: BasePromptTemplate | None = None,document_separator: str = "\n\n",response_format: Literal["content", "content_and_artifact"] = "content",-) -> Tool:+) -> StructuredTool:r"""Create a tool to do retrieval of documents.Args:@@ -93,22 +55,31 @@ def create_retriever_tool(Returns:Tool class to pass to an agent."""- document_prompt = document_prompt or PromptTemplate.from_template("{page_content}")- func = partial(- _get_relevant_documents,- retriever=retriever,- document_prompt=document_prompt,- document_separator=document_separator,- response_format=response_format,- )- afunc = partial(- _aget_relevant_documents,- retriever=retriever,- document_prompt=document_prompt,- document_separator=document_separator,- response_format=response_format,- )- return Tool(+ document_prompt_ = document_prompt or PromptTemplate.from_template("{page_content}")++ def func(+ query: str, callbacks: Callbacks = None+ ) -> str | tuple[str, list[Document]]:+ docs = retriever.invoke(query, config={"callbacks": callbacks})+ content = document_separator.join(+ format_document(doc, document_prompt_) for doc in docs+ )+ if response_format == "content_and_artifact":+ return (content, docs)+ return content++ async def afunc(+ query: str, callbacks: Callbacks = None+ ) -> str | tuple[str, list[Document]]:+ docs = await retriever.ainvoke(query, config={"callbacks": callbacks})+ content = document_separator.join(+ [await aformat_document(doc, document_prompt_) for doc in docs]+ )+ if response_format == "content_and_artifact":+ return (content, docs)+ return content++ return StructuredTool(name=name,description=description,func=func,diff --git a/libs/core/tests/unit_tests/test_tools.py b/libs/core/tests/unit_tests/test_tools.pyindex 60e04c9f76..587151f57b 100644--- a/libs/core/tests/unit_tests/test_tools.py+++ b/libs/core/tests/unit_tests/test_tools.py@@ -17,6 +17,7 @@ from typing import (Literal,TypeVar,cast,+ get_type_hints,)import pytest@@ -2245,6 +2246,32 @@ def test_create_retriever_tool() -> None:)+def test_create_retriever_tool_get_type_hints() -> None:+ """Verify get_type_hints works on retriever tool's func.++ This test ensures compatibility with Python 3.12+ where get_type_hints()+ raises TypeError on functools.partial objects. Tools like LangGraph's+ ToolNode call get_type_hints(tool.func) to generate schemas.+ """++ class MyRetriever(BaseRetriever):+ @override+ def _get_relevant_documents(+ self, query: str, *, run_manager: CallbackManagerForRetrieverRun+ ) -> list[Document]:+ return [Document(page_content="test")]++ retriever = MyRetriever()+ retriever_tool = tools.create_retriever_tool(+ retriever, "test_tool", "Test tool for type hints"+ )++ # This should not raise TypeError (as it did with functools.partial)+ hints = get_type_hints(retriever_tool.func)+ assert "query" in hints+ assert hints["query"] is str++def test_tool_args_schema_pydantic_v2_with_metadata() -> None:class Foo(BaseModel):x: list[int] = Field(