From 6e7710c5abe222afbfd012fb5a58f30fda62d23b Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Tue, 10 Nov 2026 18:56:11 -0800 Subject: [PATCH] fix(vector-stores): redact images nested in extra_body and keep a store's extra_body retrievalQuery out of searches (cherry picked from commit c99fd3631b256caf20bdc5eab530226d02be0b80) --- litellm/litellm_core_utils/redact_messages.py | 12 ++++++++-- litellm/vector_stores/search_query.py | 12 +++++++++- .../test_redact_messages.py | 24 +++++++++++++++++++ .../test_vector_store_endpoints.py | 13 ++++++++++ 4 files changed, 58 insertions(+), 3 deletions(-) diff --git a/litellm/litellm_core_utils/redact_messages.py b/litellm/litellm_core_utils/redact_messages.py index 6a93335b5a3..4ee52f4d076 100644 --- a/litellm/litellm_core_utils/redact_messages.py +++ b/litellm/litellm_core_utils/redact_messages.py @@ -250,11 +250,19 @@ def _redact_responses_api_output_dict(output_items, redacted_str: str): output_item["input"] = redacted_str -_REDACTED_REQUEST_FIELDS: Final = ("query_image",) +_REDACTED_REQUEST_FIELDS: Final = ("query_image", "retrievalQuery") +_NESTED_REQUEST_FIELDS: Final = ("extra_body", "additional_args", "complete_input_dict") def _request_field_redactions(params: Mapping[str, object]) -> Mapping[str, object]: - return {field: REDACTED_BY_LITELLM for field in _REDACTED_REQUEST_FIELDS if params.get(field) is not None} + return { + **{field: REDACTED_BY_LITELLM for field in _REDACTED_REQUEST_FIELDS if params.get(field) is not None}, + **{ + field: _with_redacted_request_fields(nested) + for field in _NESTED_REQUEST_FIELDS + if (nested := _as_mapping(params.get(field))) is not None + }, + } def _with_redacted_request_fields(params: Mapping[str, object]) -> Mapping[str, object]: diff --git a/litellm/vector_stores/search_query.py b/litellm/vector_stores/search_query.py index b1e5b975a6d..e514030bf2e 100644 --- a/litellm/vector_stores/search_query.py +++ b/litellm/vector_stores/search_query.py @@ -104,8 +104,18 @@ def sent_query_image(query_image: object, extra_body: object) -> object: return None if body is None else body.get("query_image") +def _without_retrieval_query(params: Mapping[str, object]) -> Mapping[str, object]: + return {key: value for key, value in params.items() if key not in RETRIEVAL_QUERY_PARAMS} + + def store_search_params(litellm_params: Mapping[str, object]) -> Mapping[str, object]: - return MappingProxyType({key: value for key, value in litellm_params.items() if key not in RETRIEVAL_QUERY_PARAMS}) + extra_body: Final = as_str_mapping(litellm_params.get("extra_body")) + return MappingProxyType( + { + **_without_retrieval_query(litellm_params), + **({} if extra_body is None else {"extra_body": _without_retrieval_query(extra_body)}), + } + ) def resolve_search_query( diff --git a/tests/unit/litellm_core_utils/test_redact_messages.py b/tests/unit/litellm_core_utils/test_redact_messages.py index aecd9e7f121..a9b62b7a126 100644 --- a/tests/unit/litellm_core_utils/test_redact_messages.py +++ b/tests/unit/litellm_core_utils/test_redact_messages.py @@ -1524,6 +1524,30 @@ def test_redaction_removes_a_vector_store_query_image_from_every_logged_copy() - assert SECRET not in json.dumps(details, default=str) +def test_redaction_removes_a_query_image_sent_inside_extra_body() -> None: + image: Final = {"type": "base64", "media_type": "image/jpeg", "data": SECRET} + details: Final[dict[str, object]] = { + "optional_params": {"extra_body": {"query_image": image}}, + "litellm_params": {"proxy_server_request": {"body": {"extra_body": {"query_image": image}}}}, + } + + perform_redaction(details, result=None) + + assert SECRET not in json.dumps(details, default=str) + + +def test_redaction_removes_the_image_from_the_provider_request_copy() -> None: + retrieval_query: Final = {"type": "IMAGE", "image": {"format": "jpeg", "inlineContent": SECRET}} + details: Final[dict[str, object]] = { + "additional_args": {"complete_input_dict": {"retrievalQuery": retrieval_query, "knowledgeBaseId": "kb"}}, + } + + perform_redaction(details, result=None) + + assert SECRET not in json.dumps(details, default=str) + assert details["additional_args"]["complete_input_dict"]["knowledgeBaseId"] == "kb" + + def test_a_callback_opting_out_gets_a_query_image_redacted_outside_the_spend_log_body() -> None: details: Final = _vector_store_search_details({"type": "base64", "media_type": "image/jpeg", "data": SECRET}) diff --git a/tests/unit/proxy/vector_store_endpoints/test_vector_store_endpoints.py b/tests/unit/proxy/vector_store_endpoints/test_vector_store_endpoints.py index 8ff84b512e0..c9fa293317b 100644 --- a/tests/unit/proxy/vector_store_endpoints/test_vector_store_endpoints.py +++ b/tests/unit/proxy/vector_store_endpoints/test_vector_store_endpoints.py @@ -3121,6 +3121,19 @@ def test_a_stored_raw_retrieval_query_never_reaches_the_search_request(retrieval assert request_data == {"custom_llm_provider": "bedrock", "aws_region_name": "us-west-2"} +@pytest.mark.parametrize("retrieval_query_param", ["retrievalQuery", "retrieval_query"]) +def test_a_raw_retrieval_query_in_a_stores_extra_body_never_reaches_the_search_request(retrieval_query_param: str): + request_data: Final = build_request_data_from_managed_vector_store( + { + "vector_store_id": "kb", + "custom_llm_provider": "bedrock", + "litellm_params": {"extra_body": {retrieval_query_param: {"text": "from the store"}, "kept": "value"}}, + } + ) + + assert request_data == {"custom_llm_provider": "bedrock", "extra_body": {"kept": "value"}} + + class TestConfigOwnedVectorStores: """Stores declared under ``vector_store_registry`` in config.yaml are owned by the config file"""