Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(proxy): ignore non-span parent_otel_span when deriving litellm_trace_id

yucheng49417d4fa276
diff --git a/litellm/proxy/litellm_pre_call_utils.py b/litellm/proxy/litellm_pre_call_utils.py
index 9580cd8bffe..fc81f07c7f2 100644
--- a/litellm/proxy/litellm_pre_call_utils.py
+++ b/litellm/proxy/litellm_pre_call_utils.py
@@ -112,9 +112,10 @@ def _trace_id_from_otel_span(span: "OtelSpan | None") -> str | None:
if span is None:
return None
span_context: Final = span.get_span_context()
- if not span_context.is_valid:
+ trace_id: Final = span_context.trace_id
+ if not span_context.is_valid or not isinstance(trace_id, int):
return None
- return format(span_context.trace_id, "032x")
+ return format(trace_id, "032x")
def add_otel_trace_id_to_request(
diff --git a/tests/test_litellm/proxy/test_litellm_pre_call_utils.py b/tests/test_litellm/proxy/test_litellm_pre_call_utils.py
index 5dc2ef1fc2b..81ec555b59c 100644
--- a/tests/test_litellm/proxy/test_litellm_pre_call_utils.py
+++ b/tests/test_litellm/proxy/test_litellm_pre_call_utils.py
@@ -3606,11 +3606,12 @@ async def test_add_litellm_data_to_request_otel_span_does_not_override_caller_tr
@pytest.mark.asyncio
[email protected]("parent_otel_span", [None, "invalid_span"])
[email protected]("parent_otel_span", [None, "invalid_span", "not_a_span"])
async def test_add_litellm_data_to_request_no_trace_id_without_valid_otel_span(parent_otel_span):
- """No OTel span (OTel off) or a span with an invalid context must leave
- litellm_trace_id unset so downstream keeps generating its own id."""
- span = INVALID_SPAN if parent_otel_span == "invalid_span" else None
+ """No OTel span (OTel off), a span with an invalid context, or an object
+ that only quacks like a span (auth is typed loosely and often stubbed) must
+ leave litellm_trace_id unset so downstream keeps generating its own id."""
+ span = {"invalid_span": INVALID_SPAN, "not_a_span": MagicMock()}.get(parent_otel_span)
data = await add_litellm_data_to_request(
data={"model": "gpt-5.6"},
request=_request_mock_without_trace_headers(),