mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-18 09:45:23 +00:00
fix(chatgpt): keep proxy-mapped session headers ahead of body session_id and ignore non-string cache keys
This commit is contained in:
parent
d82f2294c6
commit
11d7439da2
3 changed files with 42 additions and 17 deletions
|
|
@ -53,6 +53,23 @@ def _get_provider_affinity_header_name(litellm_params: object | None, default_he
|
|||
return validate_provider_affinity_header_name(header)
|
||||
|
||||
|
||||
def _metadata_values(litellm_params: object) -> tuple[object, ...]:
|
||||
return tuple(
|
||||
value for key in ("metadata", "litellm_metadata") if (value := _get_value(litellm_params, key)) is not None
|
||||
)
|
||||
|
||||
|
||||
def get_caller_litellm_session_id(litellm_params: object | None) -> str | None:
|
||||
if litellm_params is None:
|
||||
return None
|
||||
has_generated_session_id: Final = any(
|
||||
isinstance(metadata, Mapping) and metadata.get(SESSION_ID_GENERATED_METADATA_KEY)
|
||||
for metadata in _metadata_values(litellm_params)
|
||||
)
|
||||
litellm_session_id: Final = _get_value(litellm_params, "litellm_session_id")
|
||||
return str(litellm_session_id) if litellm_session_id and not has_generated_session_id else None
|
||||
|
||||
|
||||
def get_stable_session_id(litellm_params: object | None) -> str | None:
|
||||
if litellm_params is None:
|
||||
return None
|
||||
|
|
@ -61,19 +78,11 @@ def get_stable_session_id(litellm_params: object | None) -> str | None:
|
|||
if direct_session_id:
|
||||
return str(direct_session_id)
|
||||
|
||||
metadata_values: Final[tuple[object, ...]] = tuple(
|
||||
value for key in ("metadata", "litellm_metadata") if (value := _get_value(litellm_params, key)) is not None
|
||||
)
|
||||
has_generated_session_id: Final = any(
|
||||
isinstance(metadata, Mapping) and metadata.get(SESSION_ID_GENERATED_METADATA_KEY)
|
||||
for metadata in metadata_values
|
||||
)
|
||||
caller_session_id: Final = get_caller_litellm_session_id(litellm_params)
|
||||
if caller_session_id:
|
||||
return caller_session_id
|
||||
|
||||
litellm_session_id: Final = _get_value(litellm_params, "litellm_session_id")
|
||||
if litellm_session_id and not has_generated_session_id:
|
||||
return str(litellm_session_id)
|
||||
|
||||
for metadata in metadata_values:
|
||||
for metadata in _metadata_values(litellm_params):
|
||||
if (
|
||||
isinstance(metadata, Mapping)
|
||||
and not metadata.get(SESSION_ID_GENERATED_METADATA_KEY)
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ import httpx
|
|||
from pydantic import JsonValue, TypeAdapter
|
||||
|
||||
from litellm.litellm_core_utils.get_provider_specific_headers import ProviderSpecificHeaderUtils
|
||||
from litellm.litellm_core_utils.provider_affinity import get_stable_session_id
|
||||
from litellm.litellm_core_utils.provider_affinity import get_caller_litellm_session_id, get_stable_session_id
|
||||
from litellm.llms.base_llm.chat.transformation import BaseLLMException
|
||||
from litellm.types.utils import ProviderSpecificHeader
|
||||
|
||||
|
|
@ -398,16 +398,20 @@ def _normalize_litellm_params(litellm_params: object) -> Mapping[str, object]:
|
|||
|
||||
|
||||
def _prompt_cache_session_id(prompt_cache_key: object) -> str | None:
|
||||
if not prompt_cache_key:
|
||||
if not isinstance(prompt_cache_key, str) or not prompt_cache_key:
|
||||
return None
|
||||
key: Final = str(prompt_cache_key)
|
||||
return key if _safe_header_value(key) == key else hashlib.sha256(key.encode("utf-8", "surrogatepass")).hexdigest()
|
||||
return (
|
||||
prompt_cache_key
|
||||
if _safe_header_value(prompt_cache_key) == prompt_cache_key
|
||||
else hashlib.sha256(prompt_cache_key.encode("utf-8", "surrogatepass")).hexdigest()
|
||||
)
|
||||
|
||||
|
||||
def get_chatgpt_session_id(litellm_params: object) -> str | None:
|
||||
params: Final = _normalize_litellm_params(litellm_params)
|
||||
return (
|
||||
get_stable_session_id(params)
|
||||
get_caller_litellm_session_id(params)
|
||||
or get_stable_session_id(params)
|
||||
or _prompt_cache_session_id(params.get("prompt_cache_key"))
|
||||
or next(
|
||||
(
|
||||
|
|
|
|||
|
|
@ -116,6 +116,18 @@ class TestGetChatGPTSessionId:
|
|||
def test_prompt_cache_key_wins_over_request_scoped_ids(self, request_scoped):
|
||||
assert get_chatgpt_session_id({**request_scoped, "prompt_cache_key": "conv-123"}) == "conv-123"
|
||||
|
||||
def test_proxy_mapped_session_header_wins_over_body_session_id(self):
|
||||
assert (
|
||||
get_chatgpt_session_id(
|
||||
{"litellm_session_id": "header-session", "session_id": "body-session", "prompt_cache_key": "conv-123"}
|
||||
)
|
||||
== "header-session"
|
||||
)
|
||||
|
||||
@pytest.mark.parametrize("prompt_cache_key", [{"tenant": "a"}, 12345, True, ["x", "y"]])
|
||||
def test_non_string_prompt_cache_key_is_ignored(self, prompt_cache_key):
|
||||
assert get_chatgpt_session_id({"prompt_cache_key": prompt_cache_key, "litellm_call_id": "call-1"}) == "call-1"
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"first, second",
|
||||
[("a\nb", "a\tb"), (json.loads('"conv-\\ud800"'), json.loads('"conv-\\udc00"'))],
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue