From 0d44ce88f81268a36d1c9b2005068991551b8a8e Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Tue, 20 Oct 2026 18:58:26 -0700 Subject: [PATCH 1/6] feat(vector_stores): add Amazon OpenSearch as a vector store provider --- litellm/llms/opensearch/__init__.py | 0 .../llms/opensearch/vector_stores/__init__.py | 0 .../vector_stores/transformation.py | 330 ++++++++++++++++++ litellm/types/utils.py | 1 + litellm/utils.py | 35 +- provider_endpoints_support.json | 7 + tests/unit/llms/opensearch/__init__.py | 0 .../llms/opensearch/vector_stores/__init__.py | 0 .../test_opensearch_transformation.py | 292 ++++++++++++++++ .../public_endpoints/test_public_endpoints.py | 1 + 10 files changed, 654 insertions(+), 12 deletions(-) create mode 100644 litellm/llms/opensearch/__init__.py create mode 100644 litellm/llms/opensearch/vector_stores/__init__.py create mode 100644 litellm/llms/opensearch/vector_stores/transformation.py create mode 100644 tests/unit/llms/opensearch/__init__.py create mode 100644 tests/unit/llms/opensearch/vector_stores/__init__.py create mode 100644 tests/unit/llms/opensearch/vector_stores/test_opensearch_transformation.py diff --git a/litellm/llms/opensearch/__init__.py b/litellm/llms/opensearch/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/litellm/llms/opensearch/vector_stores/__init__.py b/litellm/llms/opensearch/vector_stores/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/litellm/llms/opensearch/vector_stores/transformation.py b/litellm/llms/opensearch/vector_stores/transformation.py new file mode 100644 index 00000000000..bd3bc90b384 --- /dev/null +++ b/litellm/llms/opensearch/vector_stores/transformation.py @@ -0,0 +1,330 @@ +import hashlib +import json +import re +from collections.abc import Mapping, Sequence +from functools import partial +from typing import TYPE_CHECKING, Final, Literal, NoReturn +from urllib.parse import quote, urlsplit + +import httpx +from pydantic import ConfigDict, Field, TypeAdapter, ValidationError + +from litellm.exceptions import BadRequestError +from litellm.llms.base_llm.chat.transformation import BaseLLMException +from litellm.llms.base_llm.vector_store.transformation import ( + BaseQueryEmbeddingVectorStoreConfig, + VectorStoreEmbeddingExecutor, +) +from litellm.llms.bedrock.base_aws_llm import BaseAWSLLM, SignsRequestsWithAWS, sign_aws_json_post +from litellm.types.llms.base import LiteLLMBaseModel +from litellm.types.llms.bedrock import AwsAuthParams +from litellm.types.router import GenericLiteLLMParams +from litellm.types.utils import EmbeddingResponse +from litellm.types.vector_stores import ( + BaseVectorStoreAuthCredentials, + VectorStoreCreateOptionalRequestParams, + VectorStoreIndexEndpoints, + VectorStoreResultContent, + VectorStoreSearchOptionalRequestParams, + VectorStoreSearchResponse, + VectorStoreSearchResult, +) + +if TYPE_CHECKING: + from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj + +DEFAULT_EMBEDDING_FIELD: Final = "embedding" +DEFAULT_TEXT_FIELD: Final = "text" +DEFAULT_MAX_NUM_RESULTS: Final = 10 +MAX_MAX_NUM_RESULTS: Final = 50 +_PARAM_PREFIX: Final = "opensearch_" +_AWS_HOST: Final = re.compile(r"\.(?P[a-z0-9-]+)\.(?Paoss|es)\.amazonaws\.com(?:\.cn)?\Z") +_QUERY_VECTOR: Final = TypeAdapter(tuple[float, ...]) +_SEARCH_ONLY_MESSAGE: Final = ( + "The OpenSearch vector store is search-only. Create the k-NN index in OpenSearch, " + "then register it here with the index name as vector_store_id." +) + + +def _bad_request(message: str) -> BadRequestError: + return BadRequestError(message=message, model=None, llm_provider="opensearch") + + +class _OpenSearchParams(LiteLLMBaseModel): + model_config = ConfigDict(frozen=True, extra="ignore") + + litellm_embedding_model: str | None = None + aws_region_name: str | None = None + opensearch_embedding_field: str | None = None + opensearch_text_field: str | None = None + + @property + def embedding_field(self) -> str: + return self.opensearch_embedding_field or DEFAULT_EMBEDDING_FIELD + + @property + def text_field(self) -> str: + return self.opensearch_text_field or DEFAULT_TEXT_FIELD + + def require_embedding_model(self) -> str: + if not self.litellm_embedding_model: + raise _bad_request( + "litellm_embedding_model is required for the OpenSearch vector store. It must be the model that " + f"produced the vectors in '{self.embedding_field}', for example openai/text-embedding-3-small." + ) + return self.litellm_embedding_model + + +class _SearchOptions(LiteLLMBaseModel): + model_config = ConfigDict(frozen=True, extra="ignore") + + max_num_results: int | None = None + filters: Mapping[str, object] | None = None + ranking_options: Mapping[str, object] | None = None + rewrite_query: bool | None = None + + @property + def unsupported(self) -> tuple[str, ...]: + requested: Final = ( + ("filters", self.filters), + ("ranking_options", self.ranking_options), + ("rewrite_query", self.rewrite_query), + ) + return tuple(name for name, value in requested if value) + + @property + def k(self) -> int: + if self.max_num_results is None: + return DEFAULT_MAX_NUM_RESULTS + if not 1 <= self.max_num_results <= MAX_MAX_NUM_RESULTS: + raise _bad_request( + f"max_num_results must be between 1 and {MAX_MAX_NUM_RESULTS}, got {self.max_num_results}" + ) + return self.max_num_results + + +_KNOWN_PARAMS: Final = frozenset(name for name in _OpenSearchParams.model_fields if name.startswith(_PARAM_PREFIX)) + + +class _LoggedSearch(LiteLLMBaseModel): + model_config = ConfigDict(frozen=True, extra="ignore") + + query: str | tuple[str, ...] = "" + litellm_params: _OpenSearchParams = _OpenSearchParams() + + @property + def search_query(self) -> str: + return BaseQueryEmbeddingVectorStoreConfig.query_text(self.query) + + +class _Hit(LiteLLMBaseModel): + model_config = ConfigDict(frozen=True) + + id: str = Field(alias="_id") + score: float | None = Field(default=None, alias="_score") + source: Mapping[str, object] = Field(default_factory=dict, alias="_source") + + +class _Hits(LiteLLMBaseModel): + model_config = ConfigDict(frozen=True) + + hits: Sequence[_Hit] + + +class _SearchResponse(LiteLLMBaseModel): + model_config = ConfigDict(frozen=True) + + hits: _Hits + + +def aws_signing_target(url: str) -> tuple[Literal["aoss", "es"], str | None]: + match: Final = _AWS_HOST.search(urlsplit(url).hostname or "") + if match is None: + return "es", None + return ("aoss" if match["service"] == "aoss" else "es"), match["region"] + + +def _search_result(hit: _Hit, params: _OpenSearchParams) -> VectorStoreSearchResult: + text: Final = hit.source.get(params.text_field) + filename: Final = hit.source.get("filename") + return VectorStoreSearchResult( + score=hit.score, + content=[VectorStoreResultContent(type="text", text=text)] if isinstance(text, str) else [], + file_id=hit.id, + filename=filename if isinstance(filename, str) else None, + attributes={ + key: value for key, value in hit.source.items() if key not in (params.text_field, params.embedding_field) + }, + ) + + +class OpenSearchVectorStoreConfig(BaseQueryEmbeddingVectorStoreConfig, SignsRequestsWithAWS): + def __init__(self, aws: BaseAWSLLM | None = None) -> None: + self._aws: Final = aws if aws is not None else BaseAWSLLM() + + def get_auth_credentials(self, litellm_params: Mapping[str, object]) -> BaseVectorStoreAuthCredentials: + return BaseVectorStoreAuthCredentials() + + def get_vector_store_endpoints_by_type(self) -> VectorStoreIndexEndpoints: + return VectorStoreIndexEndpoints(read=[], write=[]) + + def validate_environment( + self, headers: Mapping[str, object], litellm_params: GenericLiteLLMParams | None + ) -> dict[str, object]: # mutable-ok: the shared HTTP handler requires writable headers + return {**headers, "Content-Type": "application/json"} + + def get_complete_url(self, api_base: str | None, litellm_params: Mapping[str, object]) -> str: + parsed: Final = urlsplit(api_base or "") + if parsed.scheme not in ("http", "https") or not parsed.hostname: + raise _bad_request( + "api_base must be the OpenSearch endpoint URL, for example " + "https://..aoss.amazonaws.com or https://." + ) + return (api_base or "").rstrip("/") + + @staticmethod + def _params( + litellm_params: Mapping[str, object], + optional_params: VectorStoreSearchOptionalRequestParams, + extra_body: Mapping[str, object] | None, + ) -> tuple[_OpenSearchParams, int]: + unknown: Final = sorted( + key for key in litellm_params if key.startswith(_PARAM_PREFIX) and key not in _KNOWN_PARAMS + ) + if unknown: + raise _bad_request( + f"Unknown OpenSearch vector store parameter(s): {', '.join(unknown)}. " + f"Supported: {', '.join(sorted(_KNOWN_PARAMS))}." + ) + if extra_body: + raise _bad_request("The OpenSearch vector store does not support extra_body.") + try: + options: Final = _SearchOptions.model_validate(optional_params) + except ValidationError as error: + raise _bad_request(f"Invalid vector store search parameters: {error}") from None + if options.unsupported: + raise _bad_request( + f"The OpenSearch vector store does not support {', '.join(options.unsupported)} yet. " + "Remove it from the request." + ) + try: + return _OpenSearchParams.model_validate(litellm_params), options.k + except ValidationError: + raise _bad_request( + "opensearch_embedding_field, opensearch_text_field, and litellm_embedding_model must be strings." + ) from None + + @staticmethod + def _search_request( + vector_store_id: str, + embedding: EmbeddingResponse, + k: int, + params: _OpenSearchParams, + api_base: str, + ) -> tuple[str, dict[str, object]]: # mutable-ok: the provider contract returns a writable JSON request body + vector: Final = _QUERY_VECTOR.validate_python(embedding.data[0]["embedding"]) # pyright: ignore[reportUnknownMemberType] # EmbeddingResponse.data is an untyped list, the vector is validated here + return ( + f"{api_base}/{quote(vector_store_id, safe='')}/_search", + { + "size": k, + "query": {"knn": {params.embedding_field: {"vector": vector, "k": k}}}, + "_source": {"excludes": [params.embedding_field]}, + }, + ) + + def transform_search_vector_store_request( + self, + vector_store_id: str, + query: str | Sequence[str], + vector_store_search_optional_params: VectorStoreSearchOptionalRequestParams, + api_base: str, + litellm_logging_obj: "LiteLLMLoggingObj", + litellm_params: Mapping[str, object], + extra_body: Mapping[str, object] | None = None, + embedding_executor: VectorStoreEmbeddingExecutor | None = None, + ) -> tuple[str, dict[str, object]]: # mutable-ok: the provider contract returns a writable JSON request body + params, k = self._params(litellm_params, vector_store_search_optional_params, extra_body) + embedding: Final = self.query_embedding_executor(embedding_executor, None).embed( + params.require_embedding_model(), self.query_text(query), self.query_embedding_configuration(litellm_params) + ) + return self._search_request(vector_store_id, embedding, k, params, api_base) + + async def atransform_search_vector_store_request( + self, + vector_store_id: str, + query: str | Sequence[str], + vector_store_search_optional_params: VectorStoreSearchOptionalRequestParams, + api_base: str, + litellm_logging_obj: "LiteLLMLoggingObj", + litellm_params: Mapping[str, object], + extra_body: Mapping[str, object] | None = None, + embedding_executor: VectorStoreEmbeddingExecutor | None = None, + ) -> tuple[str, dict[str, object]]: # mutable-ok: the provider contract returns a writable JSON request body + params, k = self._params(litellm_params, vector_store_search_optional_params, extra_body) + embedding: Final = await self.query_embedding_executor(embedding_executor, None).aembed( + params.require_embedding_model(), self.query_text(query), self.query_embedding_configuration(litellm_params) + ) + return self._search_request(vector_store_id, embedding, k, params, api_base) + + def sign_request( + self, + headers: Mapping[str, str], + optional_params: Mapping[str, object], + request_data: Mapping[str, object], + api_base: str, + api_key: str | None = None, + ) -> tuple[dict[str, str], bytes]: # mutable-ok: the shared HTTP handler requires writable headers + service, host_region = aws_signing_target(api_base) + configured_region: Final = _OpenSearchParams.model_validate(optional_params).aws_region_name + region: Final = self._aws.get_aws_region_name_for_non_llm_api_calls(configured_region or host_region) + body: Final = json.dumps(request_data) + signed: Final = sign_aws_json_post( + get_credentials=partial( + self._aws.resolve_credentials, AwsAuthParams.model_validate(optional_params), region + ), + service_name=service, + aws_region_name=region, + url=api_base, + body=body, + headers={ + "Content-Type": "application/json", + "x-amz-content-sha256": hashlib.sha256(body.encode()).hexdigest(), + }, + ) + signed_headers: Final = dict(signed.headers.items()) + signed_names: Final = frozenset(name.lower() for name in signed_headers) + return { + **{name: value for name, value in headers.items() if name.lower() not in signed_names}, + **signed_headers, + }, body.encode() + + def transform_search_vector_store_response( + self, response: httpx.Response, litellm_logging_obj: "LiteLLMLoggingObj" + ) -> VectorStoreSearchResponse: + if not response.is_success: + raise BaseLLMException(status_code=response.status_code, message=response.text, headers=response.headers) + try: + hits: Final = _SearchResponse.model_validate_json(response.content).hits.hits + except ValidationError: + raise BaseLLMException( + status_code=502, + message="OpenSearch answered the search without a hits.hits list.", + headers=response.headers, + ) from None + logged: Final = _LoggedSearch.model_validate(litellm_logging_obj.model_call_details) + return VectorStoreSearchResponse( + object="vector_store.search_results.page", + search_query=logged.search_query, + data=[_search_result(hit, logged.litellm_params) for hit in hits], + ) + + def validate_create_vector_store(self) -> NoReturn: + raise _bad_request(_SEARCH_ONLY_MESSAGE) + + def transform_create_vector_store_request( + self, vector_store_create_optional_params: VectorStoreCreateOptionalRequestParams, api_base: str + ) -> NoReturn: + raise _bad_request(_SEARCH_ONLY_MESSAGE) + + def transform_create_vector_store_response(self, response: httpx.Response) -> NoReturn: + raise _bad_request(_SEARCH_ONLY_MESSAGE) diff --git a/litellm/types/utils.py b/litellm/types/utils.py index 5939a350970..9e8d4a218bb 100644 --- a/litellm/types/utils.py +++ b/litellm/types/utils.py @@ -4507,6 +4507,7 @@ class LlmProviders(str, Enum): S3_VECTORS = "s3_vectors" VALKEY = "valkey" MONGODB = "mongodb" + OPENSEARCH = "opensearch" HELICONE = "helicone" HYPERBOLIC = "hyperbolic" RECRAFT = "recraft" diff --git a/litellm/utils.py b/litellm/utils.py index 897138a940c..f832e224752 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -10132,6 +10132,22 @@ class ProviderConfigManager: return BedrockVectorStore.get_initialized_custom_logger() return None + @staticmethod + def _get_vertex_ai_vector_stores_config(api_type: str | None) -> BaseVectorStoreConfig | None: + if api_type == "rag_api" or api_type is None: + from litellm.llms.vertex_ai.vector_stores.rag_api.transformation import ( + VertexVectorStoreConfig, + ) + + return VertexVectorStoreConfig() + if api_type == "search_api": + from litellm.llms.vertex_ai.vector_stores.search_api.transformation import ( + VertexSearchAPIVectorStoreConfig, + ) + + return VertexSearchAPIVectorStoreConfig() + return None + @staticmethod def get_provider_vector_stores_config( provider: LlmProviders, @@ -10153,18 +10169,7 @@ class ProviderConfigManager: return AzureOpenAIVectorStoreConfig() elif litellm.LlmProviders.VERTEX_AI == provider: - if api_type == "rag_api" or api_type is None: # default to rag_api - from litellm.llms.vertex_ai.vector_stores.rag_api.transformation import ( - VertexVectorStoreConfig, - ) - - return VertexVectorStoreConfig() - elif api_type == "search_api": - from litellm.llms.vertex_ai.vector_stores.search_api.transformation import ( - VertexSearchAPIVectorStoreConfig, - ) - - return VertexSearchAPIVectorStoreConfig() + return ProviderConfigManager._get_vertex_ai_vector_stores_config(api_type) elif litellm.LlmProviders.BEDROCK == provider: from litellm.llms.bedrock.vector_stores.transformation import ( BedrockVectorStoreConfig, @@ -10219,6 +10224,12 @@ class ProviderConfigManager: ) return MongoDBVectorStoreConfig() + elif litellm.LlmProviders.OPENSEARCH == provider: + from litellm.llms.opensearch.vector_stores.transformation import ( + OpenSearchVectorStoreConfig, + ) + + return OpenSearchVectorStoreConfig() return None @staticmethod diff --git a/provider_endpoints_support.json b/provider_endpoints_support.json index d65da3f41fd..09714228549 100644 --- a/provider_endpoints_support.json +++ b/provider_endpoints_support.json @@ -3098,6 +3098,13 @@ "vector_stores_search": true } }, + "opensearch": { + "display_name": "Amazon OpenSearch (`opensearch`)", + "url": "https://docs.litellm.ai/docs/providers/opensearch_vector_stores", + "endpoints": { + "vector_stores_search": true + } + }, "helicone": { "display_name": "Helicone (`helicone`)", "url": "https://docs.litellm.ai/docs/providers/helicone", diff --git a/tests/unit/llms/opensearch/__init__.py b/tests/unit/llms/opensearch/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/unit/llms/opensearch/vector_stores/__init__.py b/tests/unit/llms/opensearch/vector_stores/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/unit/llms/opensearch/vector_stores/test_opensearch_transformation.py b/tests/unit/llms/opensearch/vector_stores/test_opensearch_transformation.py new file mode 100644 index 00000000000..56fdb25949a --- /dev/null +++ b/tests/unit/llms/opensearch/vector_stores/test_opensearch_transformation.py @@ -0,0 +1,292 @@ +import hashlib +import json +import re +from collections.abc import Callable, Mapping +from datetime import datetime, timezone +from typing import Final +from unittest.mock import MagicMock + +import httpx +import openai +import pytest +from botocore.auth import SigV4Auth +from botocore.awsrequest import AWSRequest +from botocore.credentials import Credentials + +import litellm +from litellm.litellm_core_utils.litellm_logging import Logging +from litellm.llms.base_llm.chat.transformation import BaseLLMException +from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler, HTTPHandler +from litellm.llms.opensearch.vector_stores.transformation import OpenSearchVectorStoreConfig +from litellm.types.utils import EmbeddingResponse +from litellm.types.vector_stores import VectorStoreSearchResponse + +ACCESS_KEY: Final = "AKIDOPENSEARCHTEST" +SECRET_KEY: Final = "opensearch-test-secret" +QUERY: Final = "How many days a year can I work abroad?" +QUERY_VECTOR: Final = [0.1, 0.2, 0.3] +SERVERLESS: Final = "https://abc123.us-east-1.aoss.amazonaws.com" +BASE_PARAMS: Final = { + "api_base": SERVERLESS, + "aws_access_key_id": ACCESS_KEY, + "aws_secret_access_key": SECRET_KEY, + "litellm_embedding_model": "embedding-alias", +} +_AUTHORIZATION: Final = re.compile( + r"\AAWS4-HMAC-SHA256 Credential=(?P[^/]+)/(?P\d{8})/(?P[^/]+)/(?P[^/]+)/aws4_request, " + r"SignedHeaders=(?P[^,]+), Signature=(?P[0-9a-f]{64})\Z" +) + + +class RecordingEmbeddingExecutor: + def __init__(self) -> None: + self.call: Final = MagicMock(return_value=EmbeddingResponse(data=[{"embedding": QUERY_VECTOR}])) + + def embed(self, model: str, query: str, configuration: Mapping[str, object]) -> EmbeddingResponse: + return self.call(model, query, configuration) + + async def aembed(self, model: str, query: str, configuration: Mapping[str, object]) -> EmbeddingResponse: + return self.call(model, query, configuration) + + +def _signature_scope(request: httpx.Request) -> tuple[str, str]: + authorization: Final = _AUTHORIZATION.match(request.headers["authorization"]) + assert authorization is not None, request.headers["authorization"] + signed_names: Final = authorization["signed"].split(";") + assert {"host", "x-amz-content-sha256", "x-amz-date"} <= set(signed_names), signed_names + assert request.headers["x-amz-content-sha256"] == hashlib.sha256(request.content).hexdigest() + signed_request: Final = AWSRequest( + method=request.method, + url=str(request.url), + data=request.content, + headers={name: request.headers[name] for name in signed_names}, + ) + signed_request.context["timestamp"] = request.headers["x-amz-date"] + signer: Final = SigV4Auth(Credentials(ACCESS_KEY, SECRET_KEY), authorization["service"], authorization["region"]) + expected: Final = signer.signature( + signer.string_to_sign(signed_request, signer.canonical_request(signed_request)), signed_request + ) + assert authorization["key"] == ACCESS_KEY + assert authorization["signature"] == expected, "SigV4 signature does not cover the request that was sent" + return authorization["service"], authorization["region"] + + +def _sent(respond: MagicMock) -> tuple[httpx.Request, ...]: + return tuple(call.args[0] for call in respond.call_args_list) + + +def _opensearch_hits(text_field: str, embedding_field: str) -> Mapping[str, object]: + return { + "took": 4, + "timed_out": False, + "hits": { + "total": {"value": 2, "relation": "eq"}, + "max_score": 0.581, + "hits": [ + { + "_index": "litellm-qa-docs", + "_id": "1%3A0%3Aw6ftW6EB", + "_score": 0.581, + "_source": { + text_field: "Employees may work abroad for at most 37 calendar days per year.", + embedding_field: [0.5, 0.5, 0.5], + "filename": "hr-remote-work-policy.md", + "category": "hr", + }, + }, + {"_index": "litellm-qa-docs", "_id": "doc-2", "_score": 0.42, "_source": {"category": "facilities"}}, + ], + }, + } + + +EXPECTED_RESULTS: Final = [ + { + "score": 0.581, + "content": [{"type": "text", "text": "Employees may work abroad for at most 37 calendar days per year."}], + "file_id": "1%3A0%3Aw6ftW6EB", + "filename": "hr-remote-work-policy.md", + "attributes": {"filename": "hr-remote-work-policy.md", "category": "hr"}, + }, + {"score": 0.42, "content": [], "file_id": "doc-2", "filename": None, "attributes": {"category": "facilities"}}, +] + + +async def _search( + asynchronous: bool, + respond: Callable[[httpx.Request], httpx.Response], + executor: RecordingEmbeddingExecutor, + kwargs: Mapping[str, object], +) -> VectorStoreSearchResponse: + request_kwargs: Final = { + **BASE_PARAMS, + "vector_store_id": "litellm-qa-docs", + "query": QUERY, + "custom_llm_provider": "opensearch", + "_direct_vector_store_embedding_executor": executor, + **kwargs, + } + if asynchronous: + return await litellm.vector_stores.asearch( + **request_kwargs, client=AsyncHTTPHandler(transport=httpx.MockTransport(respond)) + ) + with httpx.Client(transport=httpx.MockTransport(respond)) as transport: + return litellm.vector_stores.search(**request_kwargs, client=HTTPHandler(client=transport)) + + +@pytest.mark.parametrize("asynchronous", [False, True]) +@pytest.mark.parametrize( + "fields", + [{}, {"opensearch_embedding_field": "passage_vector", "opensearch_text_field": "passage"}], +) +@pytest.mark.parametrize( + "api_base,region_params,service,region", + [ + (SERVERLESS, {}, "aoss", "us-east-1"), + (SERVERLESS, {"aws_region_name": "us-west-2"}, "aoss", "us-west-2"), + ("https://search-docs-xyz.eu-west-1.es.amazonaws.com/", {}, "es", "eu-west-1"), + ("https://vpc-docs-xyz.cn-north-1.es.amazonaws.com.cn", {}, "es", "cn-north-1"), + ("https://search.docs.example", {"aws_region_name": "ap-south-1"}, "es", "ap-south-1"), + ], +) +@pytest.mark.asyncio +async def test_search_sends_a_signed_knn_query_and_returns_an_openai_results_page( + asynchronous: bool, + fields: Mapping[str, str], + api_base: str, + region_params: Mapping[str, str], + service: str, + region: str, +) -> None: + executor: Final = RecordingEmbeddingExecutor() + embedding_field: Final = fields.get("opensearch_embedding_field", "embedding") + text_field: Final = fields.get("opensearch_text_field", "text") + respond: Final = MagicMock(return_value=httpx.Response(200, json=_opensearch_hits(text_field, embedding_field))) + + response: Final = await _search( + asynchronous, + respond, + executor, + {**fields, **region_params, "api_base": api_base, "max_num_results": 3}, + ) + + assert response == {"object": "vector_store.search_results.page", "search_query": QUERY, "data": EXPECTED_RESULTS} + sent: Final = _sent(respond) + assert len(sent) == 1 + assert str(sent[0].url) == f"{api_base.rstrip('/')}/litellm-qa-docs/_search" + assert json.loads(sent[0].content) == { + "size": 3, + "query": {"knn": {embedding_field: {"vector": QUERY_VECTOR, "k": 3}}}, + "_source": {"excludes": [embedding_field]}, + } + assert _signature_scope(sent[0]) == (service, region) + executor.call.assert_called_once_with("embedding-alias", QUERY, {}) + + +@pytest.mark.asyncio +async def test_search_defaults_to_ten_results_like_openai() -> None: + respond: Final = MagicMock(return_value=httpx.Response(200, json={"hits": {"hits": []}})) + + response: Final = await _search(True, respond, RecordingEmbeddingExecutor(), {}) + + assert response == {"object": "vector_store.search_results.page", "search_query": QUERY, "data": []} + body: Final = json.loads(_sent(respond)[0].content) + assert (body["size"], body["query"]["knn"]["embedding"]["k"]) == (10, 10) + + +@pytest.mark.parametrize("asynchronous", [False, True]) +@pytest.mark.asyncio +async def test_a_list_query_is_embedded_and_echoed_as_one_string(asynchronous: bool) -> None: + executor: Final = RecordingEmbeddingExecutor() + respond: Final = MagicMock(return_value=httpx.Response(200, json={"hits": {"hits": []}})) + + response: Final = await _search(asynchronous, respond, executor, {"query": ["remote work", "abroad"]}) + + assert response["search_query"] == "remote work abroad" + executor.call.assert_called_once_with("embedding-alias", "remote work abroad", {}) + + +@pytest.mark.parametrize("asynchronous", [False, True]) +@pytest.mark.parametrize("status", [403, 404, 429, 500]) +@pytest.mark.asyncio +async def test_opensearch_error_keeps_its_status(asynchronous: bool, status: int) -> None: + def respond(request: httpx.Request) -> httpx.Response: + return httpx.Response(status, json={"status": status, "error": {"reason": "upstream refused", "type": "x"}}) + + with pytest.raises(openai.APIStatusError) as error: + await _search(asynchronous, respond, RecordingEmbeddingExecutor(), {}) + + assert error.value.status_code == status + + +@pytest.mark.parametrize( + "overrides,message", + [ + ({"filters": {"type": "eq", "key": "category", "value": "hr"}}, "filters"), + ({"ranking_options": {"score_threshold": 0.5}}, "ranking_options"), + ({"rewrite_query": True}, "rewrite_query"), + ({"max_num_results": 0}, "max_num_results"), + ({"max_num_results": 51}, "max_num_results"), + ({"litellm_embedding_model": None}, "litellm_embedding_model"), + ({"opensearch_text_feild": "passage"}, "opensearch_text_feild"), + ({"opensearch_text_field": 7}, "opensearch_text_field"), + ({"extra_body": {"min_score": 0.5}}, "extra_body"), + ({"api_base": None}, "api_base"), + ({"api_base": "abc123.us-east-1.aoss.amazonaws.com"}, "api_base"), + ], +) +@pytest.mark.asyncio +async def test_unsupported_search_is_rejected_before_embedding_or_any_request( + overrides: Mapping[str, object], message: str +) -> None: + executor: Final = RecordingEmbeddingExecutor() + respond: Final = MagicMock(return_value=httpx.Response(200, json={"hits": {"hits": []}})) + + with pytest.raises(litellm.BadRequestError, match=message) as error: + await _search(True, respond, executor, overrides) + + assert error.value.status_code == 400 + respond.assert_not_called() + executor.call.assert_not_called() + + +@pytest.mark.asyncio +async def test_no_op_values_of_unsupported_params_are_accepted() -> None: + def respond(request: httpx.Request) -> httpx.Response: + return httpx.Response(200, json=_opensearch_hits("text", "embedding")) + + response: Final = await _search( + True, respond, RecordingEmbeddingExecutor(), {"filters": {}, "ranking_options": {}, "rewrite_query": False} + ) + + assert response["data"] == EXPECTED_RESULTS + + +def test_create_is_rejected_because_the_store_is_search_only() -> None: + with pytest.raises(litellm.BadRequestError, match="search-only"): + litellm.vector_stores.create(custom_llm_provider="opensearch") + + +@pytest.mark.parametrize( + "response,status", + [ + (httpx.Response(403, json={"status": 403, "error": {"type": "Forbidden"}}), 403), + (httpx.Response(200, json={"took": 1}), 502), + (httpx.Response(200, text="gateway"), 502), + ], +) +def test_response_without_search_hits_is_an_error_not_an_empty_page(response: httpx.Response, status: int) -> None: + logging: Final = Logging( + model="", + messages=[], + stream=False, + call_type="vector_store_search", + start_time=datetime(2026, 10, 20, tzinfo=timezone.utc), + litellm_call_id="opensearch-test", + function_id="opensearch-test", + ) + + with pytest.raises(BaseLLMException) as error: + OpenSearchVectorStoreConfig().transform_search_vector_store_response(response, logging) + + assert error.value.status_code == status diff --git a/tests/unit/proxy/public_endpoints/test_public_endpoints.py b/tests/unit/proxy/public_endpoints/test_public_endpoints.py index 8f27365fcc1..f0ca007aaf2 100644 --- a/tests/unit/proxy/public_endpoints/test_public_endpoints.py +++ b/tests/unit/proxy/public_endpoints/test_public_endpoints.py @@ -523,6 +523,7 @@ ADD_MODEL_UNLISTED_PROVIDERS: Final = frozenset( "mongodb", "nano-gpt", "neosantara", + "opensearch", "parasail", "pinstripes", "poe", From 7d90f764484da8865c86aca4c02ac197f73ba558 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Tue, 20 Oct 2026 19:14:50 -0700 Subject: [PATCH 2/6] feat(ui): add Amazon OpenSearch to the vector store provider dropdown --- .../_components/CreateVectorStore.test.tsx | 6 ++- .../_components/CreateVectorStore.tsx | 2 +- .../VectorStoreForm.integration.test.tsx | 38 +++++++++++++ .../_components/VectorStoreForm.tsx | 38 ++++++++++++- .../vector_store_providers.test.tsx | 23 ++++++++ .../src/components/vector_store_providers.tsx | 53 +++++++++++++++++++ 6 files changed, 157 insertions(+), 3 deletions(-) diff --git a/ui/litellm-dashboard/src/app/(dashboard)/vector-stores/_components/CreateVectorStore.test.tsx b/ui/litellm-dashboard/src/app/(dashboard)/vector-stores/_components/CreateVectorStore.test.tsx index 9b7e60aa12a..487f256d6a0 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/vector-stores/_components/CreateVectorStore.test.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/vector-stores/_components/CreateVectorStore.test.tsx @@ -18,6 +18,7 @@ vi.mock("@/components/vector_store_providers", () => ({ AZURE_OPENAI: "Azure OpenAI", S3Vectors: "AWS S3 Vectors", Valkey: "Valkey", + OpenSearch: "Amazon OpenSearch", }, vectorStoreProviderMap: { BEDROCK: "bedrock", @@ -25,6 +26,7 @@ vi.mock("@/components/vector_store_providers", () => ({ AZURE_OPENAI: "azure_openai", S3Vectors: "s3_vectors", Valkey: "valkey", + OpenSearch: "opensearch", }, vectorStoreProviderLogoMap: { "Amazon Bedrock": "https://example.com/bedrock.png", @@ -32,6 +34,7 @@ vi.mock("@/components/vector_store_providers", () => ({ "Azure OpenAI": "https://example.com/azure.png", "AWS S3 Vectors": "https://example.com/aws.png", Valkey: "https://example.com/valkey.svg", + "Amazon OpenSearch": "https://example.com/aws.svg", }, getProviderSpecificFields: vi.fn((provider: string) => { if (provider === "s3_vectors") { @@ -254,7 +257,7 @@ describe("CreateVectorStore", () => { expect(screen.getByText("Vector Store Created Successfully")).toBeInTheDocument(); }); - it("should exclude valkey from the provider dropdown since it has no RAG ingestion", async () => { + it("should exclude search-only valkey and opensearch from the provider dropdown since they have no RAG ingestion", async () => { render(); const providerSelect = screen.getByRole("combobox"); @@ -267,6 +270,7 @@ describe("CreateVectorStore", () => { expect(screen.getByText("AWS S3 Vectors")).toBeInTheDocument(); }); expect(screen.queryByText("Valkey")).not.toBeInTheDocument(); + expect(screen.queryByText("Amazon OpenSearch")).not.toBeInTheDocument(); }); it("should display S3 Vectors provider-specific fields when selected", async () => { diff --git a/ui/litellm-dashboard/src/app/(dashboard)/vector-stores/_components/CreateVectorStore.tsx b/ui/litellm-dashboard/src/app/(dashboard)/vector-stores/_components/CreateVectorStore.tsx index 2422e83355d..2d7375431cd 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/vector-stores/_components/CreateVectorStore.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/vector-stores/_components/CreateVectorStore.tsx @@ -36,7 +36,7 @@ const ACCEPTED_DOCUMENT_TYPES = [ const MAX_DOCUMENT_BYTES = 50 * 1024 * 1024; -const RAG_INGEST_UNSUPPORTED_PROVIDERS = new Set(["valkey"]); +const RAG_INGEST_UNSUPPORTED_PROVIDERS = new Set(["valkey", "opensearch"]); const providerItems = Object.entries(VectorStoreProviders) .filter(([providerEnum]) => !RAG_INGEST_UNSUPPORTED_PROVIDERS.has(vectorStoreProviderMap[providerEnum])) diff --git a/ui/litellm-dashboard/src/app/(dashboard)/vector-stores/_components/VectorStoreForm.integration.test.tsx b/ui/litellm-dashboard/src/app/(dashboard)/vector-stores/_components/VectorStoreForm.integration.test.tsx index d7ec325653f..0f245770ad3 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/vector-stores/_components/VectorStoreForm.integration.test.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/vector-stores/_components/VectorStoreForm.integration.test.tsx @@ -134,6 +134,44 @@ describe("VectorStoreForm submit payload", () => { expect(createdPayload().custom_llm_provider).toBe("milvus"); }); + it("registers an existing opensearch index with the proxy embedding model and default field names", async () => { + const user = setupUser(); + renderForm(); + + await chooseProvider(user, "Amazon OpenSearch"); + await user.type(screen.getByPlaceholderText("my-knn-index (OpenSearch index name)"), "litellm-qa-docs"); + await user.type( + screen.getByPlaceholderText("https://abc123.us-east-1.aoss.amazonaws.com"), + "https://yzatq55tpsuvlwozcal4.us-east-1.aoss.amazonaws.com", + ); + await chooseFromSelect(user, 1, "text-embedding-3-small"); + await submit(user); + + await vi.waitFor(() => expect(mockCreate).toHaveBeenCalledTimes(1)); + expect(createdPayload().custom_llm_provider).toBe("opensearch"); + expect(createdPayload().vector_store_id).toBe("litellm-qa-docs"); + expect(createdPayload().litellm_params).toStrictEqual({ + api_base: "https://yzatq55tpsuvlwozcal4.us-east-1.aoss.amazonaws.com", + aws_region_name: undefined, + litellm_embedding_model: "text-embedding-3-small", + opensearch_embedding_field: "embedding", + opensearch_text_field: "text", + }); + }); + + it("blocks an opensearch submit that has no endpoint or embedding model", async () => { + const user = setupUser(); + renderForm(); + + await chooseProvider(user, "Amazon OpenSearch"); + await user.type(screen.getByPlaceholderText("my-knn-index (OpenSearch index name)"), "litellm-qa-docs"); + await submit(user); + + expect(await screen.findByText("Please input the opensearch endpoint")).toBeInTheDocument(); + expect(screen.getByText("Please select the embedding model")).toBeInTheDocument(); + expect(mockCreate).not.toHaveBeenCalled(); + }); + it("sends a provider field's seeded default even when the user never touches it", async () => { const user = setupUser(); renderForm(); diff --git a/ui/litellm-dashboard/src/app/(dashboard)/vector-stores/_components/VectorStoreForm.tsx b/ui/litellm-dashboard/src/app/(dashboard)/vector-stores/_components/VectorStoreForm.tsx index d82f56f3870..6658971b968 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/vector-stores/_components/VectorStoreForm.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/vector-stores/_components/VectorStoreForm.tsx @@ -36,7 +36,7 @@ import { useZodForm } from "@/lib/forms/useZodForm"; import { SearchSelect } from "@/components/shared/SearchSelect"; import { credentialOptions } from "@/components/shared/credentialOptions"; -const EMBEDDING_MODEL_RENAME_PROVIDERS = new Set(["milvus", "valkey", "mongodb"]); +const EMBEDDING_MODEL_RENAME_PROVIDERS = new Set(["milvus", "valkey", "mongodb", "opensearch"]); export const buildVectorStoreLitellmParams = ( provider: string, @@ -83,6 +83,8 @@ const PROVIDER_FIELD_NAMES = [ "valkey_ssl", "valkey_text_field", "valkey_embedding_field", + "opensearch_embedding_field", + "opensearch_text_field", ] as const; type ProviderFieldName = (typeof PROVIDER_FIELD_NAMES)[number]; @@ -119,6 +121,8 @@ const vectorStoreShape = { valkey_ssl: optionalText, valkey_text_field: optionalText, valkey_embedding_field: optionalText, + opensearch_embedding_field: optionalText, + opensearch_text_field: optionalText, }; const vectorStoreSchema = z.object(vectorStoreShape).superRefine((values, ctx) => { @@ -143,6 +147,7 @@ const VECTOR_STORE_ID_PLACEHOLDERS: Record = { "vertex_ai/search_api": 'my-datastore_1234567890 (data store ID from Vertex AI / "Agent Search" console)', valkey: "my-search-index (FT index name in Valkey)", mongodb: "my-vector-index (MongoDB Vector Search index name)", + opensearch: "my-knn-index (OpenSearch index name)", }; const VERTEX_SEARCH_API_WITH_ENGINE_PLACEHOLDER = "Any identifier you'll use to reference this in LiteLLM"; @@ -159,6 +164,8 @@ const EMPTY_VALUES: VectorStoreFormValues = { valkey_ssl: "false", valkey_text_field: "text", valkey_embedding_field: "embedding", + opensearch_embedding_field: "embedding", + opensearch_text_field: "text", }; const labelWithHint = (label: string, hint: string): React.ReactNode => ( @@ -376,6 +383,35 @@ const VectorStoreForm: React.FC = ({ )} + {selectedProvider === "opensearch" && ( + + + OpenSearch Setup Required + +

