From 8cab3a78465610b59de920e1c4d9bac5560566d3 Mon Sep 17 00:00:00 2001 From: yassin Date: Wed, 16 Sep 2026 18:20:10 +0000 Subject: [PATCH] refactor(otel): walk nested metadata iteratively instead of recursively Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- litellm/integrations/otel/model/metadata.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) 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: