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]

refactor(otel): walk nested metadata iteratively instead of recursively

yassin8cab3a784656
diff --git a/litellm/integrations/otel/model/metadata.py b/litellm/integrations/otel/model/metadata.py
index d1fb3beae20..9c2c214a45c 100644
--- a/litellm/integrations/otel/model/metadata.py
+++ b/litellm/integrations/otel/model/metadata.py
@@ -374,13 +374,15 @@ def metadata_from_request_data(data: object) -> Mapping[str, object] | None:
)
-def flatten_metadata(raw: Mapping[str, object], prefix: str = "") -> Iterator[tuple[str, str]]:
+def flatten_metadata(raw: Mapping[str, object]) -> Iterator[tuple[str, str]]:
"""Scalar leaves of a nested metadata mapping, keyed by their dotted path."""
- for key, value in raw.items():
+ stack: Final = list(tuple(raw.items())[::-1]) # mutable-ok: iterative worklist keeps the walk off the call stack
+ while stack:
+ key, value = stack.pop()
if (nested := _as_str_mapping(value)) is not None:
- yield from flatten_metadata(nested, f"{prefix}{key}.")
+ stack.extend(tuple((f"{key}.{sub_key}", sub_value) for sub_key, sub_value in nested.items())[::-1])
elif isinstance(value, (str, bool, int, float)):
- yield f"{prefix}{key}", str(value)
+ yield key, str(value)
def resolve_provider_model(payload: StandardLoggingPayload) -> str | None: