fix(proxy): harden spend log session precedence per review

This commit is contained in:
Deepanshu Pal 2026-09-13 17:37:54 +05:30
parent c07e9432ed
commit 07fd7ee607
2 changed files with 5 additions and 11 deletions

View file

@ -682,12 +682,8 @@ def _get_session_id_for_spend_log(
omit_when_missing: bool,
batch_trace_session_id: str | None = None,
) -> str | None:
"""Under `omit` only `metadata.session_id`, the key Langfuse reads, counts as a session. Batch call types
carry a deterministic session derived from the batch id, which outranks everything else because the
per-request trace ids differ between the create call and the cost poller's row. Otherwise the caller's
`litellm_session_id` wins: it is only absent or a copy of the trace id when the caller did not send one,
so preferring it changes nothing for sessionless requests while letting a real session id reach the
spend log instead of being overwritten by the per-call trace id."""
"""Resolve the session id for the spend log row: `omit` honors only metadata.session_id,
batch sessions outrank everything, then the caller's litellm_session_id, then trace ids."""
if omit_when_missing:
session_id: Final = metadata.get("session_id") if metadata else None
return str(session_id) if session_id else None
@ -697,7 +693,7 @@ def _get_session_id_for_spend_log(
if batch_trace_session_id is not None:
return batch_trace_session_id
caller_session_id: Final = kwargs.get("litellm_session_id")
if caller_session_id is not None:
if caller_session_id:
return str(caller_session_id)
if standard_logging_payload is not None and standard_logging_payload.get("trace_id") is not None:
return str(standard_logging_payload.get("trace_id"))

View file

@ -234,8 +234,7 @@ def test_batch_session_outranks_the_per_request_trace_id():
def test_caller_litellm_session_id_wins_over_the_per_request_trace_id():
"""A caller-supplied litellm_session_id is a real session, not a copied trace id; the
spend log must keep it instead of overwriting it with the per-call trace id."""
"""Caller litellm_session_id wins over the per-call trace id."""
session_id: Final = _get_session_id_for_spend_log(
kwargs={"litellm_session_id": "sess-1", "litellm_trace_id": "trace-abc"},
metadata={"trace_id": "trace-abc"},
@ -246,8 +245,7 @@ def test_caller_litellm_session_id_wins_over_the_per_request_trace_id():
def test_batch_session_outranks_a_caller_litellm_session_id():
"""The batch lifecycle grouping must survive even when the create call carries a
caller session id, or the poller's cost row would land in a different session."""
"""Batch session still outranks a caller session id."""
session_id: Final = _get_session_id_for_spend_log(
kwargs={"litellm_session_id": "sess-1", "litellm_trace_id": "trace-abc"},
metadata={"trace_id": "trace-abc"},