From dd7be274b95af4b775fef9f0ebfb40bbc1e206c9 Mon Sep 17 00:00:00 2001 From: Ilya Kolchinsky <58424190+ilya-kolchinsky@users.noreply.github.com> Date: Mon, 12 May 2025 11:25:13 +0200 Subject: [PATCH] fix: raise an error when no vector DB IDs are provided to the RAG tool (#1911) # What does this PR do? This PR fixes the behavior of the `/tool-runtime/rag-tool/query` endpoint when invoked with an empty `vector_db_ids` parameter. As of now, it simply returns an empty result, which leads to a misleading error message from the server and makes it difficult and time-consuming to detect the problem with the input parameter. The proposed fix is to return an indicative error message in this case. ## Test Plan Running the following script: ``` agent = Agent( client, model=MODEL_ID, instructions=SYSTEM_PROMPT, tools=[ dict( name="builtin::rag/knowledge_search", args={ "vector_db_ids": [], }, ) ], ) response = agent.create_turn( messages=[ { "role": "user", "content": "How to install OpenShift?", } ], session_id=agent.create_session(f"rag-session") ) ``` results in the following error message in the non-patched version: ``` {"type": "function", "name": "knowledge_search", "parameters": {"query": "installing OpenShift"}}400: Invalid value: Tool call result (id: 494b8020-90bb-449b-aa76-10960d6b2cc2, name: knowledge_search) does not have any content ``` and in the following one in the patched version: ``` {"type": "function", "name": "knowledge_search", "parameters": {"query": "installing OpenShift"}}400: Invalid value: No vector DBs were provided to the RAG tool. Please provide at least one DB. ``` --- .../inline/tool_runtime/rag/memory.py | 4 +++- tests/unit/rag/test_rag_query.py | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 tests/unit/rag/test_rag_query.py diff --git a/llama_stack/providers/inline/tool_runtime/rag/memory.py b/llama_stack/providers/inline/tool_runtime/rag/memory.py index df0257718..968f93354 100644 --- a/llama_stack/providers/inline/tool_runtime/rag/memory.py +++ b/llama_stack/providers/inline/tool_runtime/rag/memory.py @@ -105,7 +105,9 @@ class MemoryToolRuntimeImpl(ToolsProtocolPrivate, ToolRuntime, RAGToolRuntime): query_config: RAGQueryConfig | None = None, ) -> RAGQueryResult: if not vector_db_ids: - return RAGQueryResult(content=None) + raise ValueError( + "No vector DBs were provided to the knowledge search tool. Please provide at least one vector DB ID." + ) query_config = query_config or RAGQueryConfig() query = await generate_rag_query( diff --git a/tests/unit/rag/test_rag_query.py b/tests/unit/rag/test_rag_query.py new file mode 100644 index 000000000..b9fd8cca4 --- /dev/null +++ b/tests/unit/rag/test_rag_query.py @@ -0,0 +1,19 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# All rights reserved. +# +# This source code is licensed under the terms described in the LICENSE file in +# the root directory of this source tree. + +from unittest.mock import MagicMock + +import pytest + +from llama_stack.providers.inline.tool_runtime.rag.memory import MemoryToolRuntimeImpl + + +class TestRagQuery: + @pytest.mark.asyncio + async def test_query_raises_on_empty_vector_db_ids(self): + rag_tool = MemoryToolRuntimeImpl(config=MagicMock(), vector_io_api=MagicMock(), inference_api=MagicMock()) + with pytest.raises(ValueError): + await rag_tool.query(content=MagicMock(), vector_db_ids=[])