From 74e50d9d351526c8b14d2d974f94804aa2103ef0 Mon Sep 17 00:00:00 2001 From: ishaan-jaff Date: Wed, 6 Mar 2024 12:17:59 -0800 Subject: [PATCH] (fix) high traffic langfuse logging --- litellm/integrations/langfuse.py | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/litellm/integrations/langfuse.py b/litellm/integrations/langfuse.py index bb8b62b507..8d135e8495 100644 --- a/litellm/integrations/langfuse.py +++ b/litellm/integrations/langfuse.py @@ -265,8 +265,14 @@ class LangFuseLogger: cost = kwargs.get("response_cost", None) print_verbose(f"trace: {cost}") - if supports_tags: + + # Clean Metadata before logging - never log raw metadata + # the raw metadata can contain circular references which leads to infinite recursion + # we clean out all extra litellm metadata params before logging + clean_metadata = {} + if isinstance(metadata, dict): for key, value in metadata.items(): + # generate langfuse tags if key in [ "user_api_key", "user_api_key_user_id", @@ -274,6 +280,22 @@ class LangFuseLogger: "semantic-similarity", ]: tags.append(f"{key}:{value}") + + # clean litellm metadata before logging + if key in [ + "headers", + "endpoint", + "model_group", + "deployment", + "model_info", + "caching_groups", + "previous_models", + ]: + continue + else: + clean_metadata[key] = value + + if supports_tags: if "cache_hit" in kwargs: if kwargs["cache_hit"] is None: kwargs["cache_hit"] = False @@ -301,7 +323,7 @@ class LangFuseLogger: "input": input, "output": output, "usage": usage, - "metadata": metadata, + "metadata": clean_metadata, "level": level, }