docs(guardrails): cut the _ensure_litellm_metadata docstring to the trap

It recounted the whole investigation; AGENTS.md asks explanations to stay
concise. What a reader needs is why not to write data["litellm_metadata"]
directly, which is seven lines. The failure history lives in #45543 and
in the test class docstring, where it explains what the assertions are
for.
This commit is contained in:
Vivek1898 2026-10-18 14:39:52 +05:30 • committed by Vivek1898
parent f90ca3ff17
commit 567fdd24ed

View file

@ -225,20 +225,12 @@ def _a2a_jsonrpc_error_chunk(exc: HTTPException, request_id: str | None) -> Mapp
def _ensure_litellm_metadata(data: dict, user_api_key_dict: UserAPIKeyAuth) -> None:
"""Merge the ``user_api_key_*`` metadata into the request's resolved bucket.
Writing ``data["litellm_metadata"]`` directly creates a second bucket on a
route whose bucket is ``metadata`` (chat completions). Every reader resolves
the bucket through ``get_metadata_variable_name_from_kwargs``, which answers
``litellm_metadata`` as soon as that key merely exists, so from then on they
all read the new dict -- which holds only the ``user_api_key_*`` keys. The
tags and router fields stay behind in ``metadata``, unread:
``get_deployments_for_tag`` finds no tags and routes the request to a
deployment of another model, and SpendLogs lose ``model_group`` and
``model_id``. Nothing raises (#45543).
``BaseTranslation.merge_user_api_key_metadata_into_request`` is the sanctioned
writer for exactly this -- it resolves the bucket via
``get_or_create_metadata_bucket`` and ``setdefault``s each key, so an existing
value is never overwritten. The OpenAI chat guardrail handler already uses it.
Not ``data["litellm_metadata"]``: creating that key on a route whose bucket
is ``metadata`` flips every reader of
``get_metadata_variable_name_from_kwargs`` onto the new dict and silently
strands the tags and router fields (#45543).
``merge_user_api_key_metadata_into_request`` resolves the bucket and
``setdefault``s, so an upstream value is never overwritten.
"""
from litellm.llms.base_llm.guardrail_translation.base_translation import (
BaseTranslation,