Skip to content

Fix removing vector store tools #11463

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

Closed
Closed
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
5 changes: 4 additions & 1 deletion litellm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,10 @@ def add_known_models():
from .cost_calculator import completion_cost
from litellm.litellm_core_utils.litellm_logging import Logging, modify_integration
from litellm.litellm_core_utils.get_llm_provider_logic import get_llm_provider
from litellm.litellm_core_utils.core_helpers import remove_index_from_tool_calls
from litellm.litellm_core_utils.core_helpers import (
remove_index_from_tool_calls,
remove_items_at_indices,
)
from litellm.litellm_core_utils.token_counter import get_modified_max_tokens
from .utils import (
client,
Expand Down
13 changes: 11 additions & 2 deletions litellm/litellm_core_utils/core_helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# What is this?
## Helper utilities
from typing import TYPE_CHECKING, Any, List, Optional, Union
from typing import TYPE_CHECKING, Any, List, Optional, Union, Iterable

import httpx

Expand Down Expand Up @@ -67,7 +67,16 @@ def remove_index_from_tool_calls(
): # Type guard to ensure it's a dict
tool_call.pop("index", None)

return
return


def remove_items_at_indices(items: Optional[List[Any]], indices: Iterable[int]) -> None:
"""Remove items from a list in-place by index"""
if items is None:
return
for index in sorted(set(indices), reverse=True):
if 0 <= index < len(items):
items.pop(index)


def add_missing_spend_metadata_to_litellm_metadata(
Expand Down
25 changes: 22 additions & 3 deletions litellm/vector_stores/vector_store_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from datetime import datetime, timezone
from typing import TYPE_CHECKING, Any, Dict, List, Optional

from litellm.litellm_core_utils.core_helpers import remove_items_at_indices

from litellm._logging import verbose_logger
from litellm.types.vector_stores import (
LiteLLM_ManagedVectorStore,
Expand Down Expand Up @@ -55,9 +57,26 @@ def pop_vector_store_ids_to_run(
vector_store_ids = non_default_params.pop("vector_store_ids", None) or []

# 2. check if vector_store_ids is provided as a tool in the request
vector_store_ids = self._get_vector_store_ids_from_tool_calls(
tools=tools, vector_store_ids=vector_store_ids
)
if tools:
tools_to_remove: List[int] = []
for i, tool in enumerate(tools):
tool_vector_store_ids: List[str] = tool.get("vector_store_ids", [])
if len(tool_vector_store_ids) == 0:
continue

vector_store_ids.extend(tool_vector_store_ids)

# remove the tool if all vector_store_ids are recognised in the registry
recognised = all(
any(vs.get("vector_store_id") == vs_id for vs in self.vector_stores)
for vs_id in tool_vector_store_ids
)
if recognised:
tools_to_remove.append(i)

# remove recognised tools from the original list
remove_items_at_indices(tools, tools_to_remove)

return vector_store_ids

def get_vector_store_to_run(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,12 @@ async def test_openai_with_knowledge_base_mock_openai(setup_vector_store_registr
# Verify the API was called
mock_client.assert_called_once()
request_body = mock_client.call_args.kwargs

# Verify the request contains messages with knowledge base context
assert "messages" in request_body
# The original tools field should be removed once the vector store ids
# have been processed by LiteLLM.
assert "tools" not in request_body
messages = request_body["messages"]

# We expect at least 2 messages:
Expand Down
Loading