Skip to content

Commit

Permalink
fix add_metadata_from_header
Browse files Browse the repository at this point in the history
  • Loading branch information
ishaan-jaff committed Jun 6, 2024
1 parent 7b2c163 commit 059c59f
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions litellm/integrations/langfuse.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,36 @@ def __init__(
@staticmethod
def add_metadata_from_header(litellm_params: dict, metadata: dict) -> dict:
"""
Adds metadata from proxy request headers to Langfuse logging if keys start with "langfuse_"
Adds metadata from proxy request headers to Langfuse logging if keys start with "langfuse_"
and overwrites litellm_params.metadata if already included.
For example if you want to append your trace to an existing `trace_id` via header, send
`headers: { ..., langfuse_existing_trace_id: your-existing-trace-id }` via proxy request.
"""
proxy_headers = litellm_params.get("proxy_server_request", {}).get("headers", {})
if litellm_params is None:
return metadata

if litellm_params.get("proxy_server_request") is None:
return metadata

if metadata is None:
metadata = {}

proxy_headers = (
litellm_params.get("proxy_server_request", {}).get("headers", {}) or {}
)

for metadata_param_key in proxy_headers:
if metadata_param_key.startswith("langfuse_"):
trace_param_key = metadata_param_key.replace("langfuse_", "", 1)
if trace_param_key in metadata:
verbose_logger.warning(f"Overwriting Langfuse `{trace_param_key}` from request header")
verbose_logger.warning(
f"Overwriting Langfuse `{trace_param_key}` from request header"
)
else:
verbose_logger.debug(f"Found Langfuse `{trace_param_key}` in request header")
verbose_logger.debug(
f"Found Langfuse `{trace_param_key}` in request header"
)
metadata[trace_param_key] = proxy_headers.get(metadata_param_key)

return metadata
Expand Down

0 comments on commit 059c59f

Please sign in to comment.