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]

fix(braintrust): pass span_attributes in async logging and skip tags on non-root spans (#18409)

Wen-Tien Chang5a4242e18825
diff --git a/litellm/integrations/braintrust_logging.py b/litellm/integrations/braintrust_logging.py
index 364fa3f5def..585de510e8b 100644
--- a/litellm/integrations/braintrust_logging.py
+++ b/litellm/integrations/braintrust_logging.py
@@ -225,10 +225,13 @@ class BraintrustLogger(CustomLogger):
"id": litellm_call_id,
"input": prompt["messages"],
"metadata": standard_logging_object,
- "tags": tags,
"span_attributes": {"name": span_name, "type": "llm"},
}
-
+
+ # Braintrust cannot specify 'tags' for non-root spans
+ if dynamic_metadata.get("root_span_id") is None:
+ request_data["tags"] = tags
+
# Only add those that are not None (or falsy)
for key, value in span_attributes.items():
if value:
@@ -351,14 +354,37 @@ class BraintrustLogger(CustomLogger):
# Allow metadata override for span name
span_name = dynamic_metadata.get("span_name", "Chat Completion")
+ # Span parents is a special case
+ span_parents = dynamic_metadata.get("span_parents")
+
+ # Convert comma-separated string to list if present
+ if span_parents:
+ span_parents = [s.strip() for s in span_parents.split(",") if s.strip()]
+
+ # Add optional span attributes only if present
+ span_attributes = {
+ "span_id": dynamic_metadata.get("span_id"),
+ "root_span_id": dynamic_metadata.get("root_span_id"),
+ "span_parents": span_parents,
+ }
+
request_data = {
"id": litellm_call_id,
"input": prompt["messages"],
"output": output,
"metadata": standard_logging_object,
- "tags": tags,
"span_attributes": {"name": span_name, "type": "llm"},
}
+
+ # Braintrust cannot specify 'tags' for non-root spans
+ if dynamic_metadata.get("root_span_id") is None:
+ request_data["tags"] = tags
+
+ # Only add those that are not None (or falsy)
+ for key, value in span_attributes.items():
+ if value:
+ request_data[key] = value
+
if choices is not None:
request_data["output"] = [choice.dict() for choice in choices]
else:
@@ -367,9 +393,6 @@ class BraintrustLogger(CustomLogger):
if metrics is not None:
request_data["metrics"] = metrics
- if metrics is not None:
- request_data["metrics"] = metrics
-
try:
await self.global_braintrust_http_handler.post(
url=f"{self.api_base}/project_logs/{project_id}/insert",