+ LiteLLM searches a k-NN index you already have in an OpenSearch Serverless collection or an + OpenSearch Service domain. It does not create the index or upload documents. Before creating this + vector store, make sure: +

+
    +
  1. + The index has a knn_vector field holding each document's embedding. Enter the index name as + the Vector Store ID +
  2. +
  3. + You know which embedding model created those embeddings. That model must be added to this proxy + under Models so you can pick it below. Using a different model returns wrong results +
  4. +
  5. + The proxy has AWS credentials that can read the index, either its own (environment variables or + an IAM role) or a saved credential picked under Existing Credentials. A Serverless collection + needs a data access policy for them, and a domain needs its access policy to allow them +
  6. +
+
+
+ )} + {selectedProvider === "vertex_rag_engine" && ( diff --git a/ui/litellm-dashboard/src/components/vector_store_providers.test.tsx b/ui/litellm-dashboard/src/components/vector_store_providers.test.tsx index 32d0f5dccc0..151f3549ad8 100644 --- a/ui/litellm-dashboard/src/components/vector_store_providers.test.tsx +++ b/ui/litellm-dashboard/src/components/vector_store_providers.test.tsx @@ -68,6 +68,29 @@ describe("getVectorStoreProviderLogoAndName", () => { expect(byName("mongodb_num_candidates")).toMatchObject({ required: false }); }); + it("registers opensearch with its endpoint, optional region, proxy embedding model, and default field names", () => { + const fields = getProviderSpecificFields("opensearch"); + const byName = (name: string) => fields.find((field) => field.name === name); + + expect(getVectorStoreProviderLogoAndName("opensearch")).toEqual({ + logo: expect.stringContaining("aws"), + displayName: VectorStoreProviders.OpenSearch, + }); + expect(fields.map((field) => field.name)).toEqual([ + "api_base", + "aws_region_name", + "embedding_model", + "opensearch_embedding_field", + "opensearch_text_field", + ]); + expect(byName("api_base")).toMatchObject({ required: true }); + expect(byName("aws_region_name")).toMatchObject({ required: false }); + expect(byName("embedding_model")).toMatchObject({ type: "select", required: true }); + expect(byName("embedding_model")).not.toHaveProperty("options"); + expect(byName("opensearch_embedding_field")).toMatchObject({ required: false, initialValue: "embedding" }); + expect(byName("opensearch_text_field")).toMatchObject({ required: false, initialValue: "text" }); + }); + it("registers valkey in the provider, logo, and field maps", () => { expect(vectorStoreProviderMap.Valkey).toBe("valkey"); expect(vectorStoreProviderLogoMap[VectorStoreProviders.Valkey]).toContain("valkey"); diff --git a/ui/litellm-dashboard/src/components/vector_store_providers.tsx b/ui/litellm-dashboard/src/components/vector_store_providers.tsx index 79e711f6f98..ec247b243b5 100644 --- a/ui/litellm-dashboard/src/components/vector_store_providers.tsx +++ b/ui/litellm-dashboard/src/components/vector_store_providers.tsx @@ -1,4 +1,5 @@ import { getProviderLogoAndName, Providers, providerLogoMap } from "@/components/provider_info_helpers"; +import awsLogo from "../../public/assets/logos/aws.svg"; import milvusLogo from "../../public/assets/logos/milvus.svg"; import mongodbLogo from "../../public/assets/logos/mongodb.svg"; import postgresqlLogo from "../../public/assets/logos/postgresql.svg"; @@ -8,6 +9,7 @@ import valkeyLogo from "../../public/assets/logos/valkey.svg"; export enum VectorStoreProviders { Bedrock = "Amazon Bedrock", S3Vectors = "Amazon S3 Vectors", + OpenSearch = "Amazon OpenSearch", PgVector = "PostgreSQL pgvector (LiteLLM Connector)", VertexRagEngine = "Vertex AI RAG Engine", VertexAiSearch = "Vertex AI Search", @@ -28,6 +30,7 @@ export const vectorStoreProviderMap: Record = { Milvus: "milvus", MongoDB: "mongodb", S3Vectors: "s3_vectors", + OpenSearch: "opensearch", Valkey: "valkey", }; @@ -41,6 +44,7 @@ export const vectorStoreProviderLogoMap: Record = { [VectorStoreProviders.Milvus]: milvusLogo.src, [VectorStoreProviders.MongoDB]: mongodbLogo.src, [VectorStoreProviders.S3Vectors]: s3VectorLogo.src, + [VectorStoreProviders.OpenSearch]: awsLogo.src, [VectorStoreProviders.Valkey]: valkeyLogo.src, }; @@ -313,6 +317,55 @@ export const vectorStoreProviderFields: Record initialValue: "embedding", }, ], + opensearch: [ + { + name: "api_base", + label: "OpenSearch Endpoint", + tooltip: + "The endpoint of your OpenSearch Serverless collection or OpenSearch Service domain, starting with https://", + placeholder: "https://abc123.us-east-1.aoss.amazonaws.com", + required: true, + type: "text", + }, + { + name: "aws_region_name", + label: "AWS Region (optional)", + tooltip: + "The AWS region of the collection or domain. Leave blank to use the region in the endpoint, and set it when the endpoint is a custom domain", + placeholder: "us-east-1", + required: false, + type: "text", + }, + { + name: "embedding_model", + label: "Embedding Model", + tooltip: + "The embedding model on this proxy that created the vectors already stored in your index. LiteLLM embeds every search query with it, so it must be the same model. Add it under Models first if it is not listed", + placeholder: "text-embedding-3-small", + required: true, + type: "select", + }, + { + name: "opensearch_embedding_field", + label: "Vector Field Name", + tooltip: + "The top-level knn_vector field in your index mapping that holds each document's embedding (default: embedding)", + placeholder: "embedding", + required: false, + type: "text", + initialValue: "embedding", + }, + { + name: "opensearch_text_field", + label: "Text Field", + tooltip: + "The top-level field in each document that holds its readable text. LiteLLM returns this text in search results (default: text)", + placeholder: "text", + required: false, + type: "text", + initialValue: "text", + }, + ], s3_vectors: [ { name: "vector_bucket_name", From 37e2b86d4251a587f34f97dc8d9d74c745d374d9 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Tue, 20 Oct 2026 19:42:28 -0700 Subject: [PATCH 3/6] feat(vector_stores): add OpenSearch k-NN search as a vector store provider --- litellm/llms/opensearch/__init__.py | 0 .../llms/opensearch/vector_stores/__init__.py | 0 .../vector_stores/transformation.py | 279 ++++++++++++++++++ litellm/types/utils.py | 1 + litellm/utils.py | 35 ++- provider_endpoints_support.json | 7 + tests/unit/llms/opensearch/__init__.py | 0 .../llms/opensearch/vector_stores/__init__.py | 0 .../test_opensearch_transformation.py | 242 +++++++++++++++ .../public_endpoints/test_public_endpoints.py | 1 + 10 files changed, 553 insertions(+), 12 deletions(-) create mode 100644 litellm/llms/opensearch/__init__.py create mode 100644 litellm/llms/opensearch/vector_stores/__init__.py create mode 100644 litellm/llms/opensearch/vector_stores/transformation.py create mode 100644 tests/unit/llms/opensearch/__init__.py create mode 100644 tests/unit/llms/opensearch/vector_stores/__init__.py create mode 100644 tests/unit/llms/opensearch/vector_stores/test_opensearch_transformation.py diff --git a/litellm/llms/opensearch/__init__.py b/litellm/llms/opensearch/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/litellm/llms/opensearch/vector_stores/__init__.py b/litellm/llms/opensearch/vector_stores/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/litellm/llms/opensearch/vector_stores/transformation.py b/litellm/llms/opensearch/vector_stores/transformation.py new file mode 100644 index 00000000000..81042c6a22a --- /dev/null +++ b/litellm/llms/opensearch/vector_stores/transformation.py @@ -0,0 +1,279 @@ +from collections.abc import Mapping, Sequence +from typing import TYPE_CHECKING, Final, NoReturn +from urllib.parse import quote, urlsplit + +import httpx +from pydantic import ConfigDict, Field, TypeAdapter, ValidationError + +from litellm.exceptions import BadRequestError +from litellm.llms.base_llm.chat.transformation import BaseLLMException +from litellm.llms.base_llm.vector_store.transformation import ( + BaseQueryEmbeddingVectorStoreConfig, + VectorStoreEmbeddingExecutor, +) +from litellm.types.llms.base import LiteLLMBaseModel +from litellm.types.router import GenericLiteLLMParams +from litellm.types.utils import EmbeddingResponse +from litellm.types.vector_stores import ( + BaseVectorStoreAuthCredentials, + VectorStoreCreateOptionalRequestParams, + VectorStoreIndexEndpoints, + VectorStoreResultContent, + VectorStoreSearchOptionalRequestParams, + VectorStoreSearchResponse, + VectorStoreSearchResult, +) + +if TYPE_CHECKING: + from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj + +DEFAULT_EMBEDDING_FIELD: Final = "embedding" +DEFAULT_TEXT_FIELD: Final = "text" +DEFAULT_MAX_NUM_RESULTS: Final = 10 +MAX_MAX_NUM_RESULTS: Final = 50 +_PARAM_PREFIX: Final = "opensearch_" +_QUERY_VECTOR: Final = TypeAdapter(tuple[float, ...]) +_SEARCH_ONLY_MESSAGE: Final = ( + "The OpenSearch vector store is search-only. Create the k-NN index in OpenSearch, " + "then register it here with the index name as vector_store_id." +) + + +def _bad_request(message: str) -> BadRequestError: + return BadRequestError(message=message, model=None, llm_provider="opensearch") + + +class _OpenSearchParams(LiteLLMBaseModel): + model_config = ConfigDict(frozen=True, extra="ignore") + + litellm_embedding_model: str | None = None + opensearch_embedding_field: str | None = None + opensearch_text_field: str | None = None + + @property + def embedding_field(self) -> str: + return self.opensearch_embedding_field or DEFAULT_EMBEDDING_FIELD + + @property + def text_field(self) -> str: + return self.opensearch_text_field or DEFAULT_TEXT_FIELD + + def require_embedding_model(self) -> str: + if not self.litellm_embedding_model: + raise _bad_request( + "litellm_embedding_model is required for the OpenSearch vector store. It must be the model that " + f"produced the vectors in '{self.embedding_field}', for example openai/text-embedding-3-small." + ) + return self.litellm_embedding_model + + +class _SearchOptions(LiteLLMBaseModel): + model_config = ConfigDict(frozen=True, extra="ignore") + + max_num_results: int | None = None + filters: Mapping[str, object] | None = None + ranking_options: Mapping[str, object] | None = None + rewrite_query: bool | None = None + + @property + def unsupported(self) -> tuple[str, ...]: + requested: Final = ( + ("filters", self.filters), + ("ranking_options", self.ranking_options), + ("rewrite_query", self.rewrite_query), + ) + return tuple(name for name, value in requested if value) + + @property + def k(self) -> int: + if self.max_num_results is None: + return DEFAULT_MAX_NUM_RESULTS + if not 1 <= self.max_num_results <= MAX_MAX_NUM_RESULTS: + raise _bad_request( + f"max_num_results must be between 1 and {MAX_MAX_NUM_RESULTS}, got {self.max_num_results}" + ) + return self.max_num_results + + +_KNOWN_PARAMS: Final = frozenset(name for name in _OpenSearchParams.model_fields if name.startswith(_PARAM_PREFIX)) + + +class _LoggedSearch(LiteLLMBaseModel): + model_config = ConfigDict(frozen=True, extra="ignore") + + query: str | tuple[str, ...] = "" + litellm_params: _OpenSearchParams = _OpenSearchParams() + + @property + def search_query(self) -> str: + return BaseQueryEmbeddingVectorStoreConfig.query_text(self.query) + + +class _Hit(LiteLLMBaseModel): + model_config = ConfigDict(frozen=True) + + id: str = Field(alias="_id") + score: float | None = Field(default=None, alias="_score") + source: Mapping[str, object] = Field(default_factory=dict, alias="_source") + + +class _Hits(LiteLLMBaseModel): + model_config = ConfigDict(frozen=True) + + hits: Sequence[_Hit] + + +class _SearchResponse(LiteLLMBaseModel): + model_config = ConfigDict(frozen=True) + + hits: _Hits + + +def _search_result(hit: _Hit, params: _OpenSearchParams) -> VectorStoreSearchResult: + text: Final = hit.source.get(params.text_field) + filename: Final = hit.source.get("filename") + return VectorStoreSearchResult( + score=hit.score, + content=[VectorStoreResultContent(type="text", text=text)] if isinstance(text, str) else [], + file_id=hit.id, + filename=filename if isinstance(filename, str) else None, + attributes={ + key: value for key, value in hit.source.items() if key not in (params.text_field, params.embedding_field) + }, + ) + + +class OpenSearchVectorStoreConfig(BaseQueryEmbeddingVectorStoreConfig): + def get_auth_credentials(self, litellm_params: Mapping[str, object]) -> BaseVectorStoreAuthCredentials: + return BaseVectorStoreAuthCredentials() + + def get_vector_store_endpoints_by_type(self) -> VectorStoreIndexEndpoints: + return VectorStoreIndexEndpoints(read=[], write=[]) + + def validate_environment( + self, headers: Mapping[str, object], litellm_params: GenericLiteLLMParams | None + ) -> dict[str, object]: # mutable-ok: the shared HTTP handler requires writable headers + return {**headers, "Content-Type": "application/json"} + + def get_complete_url(self, api_base: str | None, litellm_params: Mapping[str, object]) -> str: + parsed: Final = urlsplit(api_base or "") + if parsed.scheme not in ("http", "https") or not parsed.hostname: + raise _bad_request( + "api_base must be the OpenSearch endpoint URL, for example https://:9200." + ) + return (api_base or "").rstrip("/") + + @staticmethod + def _params( + litellm_params: Mapping[str, object], + optional_params: VectorStoreSearchOptionalRequestParams, + extra_body: Mapping[str, object] | None, + ) -> tuple[_OpenSearchParams, int]: + unknown: Final = sorted( + key for key in litellm_params if key.startswith(_PARAM_PREFIX) and key not in _KNOWN_PARAMS + ) + if unknown: + raise _bad_request( + f"Unknown OpenSearch vector store parameter(s): {', '.join(unknown)}. " + f"Supported: {', '.join(sorted(_KNOWN_PARAMS))}." + ) + if extra_body: + raise _bad_request("The OpenSearch vector store does not support extra_body.") + try: + options: Final = _SearchOptions.model_validate(optional_params) + except ValidationError as error: + raise _bad_request(f"Invalid vector store search parameters: {error}") from None + if options.unsupported: + raise _bad_request( + f"The OpenSearch vector store does not support {', '.join(options.unsupported)} yet. " + "Remove it from the request." + ) + try: + return _OpenSearchParams.model_validate(litellm_params), options.k + except ValidationError: + raise _bad_request( + "opensearch_embedding_field, opensearch_text_field, and litellm_embedding_model must be strings." + ) from None + + @staticmethod + def _search_request( + vector_store_id: str, + embedding: EmbeddingResponse, + k: int, + params: _OpenSearchParams, + api_base: str, + ) -> tuple[str, dict[str, object]]: # mutable-ok: the provider contract returns a writable JSON request body + vector: Final = _QUERY_VECTOR.validate_python(embedding.data[0]["embedding"]) # pyright: ignore[reportUnknownMemberType] # EmbeddingResponse.data is an untyped list, the vector is validated here + return ( + f"{api_base}/{quote(vector_store_id, safe='')}/_search", + { + "size": k, + "query": {"knn": {params.embedding_field: {"vector": vector, "k": k}}}, + "_source": {"excludes": [params.embedding_field]}, + }, + ) + + def transform_search_vector_store_request( + self, + vector_store_id: str, + query: str | Sequence[str], + vector_store_search_optional_params: VectorStoreSearchOptionalRequestParams, + api_base: str, + litellm_logging_obj: "LiteLLMLoggingObj", + litellm_params: Mapping[str, object], + extra_body: Mapping[str, object] | None = None, + embedding_executor: VectorStoreEmbeddingExecutor | None = None, + ) -> tuple[str, dict[str, object]]: # mutable-ok: the provider contract returns a writable JSON request body + params, k = self._params(litellm_params, vector_store_search_optional_params, extra_body) + embedding: Final = self.query_embedding_executor(embedding_executor, None).embed( + params.require_embedding_model(), self.query_text(query), self.query_embedding_configuration(litellm_params) + ) + return self._search_request(vector_store_id, embedding, k, params, api_base) + + async def atransform_search_vector_store_request( + self, + vector_store_id: str, + query: str | Sequence[str], + vector_store_search_optional_params: VectorStoreSearchOptionalRequestParams, + api_base: str, + litellm_logging_obj: "LiteLLMLoggingObj", + litellm_params: Mapping[str, object], + extra_body: Mapping[str, object] | None = None, + embedding_executor: VectorStoreEmbeddingExecutor | None = None, + ) -> tuple[str, dict[str, object]]: # mutable-ok: the provider contract returns a writable JSON request body + params, k = self._params(litellm_params, vector_store_search_optional_params, extra_body) + embedding: Final = await self.query_embedding_executor(embedding_executor, None).aembed( + params.require_embedding_model(), self.query_text(query), self.query_embedding_configuration(litellm_params) + ) + return self._search_request(vector_store_id, embedding, k, params, api_base) + + def transform_search_vector_store_response( + self, response: httpx.Response, litellm_logging_obj: "LiteLLMLoggingObj" + ) -> VectorStoreSearchResponse: + if not response.is_success: + raise BaseLLMException(status_code=response.status_code, message=response.text, headers=response.headers) + try: + hits: Final = _SearchResponse.model_validate_json(response.content).hits.hits + except ValidationError: + raise BaseLLMException( + status_code=502, + message="OpenSearch answered the search without a hits.hits list.", + headers=response.headers, + ) from None + logged: Final = _LoggedSearch.model_validate(litellm_logging_obj.model_call_details) + return VectorStoreSearchResponse( + object="vector_store.search_results.page", + search_query=logged.search_query, + data=[_search_result(hit, logged.litellm_params) for hit in hits], + ) + + def validate_create_vector_store(self) -> NoReturn: + raise _bad_request(_SEARCH_ONLY_MESSAGE) + + def transform_create_vector_store_request( + self, vector_store_create_optional_params: VectorStoreCreateOptionalRequestParams, api_base: str + ) -> NoReturn: + raise _bad_request(_SEARCH_ONLY_MESSAGE) + + def transform_create_vector_store_response(self, response: httpx.Response) -> NoReturn: + raise _bad_request(_SEARCH_ONLY_MESSAGE) diff --git a/litellm/types/utils.py b/litellm/types/utils.py index 5939a350970..9e8d4a218bb 100644 --- a/litellm/types/utils.py +++ b/litellm/types/utils.py @@ -4507,6 +4507,7 @@ class LlmProviders(str, Enum): S3_VECTORS = "s3_vectors" VALKEY = "valkey" MONGODB = "mongodb" + OPENSEARCH = "opensearch" HELICONE = "helicone" HYPERBOLIC = "hyperbolic" RECRAFT = "recraft" diff --git a/litellm/utils.py b/litellm/utils.py index 379eddb3155..ff160723f68 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -10189,6 +10189,22 @@ class ProviderConfigManager: return BedrockVectorStore.get_initialized_custom_logger() return None + @staticmethod + def _get_vertex_ai_vector_stores_config(api_type: str | None) -> BaseVectorStoreConfig | None: + if api_type == "rag_api" or api_type is None: + from litellm.llms.vertex_ai.vector_stores.rag_api.transformation import ( + VertexVectorStoreConfig, + ) + + return VertexVectorStoreConfig() + if api_type == "search_api": + from litellm.llms.vertex_ai.vector_stores.search_api.transformation import ( + VertexSearchAPIVectorStoreConfig, + ) + + return VertexSearchAPIVectorStoreConfig() + return None + @staticmethod def get_provider_vector_stores_config( provider: LlmProviders, @@ -10210,18 +10226,7 @@ class ProviderConfigManager: return AzureOpenAIVectorStoreConfig() elif litellm.LlmProviders.VERTEX_AI == provider: - if api_type == "rag_api" or api_type is None: # default to rag_api - from litellm.llms.vertex_ai.vector_stores.rag_api.transformation import ( - VertexVectorStoreConfig, - ) - - return VertexVectorStoreConfig() - elif api_type == "search_api": - from litellm.llms.vertex_ai.vector_stores.search_api.transformation import ( - VertexSearchAPIVectorStoreConfig, - ) - - return VertexSearchAPIVectorStoreConfig() + return ProviderConfigManager._get_vertex_ai_vector_stores_config(api_type) elif litellm.LlmProviders.BEDROCK == provider: from litellm.llms.bedrock.vector_stores.transformation import ( BedrockVectorStoreConfig, @@ -10276,6 +10281,12 @@ class ProviderConfigManager: ) return MongoDBVectorStoreConfig() + elif litellm.LlmProviders.OPENSEARCH == provider: + from litellm.llms.opensearch.vector_stores.transformation import ( + OpenSearchVectorStoreConfig, + ) + + return OpenSearchVectorStoreConfig() return None @staticmethod diff --git a/provider_endpoints_support.json b/provider_endpoints_support.json index d65da3f41fd..09714228549 100644 --- a/provider_endpoints_support.json +++ b/provider_endpoints_support.json @@ -3098,6 +3098,13 @@ "vector_stores_search": true } }, + "opensearch": { + "display_name": "Amazon OpenSearch (`opensearch`)", + "url": "https://docs.litellm.ai/docs/providers/opensearch_vector_stores", + "endpoints": { + "vector_stores_search": true + } + }, "helicone": { "display_name": "Helicone (`helicone`)", "url": "https://docs.litellm.ai/docs/providers/helicone", diff --git a/tests/unit/llms/opensearch/__init__.py b/tests/unit/llms/opensearch/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/unit/llms/opensearch/vector_stores/__init__.py b/tests/unit/llms/opensearch/vector_stores/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/unit/llms/opensearch/vector_stores/test_opensearch_transformation.py b/tests/unit/llms/opensearch/vector_stores/test_opensearch_transformation.py new file mode 100644 index 00000000000..98e2171f6fb --- /dev/null +++ b/tests/unit/llms/opensearch/vector_stores/test_opensearch_transformation.py @@ -0,0 +1,242 @@ +import json +from collections.abc import Callable, Mapping +from datetime import datetime, timezone +from typing import Final +from unittest.mock import MagicMock + +import httpx +import openai +import pytest + +import litellm +from litellm.litellm_core_utils.litellm_logging import Logging +from litellm.llms.base_llm.chat.transformation import BaseLLMException +from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler, HTTPHandler +from litellm.llms.opensearch.vector_stores.transformation import OpenSearchVectorStoreConfig +from litellm.types.utils import EmbeddingResponse +from litellm.types.vector_stores import VectorStoreSearchResponse + +QUERY: Final = "How many days a year can I work abroad?" +QUERY_VECTOR: Final = [0.1, 0.2, 0.3] +OPENSEARCH: Final = "https://opensearch.internal:9200" +BASE_PARAMS: Final = {"api_base": OPENSEARCH, "litellm_embedding_model": "embedding-alias"} + + +class RecordingEmbeddingExecutor: + def __init__(self) -> None: + self.call: Final = MagicMock(return_value=EmbeddingResponse(data=[{"embedding": QUERY_VECTOR}])) + + def embed(self, model: str, query: str, configuration: Mapping[str, object]) -> EmbeddingResponse: + return self.call(model, query, configuration) + + async def aembed(self, model: str, query: str, configuration: Mapping[str, object]) -> EmbeddingResponse: + return self.call(model, query, configuration) + + +def _sent(respond: MagicMock) -> tuple[httpx.Request, ...]: + return tuple(call.args[0] for call in respond.call_args_list) + + +def _opensearch_hits(text_field: str, embedding_field: str) -> Mapping[str, object]: + return { + "took": 4, + "timed_out": False, + "hits": { + "total": {"value": 2, "relation": "eq"}, + "max_score": 0.581, + "hits": [ + { + "_index": "litellm-qa-docs", + "_id": "1%3A0%3Aw6ftW6EB", + "_score": 0.581, + "_source": { + text_field: "Employees may work abroad for at most 37 calendar days per year.", + embedding_field: [0.5, 0.5, 0.5], + "filename": "hr-remote-work-policy.md", + "category": "hr", + }, + }, + {"_index": "litellm-qa-docs", "_id": "doc-2", "_score": 0.42, "_source": {"category": "facilities"}}, + ], + }, + } + + +EXPECTED_RESULTS: Final = [ + { + "score": 0.581, + "content": [{"type": "text", "text": "Employees may work abroad for at most 37 calendar days per year."}], + "file_id": "1%3A0%3Aw6ftW6EB", + "filename": "hr-remote-work-policy.md", + "attributes": {"filename": "hr-remote-work-policy.md", "category": "hr"}, + }, + {"score": 0.42, "content": [], "file_id": "doc-2", "filename": None, "attributes": {"category": "facilities"}}, +] + + +async def _search( + asynchronous: bool, + respond: Callable[[httpx.Request], httpx.Response], + executor: RecordingEmbeddingExecutor, + kwargs: Mapping[str, object], +) -> VectorStoreSearchResponse: + request_kwargs: Final = { + **BASE_PARAMS, + "vector_store_id": "litellm-qa-docs", + "query": QUERY, + "custom_llm_provider": "opensearch", + "_direct_vector_store_embedding_executor": executor, + **kwargs, + } + if asynchronous: + return await litellm.vector_stores.asearch( + **request_kwargs, client=AsyncHTTPHandler(transport=httpx.MockTransport(respond)) + ) + with httpx.Client(transport=httpx.MockTransport(respond)) as transport: + return litellm.vector_stores.search(**request_kwargs, client=HTTPHandler(client=transport)) + + +@pytest.mark.parametrize("asynchronous", [False, True]) +@pytest.mark.parametrize( + "fields", + [{}, {"opensearch_embedding_field": "passage_vector", "opensearch_text_field": "passage"}], +) +@pytest.mark.parametrize("api_base", [OPENSEARCH, "http://localhost:9200/", "https://search.docs.example/os/"]) +@pytest.mark.asyncio +async def test_search_sends_an_unsigned_knn_query_and_returns_an_openai_results_page( + asynchronous: bool, fields: Mapping[str, str], api_base: str +) -> None: + executor: Final = RecordingEmbeddingExecutor() + embedding_field: Final = fields.get("opensearch_embedding_field", "embedding") + text_field: Final = fields.get("opensearch_text_field", "text") + respond: Final = MagicMock(return_value=httpx.Response(200, json=_opensearch_hits(text_field, embedding_field))) + + response: Final = await _search( + asynchronous, + respond, + executor, + {**fields, "api_base": api_base, "max_num_results": 3}, + ) + + assert response == {"object": "vector_store.search_results.page", "search_query": QUERY, "data": EXPECTED_RESULTS} + sent: Final = _sent(respond) + assert len(sent) == 1 + assert str(sent[0].url) == f"{api_base.rstrip('/')}/litellm-qa-docs/_search" + assert json.loads(sent[0].content) == { + "size": 3, + "query": {"knn": {embedding_field: {"vector": QUERY_VECTOR, "k": 3}}}, + "_source": {"excludes": [embedding_field]}, + } + assert sent[0].headers["content-type"] == "application/json" + assert "authorization" not in sent[0].headers + executor.call.assert_called_once_with("embedding-alias", QUERY, {}) + + +@pytest.mark.asyncio +async def test_search_defaults_to_ten_results_like_openai() -> None: + respond: Final = MagicMock(return_value=httpx.Response(200, json={"hits": {"hits": []}})) + + response: Final = await _search(True, respond, RecordingEmbeddingExecutor(), {}) + + assert response == {"object": "vector_store.search_results.page", "search_query": QUERY, "data": []} + body: Final = json.loads(_sent(respond)[0].content) + assert (body["size"], body["query"]["knn"]["embedding"]["k"]) == (10, 10) + + +@pytest.mark.parametrize("asynchronous", [False, True]) +@pytest.mark.asyncio +async def test_a_list_query_is_embedded_and_echoed_as_one_string(asynchronous: bool) -> None: + executor: Final = RecordingEmbeddingExecutor() + respond: Final = MagicMock(return_value=httpx.Response(200, json={"hits": {"hits": []}})) + + response: Final = await _search(asynchronous, respond, executor, {"query": ["remote work", "abroad"]}) + + assert response["search_query"] == "remote work abroad" + executor.call.assert_called_once_with("embedding-alias", "remote work abroad", {}) + + +@pytest.mark.parametrize("asynchronous", [False, True]) +@pytest.mark.parametrize("status", [403, 404, 429, 500]) +@pytest.mark.asyncio +async def test_opensearch_error_keeps_its_status(asynchronous: bool, status: int) -> None: + def respond(request: httpx.Request) -> httpx.Response: + return httpx.Response(status, json={"status": status, "error": {"reason": "upstream refused", "type": "x"}}) + + with pytest.raises(openai.APIStatusError) as error: + await _search(asynchronous, respond, RecordingEmbeddingExecutor(), {}) + + assert error.value.status_code == status + + +@pytest.mark.parametrize( + "overrides,message", + [ + ({"filters": {"type": "eq", "key": "category", "value": "hr"}}, "filters"), + ({"ranking_options": {"score_threshold": 0.5}}, "ranking_options"), + ({"rewrite_query": True}, "rewrite_query"), + ({"max_num_results": 0}, "max_num_results"), + ({"max_num_results": 51}, "max_num_results"), + ({"max_num_results": "ten"}, "Invalid vector store search parameters"), + ({"litellm_embedding_model": None}, "litellm_embedding_model"), + ({"opensearch_text_feild": "passage"}, "opensearch_text_feild"), + ({"opensearch_text_field": 7}, "opensearch_text_field"), + ({"extra_body": {"min_score": 0.5}}, "extra_body"), + ({"api_base": None}, "api_base"), + ({"api_base": "opensearch.internal:9200"}, "api_base"), + ], +) +@pytest.mark.asyncio +async def test_unsupported_search_is_rejected_before_embedding_or_any_request( + overrides: Mapping[str, object], message: str +) -> None: + executor: Final = RecordingEmbeddingExecutor() + respond: Final = MagicMock(return_value=httpx.Response(200, json={"hits": {"hits": []}})) + + with pytest.raises(litellm.BadRequestError, match=message) as error: + await _search(True, respond, executor, overrides) + + assert error.value.status_code == 400 + respond.assert_not_called() + executor.call.assert_not_called() + + +@pytest.mark.asyncio +async def test_no_op_values_of_unsupported_params_are_accepted() -> None: + def respond(request: httpx.Request) -> httpx.Response: + return httpx.Response(200, json=_opensearch_hits("text", "embedding")) + + response: Final = await _search( + True, respond, RecordingEmbeddingExecutor(), {"filters": {}, "ranking_options": {}, "rewrite_query": False} + ) + + assert response["data"] == EXPECTED_RESULTS + + +def test_create_is_rejected_because_the_store_is_search_only() -> None: + with pytest.raises(litellm.BadRequestError, match="search-only"): + litellm.vector_stores.create(custom_llm_provider="opensearch") + + +@pytest.mark.parametrize( + "response,status", + [ + (httpx.Response(403, json={"status": 403, "error": {"type": "Forbidden"}}), 403), + (httpx.Response(200, json={"took": 1}), 502), + (httpx.Response(200, text="gateway"), 502), + ], +) +def test_response_without_search_hits_is_an_error_not_an_empty_page(response: httpx.Response, status: int) -> None: + logging: Final = Logging( + model="", + messages=[], + stream=False, + call_type="vector_store_search", + start_time=datetime(2026, 10, 20, tzinfo=timezone.utc), + litellm_call_id="opensearch-test", + function_id="opensearch-test", + ) + + with pytest.raises(BaseLLMException) as error: + OpenSearchVectorStoreConfig().transform_search_vector_store_response(response, logging) + + assert error.value.status_code == status diff --git a/tests/unit/proxy/public_endpoints/test_public_endpoints.py b/tests/unit/proxy/public_endpoints/test_public_endpoints.py index 8f27365fcc1..f0ca007aaf2 100644 --- a/tests/unit/proxy/public_endpoints/test_public_endpoints.py +++ b/tests/unit/proxy/public_endpoints/test_public_endpoints.py @@ -523,6 +523,7 @@ ADD_MODEL_UNLISTED_PROVIDERS: Final = frozenset( "mongodb", "nano-gpt", "neosantara", + "opensearch", "parasail", "pinstripes", "poe", From 4c08c689bf85b3d0caef2335ccc26740a9125bd3 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Tue, 20 Oct 2026 20:13:34 -0700 Subject: [PATCH 4/6] test(vector_stores): hold the OpenSearch provider tests alone for the first review The provider source comes back in the next commits on this PR --- litellm/llms/opensearch/__init__.py | 0 .../llms/opensearch/vector_stores/__init__.py | 0 .../vector_stores/transformation.py | 279 ------------------ litellm/types/utils.py | 1 - litellm/utils.py | 35 +-- provider_endpoints_support.json | 7 - 6 files changed, 12 insertions(+), 310 deletions(-) delete mode 100644 litellm/llms/opensearch/__init__.py delete mode 100644 litellm/llms/opensearch/vector_stores/__init__.py delete mode 100644 litellm/llms/opensearch/vector_stores/transformation.py diff --git a/litellm/llms/opensearch/__init__.py b/litellm/llms/opensearch/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/litellm/llms/opensearch/vector_stores/__init__.py b/litellm/llms/opensearch/vector_stores/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/litellm/llms/opensearch/vector_stores/transformation.py b/litellm/llms/opensearch/vector_stores/transformation.py deleted file mode 100644 index 81042c6a22a..00000000000 --- a/litellm/llms/opensearch/vector_stores/transformation.py +++ /dev/null @@ -1,279 +0,0 @@ -from collections.abc import Mapping, Sequence -from typing import TYPE_CHECKING, Final, NoReturn -from urllib.parse import quote, urlsplit - -import httpx -from pydantic import ConfigDict, Field, TypeAdapter, ValidationError - -from litellm.exceptions import BadRequestError -from litellm.llms.base_llm.chat.transformation import BaseLLMException -from litellm.llms.base_llm.vector_store.transformation import ( - BaseQueryEmbeddingVectorStoreConfig, - VectorStoreEmbeddingExecutor, -) -from litellm.types.llms.base import LiteLLMBaseModel -from litellm.types.router import GenericLiteLLMParams -from litellm.types.utils import EmbeddingResponse -from litellm.types.vector_stores import ( - BaseVectorStoreAuthCredentials, - VectorStoreCreateOptionalRequestParams, - VectorStoreIndexEndpoints, - VectorStoreResultContent, - VectorStoreSearchOptionalRequestParams, - VectorStoreSearchResponse, - VectorStoreSearchResult, -) - -if TYPE_CHECKING: - from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj - -DEFAULT_EMBEDDING_FIELD: Final = "embedding" -DEFAULT_TEXT_FIELD: Final = "text" -DEFAULT_MAX_NUM_RESULTS: Final = 10 -MAX_MAX_NUM_RESULTS: Final = 50 -_PARAM_PREFIX: Final = "opensearch_" -_QUERY_VECTOR: Final = TypeAdapter(tuple[float, ...]) -_SEARCH_ONLY_MESSAGE: Final = ( - "The OpenSearch vector store is search-only. Create the k-NN index in OpenSearch, " - "then register it here with the index name as vector_store_id." -) - - -def _bad_request(message: str) -> BadRequestError: - return BadRequestError(message=message, model=None, llm_provider="opensearch") - - -class _OpenSearchParams(LiteLLMBaseModel): - model_config = ConfigDict(frozen=True, extra="ignore") - - litellm_embedding_model: str | None = None - opensearch_embedding_field: str | None = None - opensearch_text_field: str | None = None - - @property - def embedding_field(self) -> str: - return self.opensearch_embedding_field or DEFAULT_EMBEDDING_FIELD - - @property - def text_field(self) -> str: - return self.opensearch_text_field or DEFAULT_TEXT_FIELD - - def require_embedding_model(self) -> str: - if not self.litellm_embedding_model: - raise _bad_request( - "litellm_embedding_model is required for the OpenSearch vector store. It must be the model that " - f"produced the vectors in '{self.embedding_field}', for example openai/text-embedding-3-small." - ) - return self.litellm_embedding_model - - -class _SearchOptions(LiteLLMBaseModel): - model_config = ConfigDict(frozen=True, extra="ignore") - - max_num_results: int | None = None - filters: Mapping[str, object] | None = None - ranking_options: Mapping[str, object] | None = None - rewrite_query: bool | None = None - - @property - def unsupported(self) -> tuple[str, ...]: - requested: Final = ( - ("filters", self.filters), - ("ranking_options", self.ranking_options), - ("rewrite_query", self.rewrite_query), - ) - return tuple(name for name, value in requested if value) - - @property - def k(self) -> int: - if self.max_num_results is None: - return DEFAULT_MAX_NUM_RESULTS - if not 1 <= self.max_num_results <= MAX_MAX_NUM_RESULTS: - raise _bad_request( - f"max_num_results must be between 1 and {MAX_MAX_NUM_RESULTS}, got {self.max_num_results}" - ) - return self.max_num_results - - -_KNOWN_PARAMS: Final = frozenset(name for name in _OpenSearchParams.model_fields if name.startswith(_PARAM_PREFIX)) - - -class _LoggedSearch(LiteLLMBaseModel): - model_config = ConfigDict(frozen=True, extra="ignore") - - query: str | tuple[str, ...] = "" - litellm_params: _OpenSearchParams = _OpenSearchParams() - - @property - def search_query(self) -> str: - return BaseQueryEmbeddingVectorStoreConfig.query_text(self.query) - - -class _Hit(LiteLLMBaseModel): - model_config = ConfigDict(frozen=True) - - id: str = Field(alias="_id") - score: float | None = Field(default=None, alias="_score") - source: Mapping[str, object] = Field(default_factory=dict, alias="_source") - - -class _Hits(LiteLLMBaseModel): - model_config = ConfigDict(frozen=True) - - hits: Sequence[_Hit] - - -class _SearchResponse(LiteLLMBaseModel): - model_config = ConfigDict(frozen=True) - - hits: _Hits - - -def _search_result(hit: _Hit, params: _OpenSearchParams) -> VectorStoreSearchResult: - text: Final = hit.source.get(params.text_field) - filename: Final = hit.source.get("filename") - return VectorStoreSearchResult( - score=hit.score, - content=[VectorStoreResultContent(type="text", text=text)] if isinstance(text, str) else [], - file_id=hit.id, - filename=filename if isinstance(filename, str) else None, - attributes={ - key: value for key, value in hit.source.items() if key not in (params.text_field, params.embedding_field) - }, - ) - - -class OpenSearchVectorStoreConfig(BaseQueryEmbeddingVectorStoreConfig): - def get_auth_credentials(self, litellm_params: Mapping[str, object]) -> BaseVectorStoreAuthCredentials: - return BaseVectorStoreAuthCredentials() - - def get_vector_store_endpoints_by_type(self) -> VectorStoreIndexEndpoints: - return VectorStoreIndexEndpoints(read=[], write=[]) - - def validate_environment( - self, headers: Mapping[str, object], litellm_params: GenericLiteLLMParams | None - ) -> dict[str, object]: # mutable-ok: the shared HTTP handler requires writable headers - return {**headers, "Content-Type": "application/json"} - - def get_complete_url(self, api_base: str | None, litellm_params: Mapping[str, object]) -> str: - parsed: Final = urlsplit(api_base or "") - if parsed.scheme not in ("http", "https") or not parsed.hostname: - raise _bad_request( - "api_base must be the OpenSearch endpoint URL, for example https://:9200." - ) - return (api_base or "").rstrip("/") - - @staticmethod - def _params( - litellm_params: Mapping[str, object], - optional_params: VectorStoreSearchOptionalRequestParams, - extra_body: Mapping[str, object] | None, - ) -> tuple[_OpenSearchParams, int]: - unknown: Final = sorted( - key for key in litellm_params if key.startswith(_PARAM_PREFIX) and key not in _KNOWN_PARAMS - ) - if unknown: - raise _bad_request( - f"Unknown OpenSearch vector store parameter(s): {', '.join(unknown)}. " - f"Supported: {', '.join(sorted(_KNOWN_PARAMS))}." - ) - if extra_body: - raise _bad_request("The OpenSearch vector store does not support extra_body.") - try: - options: Final = _SearchOptions.model_validate(optional_params) - except ValidationError as error: - raise _bad_request(f"Invalid vector store search parameters: {error}") from None - if options.unsupported: - raise _bad_request( - f"The OpenSearch vector store does not support {', '.join(options.unsupported)} yet. " - "Remove it from the request." - ) - try: - return _OpenSearchParams.model_validate(litellm_params), options.k - except ValidationError: - raise _bad_request( - "opensearch_embedding_field, opensearch_text_field, and litellm_embedding_model must be strings." - ) from None - - @staticmethod - def _search_request( - vector_store_id: str, - embedding: EmbeddingResponse, - k: int, - params: _OpenSearchParams, - api_base: str, - ) -> tuple[str, dict[str, object]]: # mutable-ok: the provider contract returns a writable JSON request body - vector: Final = _QUERY_VECTOR.validate_python(embedding.data[0]["embedding"]) # pyright: ignore[reportUnknownMemberType] # EmbeddingResponse.data is an untyped list, the vector is validated here - return ( - f"{api_base}/{quote(vector_store_id, safe='')}/_search", - { - "size": k, - "query": {"knn": {params.embedding_field: {"vector": vector, "k": k}}}, - "_source": {"excludes": [params.embedding_field]}, - }, - ) - - def transform_search_vector_store_request( - self, - vector_store_id: str, - query: str | Sequence[str], - vector_store_search_optional_params: VectorStoreSearchOptionalRequestParams, - api_base: str, - litellm_logging_obj: "LiteLLMLoggingObj", - litellm_params: Mapping[str, object], - extra_body: Mapping[str, object] | None = None, - embedding_executor: VectorStoreEmbeddingExecutor | None = None, - ) -> tuple[str, dict[str, object]]: # mutable-ok: the provider contract returns a writable JSON request body - params, k = self._params(litellm_params, vector_store_search_optional_params, extra_body) - embedding: Final = self.query_embedding_executor(embedding_executor, None).embed( - params.require_embedding_model(), self.query_text(query), self.query_embedding_configuration(litellm_params) - ) - return self._search_request(vector_store_id, embedding, k, params, api_base) - - async def atransform_search_vector_store_request( - self, - vector_store_id: str, - query: str | Sequence[str], - vector_store_search_optional_params: VectorStoreSearchOptionalRequestParams, - api_base: str, - litellm_logging_obj: "LiteLLMLoggingObj", - litellm_params: Mapping[str, object], - extra_body: Mapping[str, object] | None = None, - embedding_executor: VectorStoreEmbeddingExecutor | None = None, - ) -> tuple[str, dict[str, object]]: # mutable-ok: the provider contract returns a writable JSON request body - params, k = self._params(litellm_params, vector_store_search_optional_params, extra_body) - embedding: Final = await self.query_embedding_executor(embedding_executor, None).aembed( - params.require_embedding_model(), self.query_text(query), self.query_embedding_configuration(litellm_params) - ) - return self._search_request(vector_store_id, embedding, k, params, api_base) - - def transform_search_vector_store_response( - self, response: httpx.Response, litellm_logging_obj: "LiteLLMLoggingObj" - ) -> VectorStoreSearchResponse: - if not response.is_success: - raise BaseLLMException(status_code=response.status_code, message=response.text, headers=response.headers) - try: - hits: Final = _SearchResponse.model_validate_json(response.content).hits.hits - except ValidationError: - raise BaseLLMException( - status_code=502, - message="OpenSearch answered the search without a hits.hits list.", - headers=response.headers, - ) from None - logged: Final = _LoggedSearch.model_validate(litellm_logging_obj.model_call_details) - return VectorStoreSearchResponse( - object="vector_store.search_results.page", - search_query=logged.search_query, - data=[_search_result(hit, logged.litellm_params) for hit in hits], - ) - - def validate_create_vector_store(self) -> NoReturn: - raise _bad_request(_SEARCH_ONLY_MESSAGE) - - def transform_create_vector_store_request( - self, vector_store_create_optional_params: VectorStoreCreateOptionalRequestParams, api_base: str - ) -> NoReturn: - raise _bad_request(_SEARCH_ONLY_MESSAGE) - - def transform_create_vector_store_response(self, response: httpx.Response) -> NoReturn: - raise _bad_request(_SEARCH_ONLY_MESSAGE) diff --git a/litellm/types/utils.py b/litellm/types/utils.py index 9e8d4a218bb..5939a350970 100644 --- a/litellm/types/utils.py +++ b/litellm/types/utils.py @@ -4507,7 +4507,6 @@ class LlmProviders(str, Enum): S3_VECTORS = "s3_vectors" VALKEY = "valkey" MONGODB = "mongodb" - OPENSEARCH = "opensearch" HELICONE = "helicone" HYPERBOLIC = "hyperbolic" RECRAFT = "recraft" diff --git a/litellm/utils.py b/litellm/utils.py index ff160723f68..379eddb3155 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -10189,22 +10189,6 @@ class ProviderConfigManager: return BedrockVectorStore.get_initialized_custom_logger() return None - @staticmethod - def _get_vertex_ai_vector_stores_config(api_type: str | None) -> BaseVectorStoreConfig | None: - if api_type == "rag_api" or api_type is None: - from litellm.llms.vertex_ai.vector_stores.rag_api.transformation import ( - VertexVectorStoreConfig, - ) - - return VertexVectorStoreConfig() - if api_type == "search_api": - from litellm.llms.vertex_ai.vector_stores.search_api.transformation import ( - VertexSearchAPIVectorStoreConfig, - ) - - return VertexSearchAPIVectorStoreConfig() - return None - @staticmethod def get_provider_vector_stores_config( provider: LlmProviders, @@ -10226,7 +10210,18 @@ class ProviderConfigManager: return AzureOpenAIVectorStoreConfig() elif litellm.LlmProviders.VERTEX_AI == provider: - return ProviderConfigManager._get_vertex_ai_vector_stores_config(api_type) + if api_type == "rag_api" or api_type is None: # default to rag_api + from litellm.llms.vertex_ai.vector_stores.rag_api.transformation import ( + VertexVectorStoreConfig, + ) + + return VertexVectorStoreConfig() + elif api_type == "search_api": + from litellm.llms.vertex_ai.vector_stores.search_api.transformation import ( + VertexSearchAPIVectorStoreConfig, + ) + + return VertexSearchAPIVectorStoreConfig() elif litellm.LlmProviders.BEDROCK == provider: from litellm.llms.bedrock.vector_stores.transformation import ( BedrockVectorStoreConfig, @@ -10281,12 +10276,6 @@ class ProviderConfigManager: ) return MongoDBVectorStoreConfig() - elif litellm.LlmProviders.OPENSEARCH == provider: - from litellm.llms.opensearch.vector_stores.transformation import ( - OpenSearchVectorStoreConfig, - ) - - return OpenSearchVectorStoreConfig() return None @staticmethod diff --git a/provider_endpoints_support.json b/provider_endpoints_support.json index 09714228549..d65da3f41fd 100644 --- a/provider_endpoints_support.json +++ b/provider_endpoints_support.json @@ -3098,13 +3098,6 @@ "vector_stores_search": true } }, - "opensearch": { - "display_name": "Amazon OpenSearch (`opensearch`)", - "url": "https://docs.litellm.ai/docs/providers/opensearch_vector_stores", - "endpoints": { - "vector_stores_search": true - } - }, "helicone": { "display_name": "Helicone (`helicone`)", "url": "https://docs.litellm.ai/docs/providers/helicone", From 313faf337826e6f2513cdb40481259b8f4a96677 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Tue, 20 Oct 2026 19:45:00 -0700 Subject: [PATCH 5/6] ci(tests): run the OpenSearch vector store unit tests --- .github/workflows/test-unit.yml | 1 + tests/unit/AGENTS.md | 3 +++ 2 files changed, 4 insertions(+) diff --git a/.github/workflows/test-unit.yml b/.github/workflows/test-unit.yml index c593acbe8fb..a29a4995a86 100644 --- a/.github/workflows/test-unit.yml +++ b/.github/workflows/test-unit.yml @@ -316,6 +316,7 @@ jobs: tests/unit/llms/openai_like tests/unit/llms/opencode tests/unit/llms/opensandbox + tests/unit/llms/opensearch tests/unit/llms/openrouter tests/unit/llms/ovhcloud tests/unit/llms/parallel_ai diff --git a/tests/unit/AGENTS.md b/tests/unit/AGENTS.md index 9ce6c58cff7..872ca567447 100644 --- a/tests/unit/AGENTS.md +++ b/tests/unit/AGENTS.md @@ -35,6 +35,9 @@ Green if `send_batched` drops every row. pydantic doubles in 12 of 203 files, fa `tests/unit/` mirrors `litellm/`, so a changed file selects its tests by path, not a mapping file. New unit tests go here +A new directory under `tests/unit` runs in CI only once a shard in `.github/workflows/test-unit.yml` names it. +`pytest tests/unit/test_assert_ci_coverage.py` checks that, so run it before you push + ## Writing it so a human can read it A class only when tests share an arrange From a780a0df76774d78dd35947b5747df1a1e89be14 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Tue, 20 Oct 2026 23:41:27 -0700 Subject: [PATCH 6/6] fix(vector_stores): sign on.aws and FIPS OpenSearch hosts and reject missing AWS credentials before embedding --- .../vector_stores/transformation.py | 78 ++++++++++++++++--- .../test_opensearch_transformation.py | 63 +++++++++++++++ .../VectorStoreForm.integration.test.tsx | 17 ++-- 3 files changed, 139 insertions(+), 19 deletions(-) diff --git a/litellm/llms/opensearch/vector_stores/transformation.py b/litellm/llms/opensearch/vector_stores/transformation.py index bd3bc90b384..904e77a730b 100644 --- a/litellm/llms/opensearch/vector_stores/transformation.py +++ b/litellm/llms/opensearch/vector_stores/transformation.py @@ -9,7 +9,8 @@ from urllib.parse import quote, urlsplit import httpx from pydantic import ConfigDict, Field, TypeAdapter, ValidationError -from litellm.exceptions import BadRequestError +from litellm.exceptions import AuthenticationError, BadRequestError +from litellm.litellm_core_utils.optional_imports import ensure_optional_import from litellm.llms.base_llm.chat.transformation import BaseLLMException from litellm.llms.base_llm.vector_store.transformation import ( BaseQueryEmbeddingVectorStoreConfig, @@ -31,6 +32,8 @@ from litellm.types.vector_stores import ( ) if TYPE_CHECKING: + from botocore.credentials import Credentials + from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj DEFAULT_EMBEDDING_FIELD: Final = "embedding" @@ -38,23 +41,49 @@ DEFAULT_TEXT_FIELD: Final = "text" DEFAULT_MAX_NUM_RESULTS: Final = 10 MAX_MAX_NUM_RESULTS: Final = 50 _PARAM_PREFIX: Final = "opensearch_" -_AWS_HOST: Final = re.compile(r"\.(?P[a-z0-9-]+)\.(?Paoss|es)\.amazonaws\.com(?:\.cn)?\Z") +_AWS_HOSTS: Final = ( + re.compile(r"\.(?P[a-z0-9-]+)\.(?Paoss|aoss-fips|es)\.amazonaws\.com(?:\.cn)?\Z"), + re.compile(r"\.(?Paoss|aos)\.(?P[a-z0-9-]+)\.on\.aws\Z"), +) _QUERY_VECTOR: Final = TypeAdapter(tuple[float, ...]) _SEARCH_ONLY_MESSAGE: Final = ( "The OpenSearch vector store is search-only. Create the k-NN index in OpenSearch, " "then register it here with the index name as vector_store_id." ) +_SIGV4_ONLY_MESSAGE: Final = ( + "The OpenSearch vector store signs every request with AWS SigV4 and does not support api_key. " + "Remove api_key and give the vector store AWS credentials instead." +) +_CREDENTIALS_HINT: Final = ( + "Set aws_access_key_id and aws_secret_access_key, aws_role_name, or aws_profile_name on the vector store, " + "or run LiteLLM with an AWS identity." +) def _bad_request(message: str) -> BadRequestError: return BadRequestError(message=message, model=None, llm_provider="opensearch") +def _unauthenticated(reason: str) -> AuthenticationError: + return AuthenticationError( + message=f"No usable AWS credentials to sign the OpenSearch request ({reason}). {_CREDENTIALS_HINT}", + llm_provider="opensearch", + model=None, + ) + + +def _usable_credentials(credentials: object) -> "Credentials": + from botocore.credentials import Credentials + + if isinstance(credentials, Credentials): + return credentials + raise _unauthenticated("none were found") + + class _OpenSearchParams(LiteLLMBaseModel): model_config = ConfigDict(frozen=True, extra="ignore") litellm_embedding_model: str | None = None - aws_region_name: str | None = None opensearch_embedding_field: str | None = None opensearch_text_field: str | None = None @@ -75,6 +104,12 @@ class _OpenSearchParams(LiteLLMBaseModel): return self.litellm_embedding_model +class _SigningRegion(LiteLLMBaseModel): + model_config = ConfigDict(frozen=True, extra="ignore") + + aws_region_name: str | None = None + + class _SearchOptions(LiteLLMBaseModel): model_config = ConfigDict(frozen=True, extra="ignore") @@ -138,10 +173,11 @@ class _SearchResponse(LiteLLMBaseModel): def aws_signing_target(url: str) -> tuple[Literal["aoss", "es"], str | None]: - match: Final = _AWS_HOST.search(urlsplit(url).hostname or "") + hostname: Final = urlsplit(url).hostname or "" + match: Final = next(filter(None, (pattern.search(hostname) for pattern in _AWS_HOSTS)), None) if match is None: return "es", None - return ("aoss" if match["service"] == "aoss" else "es"), match["region"] + return ("aoss" if match["service"].startswith("aoss") else "es"), match["region"] def _search_result(hit: _Hit, params: _OpenSearchParams) -> VectorStoreSearchResult: @@ -171,8 +207,30 @@ class OpenSearchVectorStoreConfig(BaseQueryEmbeddingVectorStoreConfig, SignsRequ def validate_environment( self, headers: Mapping[str, object], litellm_params: GenericLiteLLMParams | None ) -> dict[str, object]: # mutable-ok: the shared HTTP handler requires writable headers + params: Final = dict(litellm_params or {}) + self._signing_credentials(params, self._signing_region(params, str(params.get("api_base") or ""))) return {**headers, "Content-Type": "application/json"} + def _signing_region(self, params: Mapping[str, object], api_base: str) -> str: + configured_region: Final = _SigningRegion.model_validate(params).aws_region_name + return self._aws.get_aws_region_name_for_non_llm_api_calls(configured_region or aws_signing_target(api_base)[1]) + + def _signing_credentials(self, params: Mapping[str, object], region: str) -> "Credentials": + if params.get("api_key"): + raise _bad_request(_SIGV4_ONLY_MESSAGE) + ensure_optional_import("botocore") + from botocore.exceptions import ( + CredentialRetrievalError, + NoCredentialsError, + PartialCredentialsError, + ProfileNotFound, + ) + + try: + return _usable_credentials(self._aws.resolve_credentials(AwsAuthParams.model_validate(params), region)) + except (CredentialRetrievalError, NoCredentialsError, PartialCredentialsError, ProfileNotFound) as error: + raise _unauthenticated(str(error)) from None + def get_complete_url(self, api_base: str | None, litellm_params: Mapping[str, object]) -> str: parsed: Final = urlsplit(api_base or "") if parsed.scheme not in ("http", "https") or not parsed.hostname: @@ -274,15 +332,11 @@ class OpenSearchVectorStoreConfig(BaseQueryEmbeddingVectorStoreConfig, SignsRequ api_base: str, api_key: str | None = None, ) -> tuple[dict[str, str], bytes]: # mutable-ok: the shared HTTP handler requires writable headers - service, host_region = aws_signing_target(api_base) - configured_region: Final = _OpenSearchParams.model_validate(optional_params).aws_region_name - region: Final = self._aws.get_aws_region_name_for_non_llm_api_calls(configured_region or host_region) + region: Final = self._signing_region(optional_params, api_base) body: Final = json.dumps(request_data) signed: Final = sign_aws_json_post( - get_credentials=partial( - self._aws.resolve_credentials, AwsAuthParams.model_validate(optional_params), region - ), - service_name=service, + get_credentials=partial(self._signing_credentials, optional_params, region), + service_name=aws_signing_target(api_base)[0], aws_region_name=region, url=api_base, body=body, diff --git a/tests/unit/llms/opensearch/vector_stores/test_opensearch_transformation.py b/tests/unit/llms/opensearch/vector_stores/test_opensearch_transformation.py index 4b4339dbe39..4ffffba862d 100644 --- a/tests/unit/llms/opensearch/vector_stores/test_opensearch_transformation.py +++ b/tests/unit/llms/opensearch/vector_stores/test_opensearch_transformation.py @@ -3,6 +3,7 @@ import json import re from collections.abc import Callable, Mapping from datetime import datetime, timezone +from pathlib import Path from typing import Final from unittest.mock import MagicMock @@ -146,6 +147,9 @@ async def _search( (SERVERLESS, {"aws_region_name": "us-west-2"}, "aoss", "us-west-2"), ("https://search-docs-xyz.eu-west-1.es.amazonaws.com/", {}, "es", "eu-west-1"), ("https://vpc-docs-xyz.cn-north-1.es.amazonaws.com.cn", {}, "es", "cn-north-1"), + ("https://abc123.us-gov-west-1.aoss-fips.amazonaws.com", {}, "aoss", "us-gov-west-1"), + ("https://abc123.aoss.eu-central-1.on.aws", {}, "aoss", "eu-central-1"), + ("https://search-docs-xyz.aos.ap-southeast-2.on.aws", {}, "es", "ap-southeast-2"), ("https://search.docs.example", {"aws_region_name": "ap-south-1"}, "es", "ap-south-1"), ], ) @@ -234,6 +238,7 @@ async def test_opensearch_error_keeps_its_status(asynchronous: bool, status: int ({"extra_body": {"min_score": 0.5}}, "extra_body"), ({"api_base": None}, "api_base"), ({"api_base": "abc123.us-east-1.aoss.amazonaws.com"}, "api_base"), + ({"api_key": "opensearch-basic-auth"}, "api_key"), ], ) @pytest.mark.asyncio @@ -251,6 +256,64 @@ async def test_unsupported_search_is_rejected_before_embedding_or_any_request( executor.call.assert_not_called() +_AMBIENT_AWS_ENV: Final = ( + "AWS_ACCESS_KEY_ID", + "AWS_SECRET_ACCESS_KEY", + "AWS_SESSION_TOKEN", + "AWS_PROFILE", + "AWS_PROFILE_NAME", + "AWS_ROLE_NAME", + "AWS_ROLE_ARN", + "AWS_SESSION_NAME", + "AWS_WEB_IDENTITY_TOKEN", + "AWS_WEB_IDENTITY_TOKEN_FILE", + "AWS_CONTAINER_CREDENTIALS_RELATIVE_URI", + "AWS_CONTAINER_CREDENTIALS_FULL_URI", +) + + +@pytest.mark.parametrize("asynchronous", [False, True]) +@pytest.mark.parametrize( + "credentials,env", + [ + pytest.param({"aws_session_name": "opensearch-no-credentials"}, {}, id="none-anywhere"), + pytest.param({"aws_profile_name": "opensearch-missing-profile"}, {}, id="missing-profile"), + pytest.param({}, {"AWS_ACCESS_KEY_ID": "AKIDPARTIAL"}, id="key-without-secret"), + ], +) +@pytest.mark.asyncio +async def test_missing_aws_credentials_are_a_401_before_embedding_or_any_request( + asynchronous: bool, + credentials: Mapping[str, str], + env: Mapping[str, str], + monkeypatch: pytest.MonkeyPatch, + tmp_path: Path, +) -> None: + for name in _AMBIENT_AWS_ENV: + monkeypatch.delenv(name, raising=False) + for name in ("AWS_CONFIG_FILE", "AWS_SHARED_CREDENTIALS_FILE", "BOTO_CONFIG"): + empty: Path = tmp_path / name + empty.write_text("") + monkeypatch.setenv(name, str(empty)) + monkeypatch.setenv("AWS_EC2_METADATA_DISABLED", "true") + for name, value in env.items(): + monkeypatch.setenv(name, value) + executor: Final = RecordingEmbeddingExecutor() + respond: Final = MagicMock(return_value=httpx.Response(200, json={"hits": {"hits": []}})) + + with pytest.raises(litellm.AuthenticationError, match="No usable AWS credentials") as error: + await _search( + asynchronous, + respond, + executor, + {"aws_access_key_id": None, "aws_secret_access_key": None, **credentials}, + ) + + assert error.value.status_code == 401 + respond.assert_not_called() + executor.call.assert_not_called() + + @pytest.mark.asyncio async def test_no_op_values_of_unsupported_params_are_accepted() -> None: def respond(request: httpx.Request) -> httpx.Response: diff --git a/ui/litellm-dashboard/src/app/(dashboard)/vector-stores/_components/VectorStoreForm.integration.test.tsx b/ui/litellm-dashboard/src/app/(dashboard)/vector-stores/_components/VectorStoreForm.integration.test.tsx index 4007aa481c8..42664f54c97 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/vector-stores/_components/VectorStoreForm.integration.test.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/vector-stores/_components/VectorStoreForm.integration.test.tsx @@ -1,4 +1,4 @@ -import { render, screen } from "@testing-library/react"; +import { fireEvent, render, screen } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { beforeEach, describe, expect, it, vi } from "vitest"; @@ -139,11 +139,12 @@ describe("VectorStoreForm submit payload", () => { renderForm(); await chooseProvider(user, "Amazon OpenSearch"); - await user.type(screen.getByPlaceholderText("my-knn-index (OpenSearch index name)"), "litellm-qa-docs"); - await user.type( - screen.getByPlaceholderText("https://abc123.us-east-1.aoss.amazonaws.com"), - "https://yzatq55tpsuvlwozcal4.us-east-1.aoss.amazonaws.com", - ); + fireEvent.change(screen.getByPlaceholderText("my-knn-index (OpenSearch index name)"), { + target: { value: "litellm-qa-docs" }, + }); + fireEvent.change(screen.getByPlaceholderText("https://abc123.us-east-1.aoss.amazonaws.com"), { + target: { value: "https://yzatq55tpsuvlwozcal4.us-east-1.aoss.amazonaws.com" }, + }); await chooseFromSelect(user, 1, "text-embedding-3-small"); await submit(user); @@ -166,7 +167,9 @@ describe("VectorStoreForm submit payload", () => { renderForm(); await chooseProvider(user, "Amazon OpenSearch"); - await user.type(screen.getByPlaceholderText("my-knn-index (OpenSearch index name)"), "litellm-qa-docs"); + fireEvent.change(screen.getByPlaceholderText("my-knn-index (OpenSearch index name)"), { + target: { value: "litellm-qa-docs" }, + }); await submit(user); expect(await screen.findByText("Please input the opensearch endpoint")).toBeInTheDocument();