mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-17 09:34:48 +00:00
fix(chatgpt): keep trace ids copied into litellm_session_id from outranking prompt_cache_key
This commit is contained in:
parent
5e0fbe14a3
commit
1896516989
3 changed files with 49 additions and 19 deletions
|
|
@ -59,9 +59,7 @@ def _metadata_values(litellm_params: object) -> tuple[object, ...]:
|
|||
)
|
||||
|
||||
|
||||
def get_caller_litellm_session_id(litellm_params: object | None) -> str | None:
|
||||
if litellm_params is None:
|
||||
return None
|
||||
def _non_generated_litellm_session_id(litellm_params: object) -> str | 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)
|
||||
|
|
@ -70,18 +68,7 @@ def get_caller_litellm_session_id(litellm_params: object | None) -> str | None:
|
|||
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
|
||||
|
||||
direct_session_id: Final = _get_value(litellm_params, "session_id")
|
||||
if direct_session_id:
|
||||
return str(direct_session_id)
|
||||
|
||||
caller_session_id: Final = get_caller_litellm_session_id(litellm_params)
|
||||
if caller_session_id:
|
||||
return caller_session_id
|
||||
|
||||
def _metadata_session_id(litellm_params: object) -> str | None:
|
||||
for metadata in _metadata_values(litellm_params):
|
||||
if (
|
||||
isinstance(metadata, Mapping)
|
||||
|
|
@ -92,6 +79,37 @@ def get_stable_session_id(litellm_params: object | None) -> str | None:
|
|||
return None
|
||||
|
||||
|
||||
def _metadata_field_equals(litellm_params: object, field: str, value: str) -> bool:
|
||||
return any(_get_value(metadata, field) == value for metadata in _metadata_values(litellm_params))
|
||||
|
||||
|
||||
def get_caller_litellm_session_id(litellm_params: object | None) -> str | None:
|
||||
if litellm_params is None:
|
||||
return None
|
||||
session_id: Final = _non_generated_litellm_session_id(litellm_params)
|
||||
copies_trace_id: Final = session_id is not None and (
|
||||
_metadata_field_equals(litellm_params, "trace_id", session_id)
|
||||
and not _metadata_field_equals(litellm_params, "session_id", session_id)
|
||||
)
|
||||
return None if copies_trace_id else session_id
|
||||
|
||||
|
||||
def get_requested_session_id(litellm_params: object | None) -> str | None:
|
||||
if litellm_params is None:
|
||||
return None
|
||||
direct_session_id: Final = _get_value(litellm_params, "session_id")
|
||||
return str(direct_session_id) if direct_session_id else _metadata_session_id(litellm_params)
|
||||
|
||||
|
||||
def get_stable_session_id(litellm_params: object | None) -> str | None:
|
||||
if litellm_params is None:
|
||||
return None
|
||||
direct_session_id: Final = _get_value(litellm_params, "session_id")
|
||||
if direct_session_id:
|
||||
return str(direct_session_id)
|
||||
return _non_generated_litellm_session_id(litellm_params) or _metadata_session_id(litellm_params)
|
||||
|
||||
|
||||
def add_provider_affinity_header(
|
||||
headers: Mapping[str, object], litellm_params: object | None, default_header: str | None = None
|
||||
) -> dict[str, object]: # mutable-ok: downstream handlers add auth and signing headers
|
||||
|
|
|
|||
|
|
@ -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_caller_litellm_session_id, get_stable_session_id
|
||||
from litellm.litellm_core_utils.provider_affinity import get_caller_litellm_session_id, get_requested_session_id
|
||||
from litellm.llms.base_llm.chat.transformation import BaseLLMException
|
||||
from litellm.types.utils import ProviderSpecificHeader
|
||||
|
||||
|
|
@ -411,7 +411,7 @@ def get_chatgpt_session_id(litellm_params: object) -> str | None:
|
|||
params: Final = _normalize_litellm_params(litellm_params)
|
||||
return (
|
||||
get_caller_litellm_session_id(params)
|
||||
or get_stable_session_id(params)
|
||||
or get_requested_session_id(params)
|
||||
or _prompt_cache_session_id(params.get("prompt_cache_key"))
|
||||
or next(
|
||||
(
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import h11
|
|||
import pytest
|
||||
|
||||
from litellm.constants import SESSION_ID_GENERATED_METADATA_KEY
|
||||
from litellm.litellm_core_utils.get_litellm_params import get_litellm_params
|
||||
from litellm.llms.chatgpt.common_utils import (
|
||||
extract_chatgpt_account_id,
|
||||
get_chatgpt_client_credential,
|
||||
|
|
@ -98,7 +99,11 @@ def test_other_authentication_schemes_are_not_chatgpt_logins():
|
|||
class TestGetChatGPTSessionId:
|
||||
@pytest.mark.parametrize(
|
||||
"explicit",
|
||||
[{"litellm_session_id": "chosen"}, {"session_id": "chosen"}, {"metadata": {"session_id": "chosen"}}],
|
||||
[
|
||||
{"litellm_session_id": "chosen"},
|
||||
{"session_id": "chosen"},
|
||||
{"metadata": {"session_id": "chosen"}},
|
||||
],
|
||||
)
|
||||
def test_explicit_session_id_wins_over_prompt_cache_key(self, explicit: Mapping[str, object]) -> None:
|
||||
assert get_chatgpt_session_id({**explicit, "prompt_cache_key": "conv-123"}) == "chosen"
|
||||
|
|
@ -108,6 +113,8 @@ class TestGetChatGPTSessionId:
|
|||
[
|
||||
{"litellm_trace_id": "trace-1"},
|
||||
{"litellm_call_id": "call-1"},
|
||||
get_litellm_params(metadata={"trace_id": "trace-1"}),
|
||||
{"litellm_session_id": "trace-1", "litellm_metadata": {"trace_id": "trace-1"}},
|
||||
{"litellm_session_id": "gen-1", "metadata": {"session_id": "gen-1", SESSION_ID_GENERATED_METADATA_KEY: True}},
|
||||
{
|
||||
"litellm_session_id": "gen-1",
|
||||
|
|
@ -121,7 +128,12 @@ class TestGetChatGPTSessionId:
|
|||
def test_proxy_mapped_session_header_wins_over_body_session_id(self) -> None:
|
||||
assert (
|
||||
get_chatgpt_session_id(
|
||||
{"litellm_session_id": "header-session", "session_id": "body-session", "prompt_cache_key": "conv-123"}
|
||||
{
|
||||
"litellm_session_id": "header-session",
|
||||
"metadata": {"trace_id": "header-session", "session_id": "header-session"},
|
||||
"session_id": "body-session",
|
||||
"prompt_cache_key": "conv-123",
|
||||
}
|
||||
)
|
||||
== "header-session"
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue