From a90683149771f14429a76f89b76f3d565d6660b6 Mon Sep 17 00:00:00 2001 From: Aikins Laryea Date: Sat, 21 Nov 2026 08:28:55 +0000 Subject: [PATCH] fix(sdk-litellm): keep the router's deployment config out of span metadata LiteLLM's Router writes the deployment it picked into the caller's own metadata argument. On calls made outside the LiteLLM proxy, every key of that argument was exported, so spans carried td.metadata.api_base, whose URL can hold credentials, and td.metadata.model_info, which holds any fields an admin configured for the deployment. LiteLLM 1.110 also writes kubernetes_pod_routing, with the internal service host and pod IP of a pod-discovered deployment. These three keys are now dropped on every call, as the proxy path already did for api_base and model_info. --- sdks/python-litellm/README.md | 5 +- .../src/telemetry_dev_litellm/__init__.py | 8 +++- sdks/python-litellm/tests/test_litellm.py | 46 ++++++++++++++++--- 3 files changed, 49 insertions(+), 10 deletions(-) diff --git a/sdks/python-litellm/README.md b/sdks/python-litellm/README.md index 2aceac8..0e07282 100644 --- a/sdks/python-litellm/README.md +++ b/sdks/python-litellm/README.md @@ -128,7 +128,7 @@ Aggregate usage keys are `input_tokens`, `output_tokens`, `total_tokens`, `cache ## Metadata -On a LiteLLM or Router call made outside the LiteLLM proxy, each key of the call's `litellm_metadata` argument, or of `metadata` when `litellm_metadata` is absent or empty, is exported as `td.metadata.`, except the keys listed below. That includes keys LiteLLM's Router adds to the argument, such as `model_group`, `deployment`, `model_info` and `api_base`. Rerank spans also add `result_count` and `search_units`. +On a LiteLLM or Router call made outside the LiteLLM proxy, each key of the call's `litellm_metadata` argument, or of `metadata` when `litellm_metadata` is absent or empty, is exported as `td.metadata.`, except the keys listed below. That includes keys LiteLLM's Router adds to the argument, such as `model_group` and `deployment`, and the Router's additions to `tags`: the picked deployment's own tags and, for a deployment that uses a stored credential, `Credential: `. Rerank spans also add `result_count` and `search_units`. Strings, numbers, booleans and non-empty sequences of only strings, only booleans or only numbers are exported as they are; other values, including empty sequences, are JSON-encoded. When `max_attribute_length` is set, each string, including each string in a sequence, and each JSON value is truncated to it; by default they are exported whole, or cut at 65,536 characters with `telemetry-dev` 0.3.2 or earlier. Metadata values are not passed to the `mask` callback, and metadata is exported whatever `capture_input` and `capture_output` are set to. @@ -136,6 +136,7 @@ These top-level keys are always dropped: - `headers`, `requester_metadata`, `requester_ip_address`, `user_agent`, `endpoint`, `user_api_end_user_max_budget`, and every key that starts with `user_api_key`. LiteLLM's proxy stores the incoming request's headers, client IP address, user agent and URL under these names, along with the authenticated key, user, team and end user, including the user's email and JWT claims. - `previous_models`, where the Router keeps copies of the arguments and metadata of earlier failed calls on a retry or fallback. +- `model_info`, `api_base` and `kubernetes_pod_routing`, where the Router writes the deployment it picked: `model_info` holds any fields an admin configured for the deployment, an `api_base` URL can contain credentials, and `kubernetes_pod_routing` holds the internal service host and pod IP of a deployment found through Kubernetes pod discovery. - `userId`, `sessionId`, `user_id` and `session_id`, which the core SDK reserves. Use `propagate_attributes(user_id=..., session_id=...)` instead. Don't use these names for your own metadata. Other keys, including nested values, are exported as they are, so don't put credentials or personal data in metadata. @@ -145,7 +146,7 @@ Don't use these names for your own metadata. Other keys, including nested values When LiteLLM's proxy server handles a request, the call it makes through its Router carries a `proxy_server_request` argument. For every call with that argument, the integration reads metadata differently. The proxy and its guardrails and hooks write their own data into both metadata arguments, such as the request's headers, the authenticated key and user, or Presidio's `pii_tokens`. So only part of one argument is read: `litellm_metadata` for Responses calls, and `metadata` for chat, embedding and rerank calls. These are the arguments the proxy and Router write into for those calls. Two parts of it are exported: - `requester_metadata`, the proxy's copy of the request body's `metadata` object. The proxy takes this copy before its guardrails and hooks run, but after authentication and request preparation have added data to the body's metadata, such as the `x-litellm-tags` header and the key's tags. So these keys of the copy are not exported, even when the client sent them in the body: `tags`, `spend_logs_metadata`, `agent_id`, `trace_id`, `session_id`, `used_client_oauth_token` and `litellm_session_id_omitted`, plus the keys dropped for every call listed above. If the body has no `metadata` object, nothing from `requester_metadata` is exported. -- The Router's `model_group`, `model_group_alias`, `model_group_size`, `deployment`, `deployment_model_name`, `attempted_retries` and `max_retries`, when that argument holds a string, number or boolean under them. Objects, lists and `null` under these names are not exported. The Router's `model_info` and `api_base` are not exported, because `model_info` holds any fields an admin configured for the deployment and an `api_base` URL can contain credentials. +- The Router's `model_group`, `model_group_alias`, `model_group_size`, `deployment`, `deployment_model_name`, `attempted_retries` and `max_retries`, when that argument holds a string, number or boolean under them. Objects, lists and `null` under these names are not exported. Anything else is not exported, including keys the client sends in the body's `litellm_metadata`. Some limits remain: diff --git a/sdks/python-litellm/src/telemetry_dev_litellm/__init__.py b/sdks/python-litellm/src/telemetry_dev_litellm/__init__.py index d7e042b..d583a92 100644 --- a/sdks/python-litellm/src/telemetry_dev_litellm/__init__.py +++ b/sdks/python-litellm/src/telemetry_dev_litellm/__init__.py @@ -403,11 +403,14 @@ def _metadata(value: Any) -> Mapping[str, Any] | None: } ) _PROXY_AUTH_METADATA_PREFIX = "user_api_key" +# Keys the Router writes into the caller's metadata argument for the deployment it picked, +# dropped on every call: model_info holds whatever fields an admin configured for the +# deployment, an api_base URL can carry credentials, and kubernetes_pod_routing (LiteLLM 1.110 +# and later) holds the internal service host and pod IP of a pod-discovered deployment. +_ROUTER_DEPLOYMENT_METADATA_KEYS = frozenset({"model_info", "api_base", "kubernetes_pod_routing"}) # Keys the Router writes into the metadata argument when it picks a deployment and retries, # exported on the proxy only with a scalar value, since a hook can write anything under them. -# model_info and api_base are left out: model_info holds whatever fields an admin configured -# for the deployment, and an api_base URL can carry credentials. _PROXY_ROUTER_METADATA_KEYS = ( "model_group", "model_group_alias", @@ -452,6 +455,7 @@ def _request_metadata( key: value for key, value in metadata.items() if str(key) not in _PROXY_REQUEST_METADATA_KEYS + and str(key) not in _ROUTER_DEPLOYMENT_METADATA_KEYS and not str(key).startswith(_PROXY_AUTH_METADATA_PREFIX) } diff --git a/sdks/python-litellm/tests/test_litellm.py b/sdks/python-litellm/tests/test_litellm.py index c575801..b81c1b0 100644 --- a/sdks/python-litellm/tests/test_litellm.py +++ b/sdks/python-litellm/tests/test_litellm.py @@ -240,7 +240,14 @@ def test_completion_metadata_param_maps_to_td_metadata( [ ( "/v1/chat/completions", - {"messages": CHAT_MESSAGES, "metadata": {"tenant": "acme"}}, + { + "messages": CHAT_MESSAGES, + "metadata": { + "tenant": "acme", + "api_base": "https://user:api-base-secret@llm.internal/v1", + "model_info": {"note": "model-info-secret"}, + }, + }, True, {}, ), @@ -394,7 +401,9 @@ async def test_litellm_proxy_request_exports_only_requester_and_router_metadata( assert leaked == [] -def test_router_fallback_drops_the_failed_call_history(memory: SimpleNamespace) -> None: +def test_router_drops_the_deployment_config_and_the_failed_call_history( + memory: SimpleNamespace, +) -> None: instrument_litellm() router = llm.Router( model_list=[ @@ -404,7 +413,13 @@ def test_router_fallback_drops_the_failed_call_history(memory: SimpleNamespace) }, { "model_name": "backup", - "litellm_params": {"model": "gpt-4o-mini", "mock_response": "routed"}, + "litellm_params": { + "model": "openai/gpt-4o-mini", + "api_key": "sk-fake", + "api_base": "https://user:api-base-secret@llm.internal/v1", + "mock_response": "routed", + }, + "model_info": {"note": "model-info-secret"}, }, ], fallbacks=[{"primary": ["backup"]}], @@ -413,7 +428,12 @@ def test_router_fallback_drops_the_failed_call_history(memory: SimpleNamespace) router.completion( model="primary", messages=CHAT_MESSAGES, - metadata={"tenant": "acme", "headers": {"cookie": "session=cookie-secret"}}, + metadata={ + "tenant": "acme", + "headers": {"cookie": "session=cookie-secret"}, + # What pod discovery writes, which the mock deployments cannot reach. + "kubernetes_pod_routing": {"service_host": "llm.internal", "pod_ip": "10.0.0.7"}, + }, ) spans = memory.span_exporter.get_finished_spans() @@ -425,8 +445,22 @@ def test_router_fallback_drops_the_failed_call_history(memory: SimpleNamespace) ] exported = [(key, str(value)) for found in attribute_sets for key, value in found.items()] assert ("td.metadata.tenant", "acme") in exported - assert [key for key, _ in exported if key == "td.metadata.previous_models"] == [] - assert [key for key, value in exported if "cookie-secret" in value] == [] + assert ("td.metadata.deployment", "openai/gpt-4o-mini") in exported + dropped = { + "td.metadata.previous_models", + "td.metadata.api_base", + "td.metadata.model_info", + "td.metadata.kubernetes_pod_routing", + } + assert [key for key, _ in exported if key in dropped] == [] + assert [ + key + for key, value in exported + if any( + secret in value + for secret in ("cookie-secret", "api-base-secret", "model-info-secret", "10.0.0.7") + ) + ] == [] def test_completion_cache_write_tokens_map_to_cache_creation(