From d7199e5874a59ed30eb48c2b39a1e7b207ada27b Mon Sep 17 00:00:00 2001 From: Jimmy Sambuo Date: Mon, 13 Jan 2025 09:59:46 -0500 Subject: [PATCH] docs: use InjectedStore in semantic search guide (#2995) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When I tried to follow the How-to guide for [How to add semantic search to your agent's memory](https://langchain-ai.github.io/langgraph/how-tos/memory/semantic-search/#using-in-create_react_agent) using `create_react_agent`, I got this error message when my agent used the tool: ```python 1 validation error for upsert_memory store Field required [type=missing, input_value={'content': '@jimmy works...ny.', 'memory_id': None}, input_type=dict] For further information visit https://errors.pydantic.dev/2.10/v/missingTraceback (most recent call last): File "/usr/local/lib/python3.9/site-packages/langchain_core/tools/base.py", line 688, in run tool_args, tool_kwargs = self._to_args_and_kwargs(tool_input, tool_call_id) File "/usr/local/lib/python3.9/site-packages/langchain_core/tools/base.py", line 611, in _to_args_and_kwargs tool_input = self._parse_input(tool_input, tool_call_id) File "/usr/local/lib/python3.9/site-packages/langchain_core/tools/base.py", line 532, in _parse_input result = input_args.model_validate(tool_input) File "/usr/local/lib/python3.9/site-packages/pydantic/main.py", line 627, in model_validate return cls.__pydantic_validator__.validate_python( pydantic_core._pydantic_core.ValidationError: 1 validation error for upsert_memory store Field required [type=missing, input_value={'content': '@jimmy works...ny.', 'memory_id': None}, input_type=dict] For further information visit https://errors.pydantic.dev/2.10/v/missing ``` I believe it’s because the graph did not inject the store into the tool if we use `InjectedToolArg`. When looking at the guide for [How to pass runtime values to tools](https://langchain-ai.github.io/langgraph/how-tos/pass-run-time-values-to-tools/), it suggests to use `InjectedStore` with `create_react_agent`. After changing my code to use `InjectedStore`, my agent was able to save to the store. --- docs/docs/how-tos/memory/semantic-search.ipynb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/docs/how-tos/memory/semantic-search.ipynb b/docs/docs/how-tos/memory/semantic-search.ipynb index 8e7a8d057..e7f4fc11f 100644 --- a/docs/docs/how-tos/memory/semantic-search.ipynb +++ b/docs/docs/how-tos/memory/semantic-search.ipynb @@ -208,7 +208,7 @@ "from typing import Optional\n", "\n", "from langchain.chat_models import init_chat_model\n", - "from langchain_core.tools import InjectedToolArg\n", + "from langgraph.prebuilt import InjectedStore\n", "from langgraph.store.base import BaseStore\n", "from typing_extensions import Annotated\n", "\n", @@ -232,7 +232,7 @@ " content: str,\n", " *,\n", " memory_id: Optional[uuid.UUID] = None,\n", - " store: Annotated[BaseStore, InjectedToolArg],\n", + " store: Annotated[BaseStore, InjectedStore],\n", "):\n", " \"\"\"Upsert a memory in the database.\"\"\"\n", " # The LLM can use this tool to store a new memory\n",