mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-16 09:22:22 +00:00
fix(vector-stores): redact images nested in extra_body and keep a store's extra_body retrievalQuery out of searches
(cherry picked from commit c99fd3631b)
This commit is contained in:
parent
ca5a6e89d0
commit
6e7710c5ab
4 changed files with 58 additions and 3 deletions
|
|
@ -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]:
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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})
|
||||
|
||||
|
|
|
|||
|
|
@ -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"""
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue