From 141547771a6fb83fe0da44d040490a3f1f14d392 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Wed, 28 Oct 2026 03:22:22 -0700 Subject: [PATCH] fix(chatgpt): hash cache keys with leading or trailing whitespace so the session header stays valid --- litellm/llms/chatgpt/common_utils.py | 2 +- tests/unit/llms/chatgpt/test_chatgpt_common_utils.py | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/litellm/llms/chatgpt/common_utils.py b/litellm/llms/chatgpt/common_utils.py index 45cdf9cf387..4999fbd2133 100644 --- a/litellm/llms/chatgpt/common_utils.py +++ b/litellm/llms/chatgpt/common_utils.py @@ -402,7 +402,7 @@ def _prompt_cache_session_id(prompt_cache_key: object) -> str | None: return None return ( prompt_cache_key - if _safe_header_value(prompt_cache_key) == prompt_cache_key + if _safe_header_value(prompt_cache_key.strip()) == prompt_cache_key else hashlib.sha256(prompt_cache_key.encode("utf-8", "surrogatepass")).hexdigest() ) diff --git a/tests/unit/llms/chatgpt/test_chatgpt_common_utils.py b/tests/unit/llms/chatgpt/test_chatgpt_common_utils.py index 0ab71dd48b7..8efdbe2df2c 100644 --- a/tests/unit/llms/chatgpt/test_chatgpt_common_utils.py +++ b/tests/unit/llms/chatgpt/test_chatgpt_common_utils.py @@ -2,6 +2,7 @@ import base64 import json from typing import Final +import h11 import pytest from litellm.constants import SESSION_ID_GENERATED_METADATA_KEY @@ -130,7 +131,11 @@ class TestGetChatGPTSessionId: @pytest.mark.parametrize( "first, second", - [("a\nb", "a\tb"), (json.loads('"conv-\\ud800"'), json.loads('"conv-\\udc00"'))], + [ + ("a\nb", "a\tb"), + (json.loads('"conv-\\ud800"'), json.loads('"conv-\\udc00"')), + ("conv-123 ", " conv-123"), + ], ) def test_header_unsafe_cache_keys_map_to_distinct_stable_header_values(self, first, second): first_id: Final = get_chatgpt_session_id({"prompt_cache_key": first}) @@ -139,4 +144,5 @@ class TestGetChatGPTSessionId: assert first_id is not None and second_id is not None assert first_id == get_chatgpt_session_id({"prompt_cache_key": first}) assert first_id != second_id - assert all(value.isascii() and value.isprintable() for value in (first_id, second_id)) + for value in (first_id, second_id): + h11.Request(method="POST", target="/", headers=[("host", "chatgpt.com"), ("session_id", value)])