From cb4a6bf983668528964da3b7a2240716b2e28039 Mon Sep 17 00:00:00 2001 From: Yujong Lee Date: Thu, 1 Oct 2026 13:38:21 -0700 Subject: [PATCH 1/7] refactor(tracing): normalize agent spans in Rust --- litellm-rust/crates/python-bridge/src/lib.rs | 5 +- .../crates/python-bridge/src/routes/traces.rs | 5 + litellm-rust/crates/traces/src/lib.rs | 4 + .../crates/traces/src/normalize/genai.rs | 46 +++ .../crates/traces/src/normalize/langsmith.rs | 385 ++++++++++++++++++ .../crates/traces/src/normalize/mod.rs | 191 +++++++++ .../traces/src/normalize/openinference.rs | 39 ++ litellm-rust/crates/traces/src/otlp.rs | 11 +- .../crates/traces/tests/migrations.rs | 41 +- litellm-rust/crates/traces/tests/otlp.rs | 46 ++- litellm/rust_bridge/_native.pyi | 2 + litellm/rust_bridge/traces.py | 40 +- litellm/tracing/decode.py | 43 +- litellm/tracing/{normalizers => }/messages.py | 19 +- litellm/tracing/normalizers/__init__.py | 32 -- litellm/tracing/normalizers/base.py | 22 - litellm/tracing/normalizers/genai.py | 31 -- litellm/tracing/normalizers/langsmith.py | 115 ------ litellm/tracing/normalizers/openinference.py | 27 -- litellm/tracing/ui_format.py | 2 +- .../tracing/normalizers/test_registry.py | 68 ---- 21 files changed, 818 insertions(+), 356 deletions(-) create mode 100644 litellm-rust/crates/traces/src/normalize/genai.rs create mode 100644 litellm-rust/crates/traces/src/normalize/langsmith.rs create mode 100644 litellm-rust/crates/traces/src/normalize/mod.rs create mode 100644 litellm-rust/crates/traces/src/normalize/openinference.rs rename litellm/tracing/{normalizers => }/messages.py (61%) delete mode 100644 litellm/tracing/normalizers/__init__.py delete mode 100644 litellm/tracing/normalizers/base.py delete mode 100644 litellm/tracing/normalizers/genai.py delete mode 100644 litellm/tracing/normalizers/langsmith.py delete mode 100644 litellm/tracing/normalizers/openinference.py delete mode 100644 tests/test_litellm/tracing/normalizers/test_registry.py diff --git a/litellm-rust/crates/python-bridge/src/lib.rs b/litellm-rust/crates/python-bridge/src/lib.rs index 0d4df996552a..6ba53d0c415e 100644 --- a/litellm-rust/crates/python-bridge/src/lib.rs +++ b/litellm-rust/crates/python-bridge/src/lib.rs @@ -44,7 +44,9 @@ mod _native { #[pymodule_export] use crate::routes::token_counter::TokenCounter; #[pymodule_export] - use crate::routes::traces::{NativeTraceStorage, trace_decode_otlp}; + use crate::routes::traces::{ + NativeTraceStorage, trace_decode_otlp, trace_normalized_field_definitions, + }; #[cfg(feature = "huggingface")] #[pymodule_export] use crate::tokenizer::HuggingFaceEncoding; @@ -111,6 +113,7 @@ mod tests { "NativeDiagnosticProcessor", "NativeTraceStorage", "trace_decode_otlp", + "trace_normalized_field_definitions", "TokenCounter", "Tokenizer", "gil_stats", diff --git a/litellm-rust/crates/python-bridge/src/routes/traces.rs b/litellm-rust/crates/python-bridge/src/routes/traces.rs index 2e7a6b178a80..f64f79197681 100644 --- a/litellm-rust/crates/python-bridge/src/routes/traces.rs +++ b/litellm-rust/crates/python-bridge/src/routes/traces.rs @@ -164,3 +164,8 @@ pub fn trace_decode_otlp<'py>( })?; litellm_host_python::Pythonized(spans).into_pyobject(py) } + +#[pyfunction] +pub fn trace_normalized_field_definitions<'py>(py: Python<'py>) -> PyResult> { + litellm_host_python::Pythonized(litellm_traces::NORMALIZED_FIELD_DEFINITIONS).into_pyobject(py) +} diff --git a/litellm-rust/crates/traces/src/lib.rs b/litellm-rust/crates/traces/src/lib.rs index c37602cade44..af8e11dc8c29 100644 --- a/litellm-rust/crates/traces/src/lib.rs +++ b/litellm-rust/crates/traces/src/lib.rs @@ -1,11 +1,15 @@ mod error; mod insert; +mod normalize; mod otlp; mod schema; mod sql; pub use error::{DecodeError, Error}; pub use insert::{InsertTable, encode_rows, insert_rows}; +pub use normalize::{ + NORMALIZED_FIELD_DEFINITIONS, NormalizedFieldDefinition, NormalizedSpan, ObservationType, +}; pub use otlp::{DecodedSpan, decode_otlp}; pub use schema::{ensure_schema, schema_statements}; pub use sql::{LensQuery, Parameter, ReadQuery, execute_named_read, execute_read}; diff --git a/litellm-rust/crates/traces/src/normalize/genai.rs b/litellm-rust/crates/traces/src/normalize/genai.rs new file mode 100644 index 000000000000..5d28a33323bb --- /dev/null +++ b/litellm-rust/crates/traces/src/normalize/genai.rs @@ -0,0 +1,46 @@ +use std::collections::BTreeMap; + +use super::{NormalizedSpan, ObservationType, SpanNormalizer, attr, first}; + +pub(super) struct GenAiNormalizer; + +impl SpanNormalizer for GenAiNormalizer { + fn matches(&self, _scope_name: &str, _attributes: &BTreeMap) -> bool { + true + } + + fn normalize( + &self, + _name: &str, + parent_span_id: &str, + attributes: &BTreeMap, + ) -> NormalizedSpan { + let observation_type = match attr(attributes, "gen_ai.operation.name") { + "invoke_agent" => ObservationType::Agent, + _ if parent_span_id.is_empty() => ObservationType::Agent, + "chat" | "text_completion" | "generate_content" => ObservationType::Llm, + "execute_tool" => ObservationType::Tool, + _ => ObservationType::Chain, + }; + NormalizedSpan { + observation_type, + agent_name: attr(attributes, "gen_ai.agent.name").to_owned(), + litellm_request_id: attr(attributes, "gen_ai.response.id").to_owned(), + model: first(attributes, "gen_ai.request.model", "gen_ai.response.model").to_owned(), + input_tokens: 0, + output_tokens: 0, + input: first( + attributes, + "gen_ai.input.messages", + "gen_ai.tool.call.arguments", + ) + .to_owned(), + output: first( + attributes, + "gen_ai.output.messages", + "gen_ai.tool.call.result", + ) + .to_owned(), + } + } +} diff --git a/litellm-rust/crates/traces/src/normalize/langsmith.rs b/litellm-rust/crates/traces/src/normalize/langsmith.rs new file mode 100644 index 000000000000..990c7f7ed887 --- /dev/null +++ b/litellm-rust/crates/traces/src/normalize/langsmith.rs @@ -0,0 +1,385 @@ +use std::{collections::BTreeMap, io}; + +use serde::{Deserialize, Serialize}; +use serde_json::{Value, ser::Formatter}; + +use super::{NormalizedSpan, ObservationType, SpanNormalizer, attr}; + +pub(super) struct LangSmithNormalizer; + +#[derive(Deserialize)] +#[serde(untagged)] +enum MessageContent { + Text(String), + Blocks(Vec), + Other(Value), +} + +impl MessageContent { + fn display_text(&self) -> String { + match self { + Self::Text(text) => text.clone(), + Self::Blocks(blocks) => blocks + .iter() + .filter_map(|block| match block { + ContentBlock::Text { text } => Some(text.as_str()), + ContentBlock::Hidden(kind) => match kind { + HiddenBlock::Reasoning + | HiddenBlock::Thinking + | HiddenBlock::RedactedThinking + | HiddenBlock::FunctionCall + | HiddenBlock::ToolUse + | HiddenBlock::ToolCall => None, + }, + }) + .collect::>() + .join("\n\n"), + Self::Other(value) => encode(value), + } + } +} + +#[derive(Deserialize)] +#[serde(untagged)] +enum ContentBlock { + Text { text: String }, + Hidden(HiddenBlock), +} + +#[derive(Deserialize)] +#[serde(tag = "type", rename_all = "snake_case")] +enum HiddenBlock { + Reasoning, + Thinking, + RedactedThinking, + FunctionCall, + ToolUse, + ToolCall, +} + +#[derive(Deserialize, Serialize)] +struct RawToolCall { + name: Option, + args: Option, +} + +#[derive(Deserialize)] +struct ResponseMetadata { + id: Option, +} + +#[derive(Deserialize)] +struct RawMessage { + kwargs: Option>, + #[serde(rename = "type")] + kind: Option, + role: Option, + content: Option, + tool_calls: Option>, + name: Option, + response_metadata: Option, +} + +impl RawMessage { + fn unwrapped(&self) -> &Self { + self.kwargs.as_deref().unwrap_or(self) + } + + fn normalized(&self) -> NormalizedMessage<'_> { + let fields = self.unwrapped(); + let raw_role = fields + .kind + .as_deref() + .filter(|role| !role.is_empty()) + .or_else(|| fields.role.as_deref().filter(|role| !role.is_empty())) + .unwrap_or_default(); + let role = match raw_role { + "human" => "user", + "ai" => "assistant", + other => other, + }; + NormalizedMessage { + role, + content: fields + .content + .as_ref() + .map_or_else(String::new, MessageContent::display_text), + tool_calls: fields + .tool_calls + .as_deref() + .filter(|calls| !calls.is_empty()), + name: (role == "tool") + .then_some(fields.name.as_ref()) + .flatten() + .filter(|name| !name.is_null() && name != &&Value::String(String::new())), + } + } +} + +#[derive(Serialize)] +struct NormalizedMessage<'a> { + role: &'a str, + content: String, + #[serde(skip_serializing_if = "Option::is_none")] + tool_calls: Option<&'a [RawToolCall]>, + #[serde(skip_serializing_if = "Option::is_none")] + name: Option<&'a Value>, +} + +#[derive(Deserialize)] +#[serde(untagged)] +enum MessageBatch { + Flat(Vec), + Nested(Vec>), +} + +impl MessageBatch { + fn first_batch(&self) -> &[RawMessage] { + match self { + Self::Flat(messages) => messages, + Self::Nested(batches) => batches.first().map(Vec::as_slice).unwrap_or_default(), + } + } + + fn agent_messages(&self) -> &[RawMessage] { + match self { + Self::Flat(messages) => messages, + Self::Nested(_) => &[], + } + } +} + +#[derive(Deserialize)] +struct GenerationMessage { + kwargs: Option, +} + +#[derive(Deserialize)] +struct Generation { + message: Option, +} + +#[derive(Default, Deserialize)] +struct Payload { + messages: Option, + generations: Option>>, + output: Option, +} + +#[derive(Deserialize)] +struct Command { + update: CommandUpdate, +} + +#[derive(Deserialize)] +struct CommandUpdate { + messages: Vec, +} + +#[derive(Deserialize)] +struct ContentValue { + content: Value, +} + +struct SpanIo { + input: String, + output: String, + request_id: String, +} + +struct PythonJsonFormatter; + +impl Formatter for PythonJsonFormatter { + fn begin_array_value( + &mut self, + writer: &mut W, + first: bool, + ) -> io::Result<()> { + if first { + Ok(()) + } else { + writer.write_all(b", ") + } + } + + fn begin_object_key( + &mut self, + writer: &mut W, + first: bool, + ) -> io::Result<()> { + if first { + Ok(()) + } else { + writer.write_all(b", ") + } + } + + fn begin_object_value(&mut self, writer: &mut W) -> io::Result<()> { + writer.write_all(b": ") + } +} + +fn encode(value: &T) -> String { + let mut output = Vec::new(); + let mut serializer = serde_json::Serializer::with_formatter(&mut output, PythonJsonFormatter); + if value.serialize(&mut serializer).is_err() { + return String::new(); + } + String::from_utf8(output).unwrap_or_default() +} + +fn normalized_messages(messages: &[RawMessage]) -> String { + encode( + &messages + .iter() + .map(RawMessage::normalized) + .collect::>(), + ) +} + +fn span_type( + name: &str, + parent_span_id: &str, + attributes: &BTreeMap, +) -> ObservationType { + if parent_span_id.is_empty() || name == attr(attributes, "langsmith.metadata.lc_agent_name") { + return ObservationType::Agent; + } + match attr(attributes, "langsmith.span.kind") { + "llm" => ObservationType::Llm, + "tool" => ObservationType::Tool, + _ if [ + ".wrap_model_call", + ".wrap_tool_call", + ".before_agent", + ".after_agent", + ".before_model", + ".after_model", + ] + .iter() + .any(|suffix| name.ends_with(suffix)) => + { + ObservationType::Framework + } + _ => ObservationType::Chain, + } +} + +fn tool_output(payload: Payload, raw_completion: &str) -> String { + let completion = serde_json::from_str::(raw_completion).unwrap_or(Value::Null); + let raw = payload.output.unwrap_or(completion); + let selected = serde_json::from_value::(raw.clone()) + .ok() + .and_then(|command| command.update.messages.into_iter().last()) + .unwrap_or(raw); + let output = serde_json::from_value::(selected.clone()) + .map(|message| message.content) + .unwrap_or(selected); + output + .as_str() + .map(str::to_owned) + .unwrap_or_else(|| encode(&output)) +} + +fn span_io(kind: ObservationType, attributes: &BTreeMap) -> SpanIo { + let raw_prompt = attr(attributes, "gen_ai.prompt"); + let raw_completion = attr(attributes, "gen_ai.completion"); + let prompt = serde_json::from_str::(raw_prompt).unwrap_or_default(); + let completion = serde_json::from_str::(raw_completion).unwrap_or_default(); + if kind == ObservationType::Llm + && serde_json::from_str::(raw_completion).is_ok_and(|value| value.is_object()) + { + let input = prompt + .messages + .as_ref() + .map_or_else(String::new, |messages| { + normalized_messages(messages.first_batch()) + }); + let generation = completion + .generations + .as_ref() + .and_then(|batches| batches.first()) + .and_then(|batch| batch.first()) + .and_then(|generation| generation.message.as_ref()) + .and_then(|message| message.kwargs.as_ref()); + if let Some(generation) = generation { + let id = generation + .response_metadata + .as_ref() + .and_then(|metadata| metadata.id.as_deref()) + .unwrap_or_default() + .to_owned(); + return SpanIo { + input, + output: encode(&generation.normalized()), + request_id: id, + }; + } + return SpanIo { + input, + output: raw_completion.to_owned(), + request_id: String::new(), + }; + } + if kind == ObservationType::Tool { + return SpanIo { + input: raw_prompt.to_owned(), + output: tool_output(completion, raw_completion), + request_id: String::new(), + }; + } + if kind == ObservationType::Agent { + let input = prompt + .messages + .as_ref() + .filter(|messages| !messages.agent_messages().is_empty()) + .map_or_else( + || raw_prompt.to_owned(), + |messages| normalized_messages(messages.agent_messages()), + ); + let output = completion + .messages + .as_ref() + .and_then(|messages| messages.agent_messages().last()) + .map_or_else( + || raw_completion.to_owned(), + |message| encode(&message.normalized()), + ); + return SpanIo { + input, + output, + request_id: String::new(), + }; + } + SpanIo { + input: raw_prompt.to_owned(), + output: raw_completion.to_owned(), + request_id: String::new(), + } +} + +impl SpanNormalizer for LangSmithNormalizer { + fn matches(&self, scope_name: &str, attributes: &BTreeMap) -> bool { + scope_name == "langsmith" || attributes.contains_key("langsmith.span.kind") + } + + fn normalize( + &self, + name: &str, + parent_span_id: &str, + attributes: &BTreeMap, + ) -> NormalizedSpan { + let observation_type = span_type(name, parent_span_id, attributes); + let io = span_io(observation_type, attributes); + NormalizedSpan { + observation_type, + agent_name: attr(attributes, "langsmith.metadata.lc_agent_name").to_owned(), + litellm_request_id: io.request_id, + model: attr(attributes, "gen_ai.request.model").to_owned(), + input_tokens: 0, + output_tokens: 0, + input: io.input, + output: io.output, + } + } +} diff --git a/litellm-rust/crates/traces/src/normalize/mod.rs b/litellm-rust/crates/traces/src/normalize/mod.rs new file mode 100644 index 000000000000..d6d3813c5ccd --- /dev/null +++ b/litellm-rust/crates/traces/src/normalize/mod.rs @@ -0,0 +1,191 @@ +use std::collections::BTreeMap; + +use serde::Serialize; + +#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)] +#[serde(rename_all = "lowercase")] +pub enum ObservationType { + Agent, + Llm, + Tool, + Chain, + Framework, +} + +#[derive(Debug, Serialize)] +pub struct NormalizedSpan { + pub observation_type: ObservationType, + pub agent_name: String, + pub litellm_request_id: String, + pub model: String, + pub input_tokens: u32, + pub output_tokens: u32, + pub input: String, + pub output: String, +} + +#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)] +pub struct NormalizedFieldDefinition { + pub name: &'static str, + pub clickhouse_column: &'static str, + pub clickhouse_type: &'static str, + pub meaning: &'static str, +} + +pub const NORMALIZED_FIELD_DEFINITIONS: [NormalizedFieldDefinition; 8] = [ + NormalizedFieldDefinition { + name: "observation_type", + clickhouse_column: "ObservationType", + clickhouse_type: "LowCardinality(String)", + meaning: "Agent, LLM, tool, chain, or framework span", + }, + NormalizedFieldDefinition { + name: "agent_name", + clickhouse_column: "AgentName", + clickhouse_type: "LowCardinality(String)", + meaning: "Agent associated with this span", + }, + NormalizedFieldDefinition { + name: "litellm_request_id", + clickhouse_column: "LiteLLMRequestId", + clickhouse_type: "String", + meaning: "LiteLLM response ID used to link a span to a spend log", + }, + NormalizedFieldDefinition { + name: "model", + clickhouse_column: "Model", + clickhouse_type: "LowCardinality(String)", + meaning: "Model used by this span", + }, + NormalizedFieldDefinition { + name: "input_tokens", + clickhouse_column: "InputTokens", + clickhouse_type: "UInt32", + meaning: "Input token count", + }, + NormalizedFieldDefinition { + name: "output_tokens", + clickhouse_column: "OutputTokens", + clickhouse_type: "UInt32", + meaning: "Output token count", + }, + NormalizedFieldDefinition { + name: "input", + clickhouse_column: "Input", + clickhouse_type: "String", + meaning: "Normalized input payload", + }, + NormalizedFieldDefinition { + name: "output", + clickhouse_column: "Output", + clickhouse_type: "String", + meaning: "Normalized output payload", + }, +]; + +trait SpanNormalizer { + fn matches(&self, scope_name: &str, attributes: &BTreeMap) -> bool; + fn normalize( + &self, + name: &str, + parent_span_id: &str, + attributes: &BTreeMap, + ) -> NormalizedSpan; +} + +mod genai; +mod langsmith; +mod openinference; + +use genai::GenAiNormalizer; +use langsmith::LangSmithNormalizer; +use openinference::OpenInferenceNormalizer; + +fn attr<'a>(attributes: &'a BTreeMap, key: &str) -> &'a str { + attributes.get(key).map(String::as_str).unwrap_or_default() +} + +fn first<'a>(attributes: &'a BTreeMap, left: &str, right: &str) -> &'a str { + let value = attr(attributes, left); + if value.is_empty() { + attr(attributes, right) + } else { + value + } +} + +fn tokens(attributes: &BTreeMap, key: &str) -> u32 { + attr(attributes, key).parse().unwrap_or(0) +} + +pub fn normalize( + scope_name: &str, + name: &str, + parent_span_id: &str, + attributes: &BTreeMap, +) -> NormalizedSpan { + let normalizers: [&dyn SpanNormalizer; 3] = [ + &LangSmithNormalizer, + &OpenInferenceNormalizer, + &GenAiNormalizer, + ]; + let normalizer = normalizers + .into_iter() + .find(|normalizer| normalizer.matches(scope_name, attributes)) + .expect("GenAI fallback always matches"); + let fields = normalizer.normalize(name, parent_span_id, attributes); + if fields.input_tokens != 0 || fields.output_tokens != 0 { + return fields; + } + NormalizedSpan { + input_tokens: tokens(attributes, "gen_ai.usage.input_tokens"), + output_tokens: tokens(attributes, "gen_ai.usage.output_tokens"), + ..fields + } +} + +#[cfg(test)] +mod tests { + use std::collections::{BTreeMap, BTreeSet}; + + use rstest::rstest; + + use super::{NORMALIZED_FIELD_DEFINITIONS, ObservationType, normalize}; + + #[rstest] + #[case::langsmith("langsmith", [("langsmith.span.kind", "llm"), ("openinference.span.kind", "TOOL")], ObservationType::Llm)] + #[case::openinference("other", [("openinference.span.kind", "LLM"), ("gen_ai.operation.name", "execute_tool")], ObservationType::Llm)] + #[case::genai("other", [("gen_ai.operation.name", "execute_tool"), ("gen_ai.usage.input_tokens", "7")], ObservationType::Tool)] + fn convention_dispatch_preserves_precedence( + #[case] scope: &str, + #[case] attributes: [(&str, &str); 2], + #[case] expected: ObservationType, + ) { + let attributes = attributes + .into_iter() + .map(|(key, value)| (key.to_owned(), value.to_owned())) + .collect(); + let fields = normalize(scope, "step", "parent", &attributes); + assert_eq!(fields.observation_type, expected); + if expected == ObservationType::Tool { + assert_eq!(fields.input_tokens, 7); + } + } + + #[rstest] + fn field_definitions_match_serialized_normalized_span() { + let fields = normalize("", "root", "", &BTreeMap::new()); + let serialized = serde_json::to_value(fields).expect("serializable fields"); + let keys: BTreeSet<_> = serialized + .as_object() + .expect("field object") + .keys() + .map(String::as_str) + .collect(); + let mapped: BTreeSet<_> = NORMALIZED_FIELD_DEFINITIONS + .iter() + .map(|field| field.name) + .collect(); + assert_eq!(keys, mapped); + } +} diff --git a/litellm-rust/crates/traces/src/normalize/openinference.rs b/litellm-rust/crates/traces/src/normalize/openinference.rs new file mode 100644 index 000000000000..08cc75799f4b --- /dev/null +++ b/litellm-rust/crates/traces/src/normalize/openinference.rs @@ -0,0 +1,39 @@ +use std::collections::BTreeMap; + +use super::{NormalizedSpan, ObservationType, SpanNormalizer, attr, tokens}; + +pub(super) struct OpenInferenceNormalizer; + +impl SpanNormalizer for OpenInferenceNormalizer { + fn matches(&self, _scope_name: &str, attributes: &BTreeMap) -> bool { + attributes.contains_key("openinference.span.kind") + } + + fn normalize( + &self, + _name: &str, + parent_span_id: &str, + attributes: &BTreeMap, + ) -> NormalizedSpan { + let observation_type = match attr(attributes, "openinference.span.kind") + .to_ascii_uppercase() + .as_str() + { + "AGENT" => ObservationType::Agent, + "LLM" => ObservationType::Llm, + "TOOL" => ObservationType::Tool, + _ if parent_span_id.is_empty() => ObservationType::Agent, + _ => ObservationType::Chain, + }; + NormalizedSpan { + observation_type, + agent_name: attr(attributes, "agent.name").to_owned(), + litellm_request_id: String::new(), + model: attr(attributes, "llm.model_name").to_owned(), + input_tokens: tokens(attributes, "llm.token_count.prompt"), + output_tokens: tokens(attributes, "llm.token_count.completion"), + input: attr(attributes, "input.value").to_owned(), + output: attr(attributes, "output.value").to_owned(), + } + } +} diff --git a/litellm-rust/crates/traces/src/otlp.rs b/litellm-rust/crates/traces/src/otlp.rs index f162256ef1f0..b34490360a30 100644 --- a/litellm-rust/crates/traces/src/otlp.rs +++ b/litellm-rust/crates/traces/src/otlp.rs @@ -11,7 +11,7 @@ use prost::Message; use serde::Serialize; use serde_json::Value; -use crate::DecodeError; +use crate::{DecodeError, NormalizedSpan, normalize::normalize}; #[derive(Serialize)] pub struct DecodedEvent { @@ -36,6 +36,7 @@ pub struct DecodedSpan { pub status_code: String, pub status_message: String, pub events: Vec, + pub normalized: NormalizedSpan, } pub fn decode_otlp( @@ -137,10 +138,13 @@ fn decoded_span( scope_version: &str, ) -> DecodedSpan { let status = span.status.unwrap_or_default(); + let span_attributes = attributes(span.attributes); + let parent_span_id = hex_bytes(&span.parent_span_id); + let normalized = normalize(scope_name, &span.name, &parent_span_id, &span_attributes); DecodedSpan { trace_id: hex_bytes(&span.trace_id), span_id: hex_bytes(&span.span_id), - parent_span_id: hex_bytes(&span.parent_span_id), + parent_span_id, trace_state: span.trace_state, name: span.name, kind: SpanKind::try_from(span.kind) @@ -150,7 +154,7 @@ fn decoded_span( resource_attributes: resource_attributes.clone(), scope_name: scope_name.to_owned(), scope_version: scope_version.to_owned(), - attributes: attributes(span.attributes), + attributes: span_attributes, start_ns: span.start_time_unix_nano, end_ns: span.end_time_unix_nano, status_code: StatusCode::try_from(status.code) @@ -166,6 +170,7 @@ fn decoded_span( attributes: attributes(event.attributes), }) .collect(), + normalized, } } diff --git a/litellm-rust/crates/traces/tests/migrations.rs b/litellm-rust/crates/traces/tests/migrations.rs index cc8fe51a4692..c274c93c774f 100644 --- a/litellm-rust/crates/traces/tests/migrations.rs +++ b/litellm-rust/crates/traces/tests/migrations.rs @@ -2,8 +2,8 @@ use std::{collections::BTreeMap, time::Duration}; use litellm_http::Client; use litellm_traces::{ - Connection, Error, InsertTable, Parameter, ReadQuery, encode_rows, ensure_schema, - execute_named_read, execute_read, schema_statements, + Connection, Error, InsertTable, NORMALIZED_FIELD_DEFINITIONS, Parameter, ReadQuery, + encode_rows, ensure_schema, execute_named_read, execute_read, schema_statements, }; use rstest::{fixture, rstest}; use testcontainers_modules::{ @@ -210,6 +210,43 @@ async fn schema_supports_span_rollups_and_spend_joins( Ok(()) } +#[rstest] +#[tokio::test] +async fn normalized_fields_match_clickhouse_catalog( + #[future(awt)] database: TestResult, +) -> TestResult { + let database = database?; + ensure_schema( + &database.client, + &Connection::writer(&database.url)?, + "trace_test", + 7, + 14, + ) + .await?; + let catalog = read_json(&database, "SELECT name, type FROM system.columns WHERE database = 'trace_test' AND table = 'otel_traces'").await?; + let columns: BTreeMap<&str, &str> = catalog["data"] + .as_array() + .expect("catalog rows") + .iter() + .map(|row| { + ( + row["name"].as_str().expect("column name"), + row["type"].as_str().expect("column type"), + ) + }) + .collect(); + for field in NORMALIZED_FIELD_DEFINITIONS { + assert_eq!( + columns.get(field.clickhouse_column).copied(), + Some(field.clickhouse_type), + "{}", + field.name + ); + } + Ok(()) +} + #[rstest] #[tokio::test] async fn insert_rejects_unknown_columns_even_if_url_requests_skipping_them( diff --git a/litellm-rust/crates/traces/tests/otlp.rs b/litellm-rust/crates/traces/tests/otlp.rs index 002ba159ef9a..cde8286ceee9 100644 --- a/litellm-rust/crates/traces/tests/otlp.rs +++ b/litellm-rust/crates/traces/tests/otlp.rs @@ -1,5 +1,5 @@ use flate2::{Compression, write::GzEncoder}; -use litellm_traces::decode_otlp; +use litellm_traces::{ObservationType, decode_otlp}; use rstest::rstest; use std::io::Write; @@ -45,3 +45,47 @@ fn rejects_invalid_or_oversized_payload( ) { assert!(decode_otlp(body, content_type, None, limit).is_err()); } + +#[rstest] +fn normalizes_langsmith_fixture() { + let spans = decode_otlp(FIXTURE, Some("application/json"), None, 8 * 1024 * 1024) + .expect("valid OTLP export"); + let llm = spans + .iter() + .find(|span| span.name == "ChatOpenAI") + .expect("LLM span"); + assert_eq!(llm.normalized.observation_type, ObservationType::Llm); + assert_eq!(llm.normalized.agent_name, "deep_research_agent"); + assert_eq!(llm.normalized.model, "claude-sonnet-4-5"); + assert_eq!( + (llm.normalized.input_tokens, llm.normalized.output_tokens), + (3332, 467) + ); + assert_eq!( + llm.normalized.litellm_request_id, + "chatcmpl-4077bb36-9380-4a3b-9481-245700cef09a" + ); + let input: serde_json::Value = + serde_json::from_str(&llm.normalized.input).expect("message input"); + assert_eq!(input[0]["role"], "system"); + assert_eq!(input[1]["role"], "user"); + let output: serde_json::Value = + serde_json::from_str(&llm.normalized.output).expect("message output"); + assert_eq!(output["role"], "assistant"); + assert!(output["tool_calls"][0]["name"].is_string()); + let root = spans + .iter() + .find(|span| span.name == "deep_research_agent") + .expect("root span"); + assert_eq!(root.normalized.observation_type, ObservationType::Agent); + assert_eq!( + root.normalized.input, + "[{\"role\": \"user\", \"content\": \"Should we store OTEL agent spans in ClickHouse or Postgres at 50k spans/sec?\"}]" + ); + let tool = spans + .iter() + .find(|span| span.name == "task") + .expect("tool span"); + assert_eq!(tool.normalized.observation_type, ObservationType::Tool); + assert!(tool.normalized.output.starts_with("Based on my research")); +} diff --git a/litellm/rust_bridge/_native.pyi b/litellm/rust_bridge/_native.pyi index 206c0f78ed82..feb18b700adf 100644 --- a/litellm/rust_bridge/_native.pyi +++ b/litellm/rust_bridge/_native.pyi @@ -24,6 +24,7 @@ class ProcessReservedForForking(RuntimeError): ... def trace_decode_otlp( body: bytes, content_type: str | None, content_encoding: str | None, max_decompressed_bytes: int ) -> list[DecodedSpan]: ... +def trace_normalized_field_definitions() -> list[dict[str, str]]: ... @final class NativeTraceStorage: @@ -353,6 +354,7 @@ __all__ = [ "reserve_process_for_forking", "responses", "trace_decode_otlp", + "trace_normalized_field_definitions", "transcription", ] diff --git a/litellm/rust_bridge/traces.py b/litellm/rust_bridge/traces.py index 98607aa9206a..df12ae8ee025 100644 --- a/litellm/rust_bridge/traces.py +++ b/litellm/rust_bridge/traces.py @@ -2,7 +2,7 @@ from types import MappingProxyType from typing import Final, Literal, Protocol, TypedDict, cast -from pydantic import BaseModel, ConfigDict, JsonValue, TypeAdapter +from pydantic import BaseModel, ConfigDict, Field, JsonValue, TypeAdapter from typing_extensions import ReadOnly from litellm.rust_bridge.loader import get_native_bridge @@ -13,6 +13,28 @@ class DecodedEvent(TypedDict): attributes: ReadOnly[dict[str, str]] +class NormalizedSpan(BaseModel): + model_config = ConfigDict(frozen=True, extra="forbid", strict=True) + + observation_type: Literal["agent", "llm", "tool", "chain", "framework"] + agent_name: str + litellm_request_id: str + model: str + input_tokens: int = Field(ge=0, le=2**32 - 1) + output_tokens: int = Field(ge=0, le=2**32 - 1) + input: str + output: str + + +class NormalizedFieldDefinition(BaseModel): + model_config = ConfigDict(frozen=True, extra="forbid", strict=True) + + name: str + clickhouse_column: str + clickhouse_type: str + meaning: str + + class DecodedSpan(TypedDict): trace_id: ReadOnly[str] span_id: ReadOnly[str] @@ -29,6 +51,7 @@ class DecodedSpan(TypedDict): status_code: ReadOnly[str] status_message: ReadOnly[str] events: ReadOnly[list[DecodedEvent]] + normalized: ReadOnly[NormalizedSpan] ReadQueryName = Literal["list_traces", "trace_spans", "span_detail", "spend_by_response_ids"] @@ -57,6 +80,8 @@ def trace_decode_otlp( max_decompressed_bytes: int, ) -> list[DecodedSpan]: ... + def trace_normalized_field_definitions(self) -> list[dict[str, str]]: ... + class QueryResponse(BaseModel): model_config = ConfigDict(frozen=True) @@ -65,6 +90,8 @@ class QueryResponse(BaseModel): INSERT_ROWS: Final = TypeAdapter(list[dict[str, JsonValue]]) QUERY_PARAMETERS: Final = TypeAdapter(dict[str, str | int | list[str]]) +DECODED_SPANS: Final = TypeAdapter(list[DecodedSpan]) +_FIELD_DEFINITIONS_ADAPTER: Final = TypeAdapter(tuple[NormalizedFieldDefinition, ...]) def _native() -> NativeTraces: @@ -77,7 +104,16 @@ def _native() -> NativeTraces: def decode_otlp( body: bytes, content_type: str | None, content_encoding: str | None, max_decompressed_bytes: int ) -> list[DecodedSpan]: - return _native().trace_decode_otlp(body, content_type, content_encoding, max_decompressed_bytes) + return DECODED_SPANS.validate_python( + _native().trace_decode_otlp(body, content_type, content_encoding, max_decompressed_bytes) + ) + + +def normalized_field_definitions() -> tuple[NormalizedFieldDefinition, ...]: + fields: Final = _FIELD_DEFINITIONS_ADAPTER.validate_python(_native().trace_normalized_field_definitions()) + if frozenset(field.name for field in fields) != frozenset(NormalizedSpan.model_fields): + raise ValueError("Rust and Python normalized trace fields disagree") + return fields class TraceStorage: diff --git a/litellm/tracing/decode.py b/litellm/tracing/decode.py index ce46bed5e22d..53d617cecd7f 100644 --- a/litellm/tracing/decode.py +++ b/litellm/tracing/decode.py @@ -1,13 +1,3 @@ -""" -OTLP/HTTP trace export -> `SpanRow`s. - -Pure functions, no I/O. Two steps: -1. `decode_otlp()` protobuf / JSON / gzip `ExportTraceServiceRequest` -> flat spans -2. `normalize()` framework conventions -> LiteLLM columns (type, agent, input/output, - LiteLLM request id). Supported: LangSmith (LangChain, LangGraph, - Deep Agents), OTEL GenAI semconv, OpenInference. -""" - import json from collections.abc import Mapping from itertools import accumulate @@ -20,8 +10,6 @@ from litellm.constants import OTLP_MAX_ATTRIBUTE_VALUE_BYTES, OTLP_MAX_BODY_BYTES from litellm.rust_bridge.traces import DecodedSpan from litellm.rust_bridge.traces import decode_otlp as native_decode_otlp -from litellm.tracing.normalizers import select_normalizer -from litellm.tracing.normalizers.base import to_int from litellm.tracing.types import SpanRow _MESSAGE_LIST: Final = TypeAdapter(tuple[dict[str, JsonValue], ...]) @@ -72,7 +60,7 @@ def _elided(count: int) -> str: def _with_content(message: Mapping[str, JsonValue], content: str) -> str: - return json.dumps(MappingProxyType({**message, "content": content}), default=lambda proxy: proxy.copy()) + return json.dumps({**message, "content": content}) def _shrunk_message(message: Mapping[str, JsonValue], budget: int) -> str: @@ -159,6 +147,7 @@ def _exception_message(span: DecodedSpan) -> str: def _span_row(span: DecodedSpan) -> SpanRow: attributes = span["attributes"] resource = span["resource_attributes"] + normalized = span["normalized"] row = SpanRow( Timestamp=span["start_ns"], TraceId=span["trace_id"], @@ -177,32 +166,20 @@ def _span_row(span: DecodedSpan) -> SpanRow: StatusMessage=span["status_message"] or _exception_message(span), TeamId="", ApiKeyHash="", - ObservationType="chain", - AgentName="", - LiteLLMRequestId="", - Model="", - InputTokens=0, - OutputTokens=0, - Input="", - Output="", + ObservationType=normalized.observation_type, + AgentName=normalized.agent_name, + LiteLLMRequestId=normalized.litellm_request_id, + Model=normalized.model, + InputTokens=normalized.input_tokens, + OutputTokens=normalized.output_tokens, + Input=normalized.input, + Output=normalized.output, ) - normalize(row, attributes) row["SpanAttributes"] = {k: _truncate(v) for k, v in attributes.items() if k not in _HEAVY_ATTRIBUTES} row["Input"], row["Output"] = _truncate_payload(row["Input"]), _truncate(row["Output"]) return row -def _set_tokens(row: SpanRow, attributes: Mapping[str, str]) -> None: - row["InputTokens"] = to_int(attributes.get("gen_ai.usage.input_tokens")) - row["OutputTokens"] = to_int(attributes.get("gen_ai.usage.output_tokens")) - - -def normalize(row: SpanRow, attributes: Mapping[str, str]) -> None: - select_normalizer(row["ScopeName"], attributes).normalize(row, attributes) - if not row["InputTokens"] and not row["OutputTokens"]: - _set_tokens(row, attributes) - - def encode_otlp_response(content_type: str | None) -> tuple[bytes, str]: """Empty ExportTraceServiceResponse in the caller's encoding.""" if content_type and "json" in content_type: diff --git a/litellm/tracing/normalizers/messages.py b/litellm/tracing/messages.py similarity index 61% rename from litellm/tracing/normalizers/messages.py rename to litellm/tracing/messages.py index 8a9aa914dfd0..09f57e253089 100644 --- a/litellm/tracing/normalizers/messages.py +++ b/litellm/tracing/messages.py @@ -1,7 +1,7 @@ import json from collections.abc import Mapping from types import MappingProxyType -from typing import Any, Final, Literal, TypeAlias +from typing import Final, Literal, TypeAlias from pydantic import BaseModel, ConfigDict, TypeAdapter, ValidationError @@ -37,20 +37,3 @@ def content_text(content: object) -> str: if not all(block.text is not None or block.type in _NON_TEXT_BLOCKS for block in blocks): return json.dumps(content) return "\n\n".join(block.text for block in blocks if block.text is not None) - - -def lc_message(message: Mapping[str, Any]) -> dict[str, Any]: - """LangChain serialized message (or plain {role, content}) -> {role, content, tool_calls?}.""" - kwargs: Final = message.get("kwargs", message) - role: Final = MESSAGE_ROLES.get( - kwargs.get("type") or kwargs.get("role"), kwargs.get("role") or kwargs.get("type") or "" - ) - out: Final[dict[str, Any]] = { # mutable-ok: the framework message is built for JSON serialization - "role": role, - "content": content_text(kwargs.get("content", "")), - } - if kwargs.get("tool_calls"): - out["tool_calls"] = tuple({"name": t.get("name"), "args": t.get("args")} for t in kwargs["tool_calls"]) - if role == "tool" and kwargs.get("name"): - out["name"] = kwargs["name"] - return out diff --git a/litellm/tracing/normalizers/__init__.py b/litellm/tracing/normalizers/__init__.py deleted file mode 100644 index 2f861330a36c..000000000000 --- a/litellm/tracing/normalizers/__init__.py +++ /dev/null @@ -1,32 +0,0 @@ -"""Per-convention span normalizers, tried in order: the first whose `matches()` is true wins.""" - -from collections.abc import Mapping, Sequence -from typing import Final - -from litellm.tracing.normalizers.base import SpanNormalizer -from litellm.tracing.normalizers.genai import GenAISemconvNormalizer -from litellm.tracing.normalizers.langsmith import LangSmithNormalizer -from litellm.tracing.normalizers.openinference import OpenInferenceNormalizer - -NORMALIZERS: Final[tuple[SpanNormalizer, ...]] = ( - LangSmithNormalizer(), - OpenInferenceNormalizer(), - GenAISemconvNormalizer(), -) -_FALLBACK: Final[SpanNormalizer] = GenAISemconvNormalizer() - - -def select_normalizer( - scope_name: str, attributes: Mapping[str, str], registry: Sequence[SpanNormalizer] = NORMALIZERS -) -> SpanNormalizer: - return next((n for n in registry if n.matches(scope_name, attributes)), _FALLBACK) - - -__all__ = ( - "NORMALIZERS", - "GenAISemconvNormalizer", - "LangSmithNormalizer", - "OpenInferenceNormalizer", - "SpanNormalizer", - "select_normalizer", -) diff --git a/litellm/tracing/normalizers/base.py b/litellm/tracing/normalizers/base.py deleted file mode 100644 index 37735113ce22..000000000000 --- a/litellm/tracing/normalizers/base.py +++ /dev/null @@ -1,22 +0,0 @@ -from collections.abc import Mapping -from typing import Protocol - -from litellm.tracing.types import SpanRow - - -class SpanNormalizer(Protocol): - """Maps one tracing convention's span attributes onto the LiteLLM `SpanRow` columns.""" - - @property - def name(self) -> str: ... - - def matches(self, scope_name: str, attributes: Mapping[str, str]) -> bool: ... - - def normalize(self, row: SpanRow, attributes: Mapping[str, str]) -> None: ... - - -def to_int(value: str | None) -> int: - try: - return int(value) if value else 0 - except ValueError: - return 0 diff --git a/litellm/tracing/normalizers/genai.py b/litellm/tracing/normalizers/genai.py deleted file mode 100644 index 16986607396a..000000000000 --- a/litellm/tracing/normalizers/genai.py +++ /dev/null @@ -1,31 +0,0 @@ -from collections.abc import Mapping -from dataclasses import dataclass -from typing import Final - -from litellm.tracing.types import SpanRow - -_LLM_OPERATIONS: Final = frozenset({"chat", "text_completion", "generate_content"}) - - -@dataclass(frozen=True, slots=True) -class GenAISemconvNormalizer: - """OTEL `gen_ai.*` semantic conventions. Matches every span, so it belongs last as the fallback.""" - - name: str = "genai" - - def matches(self, scope_name: str, attributes: Mapping[str, str]) -> bool: - return True - - def normalize(self, row: SpanRow, attributes: Mapping[str, str]) -> None: - operation: Final = attributes.get("gen_ai.operation.name", "") - if operation == "invoke_agent" or not row["ParentSpanId"]: - row["ObservationType"] = "agent" - elif operation in _LLM_OPERATIONS: - row["ObservationType"] = "llm" - elif operation == "execute_tool": - row["ObservationType"] = "tool" - row["AgentName"] = attributes.get("gen_ai.agent.name", "") - row["Model"] = attributes.get("gen_ai.request.model") or attributes.get("gen_ai.response.model", "") - row["LiteLLMRequestId"] = attributes.get("gen_ai.response.id", "") - row["Input"] = attributes.get("gen_ai.input.messages") or attributes.get("gen_ai.tool.call.arguments", "") - row["Output"] = attributes.get("gen_ai.output.messages") or attributes.get("gen_ai.tool.call.result", "") diff --git a/litellm/tracing/normalizers/langsmith.py b/litellm/tracing/normalizers/langsmith.py deleted file mode 100644 index daca932d57df..000000000000 --- a/litellm/tracing/normalizers/langsmith.py +++ /dev/null @@ -1,115 +0,0 @@ -"""LangSmith OTEL mode, which LangChain, LangGraph and Deep Agents export through.""" - -import json -from collections.abc import Mapping -from dataclasses import dataclass -from types import MappingProxyType -from typing import Final - -from litellm.tracing.normalizers.messages import lc_message -from litellm.tracing.types import SpanRow, SpanType - -# LangChain / Deep Agents middleware wrappers: real spans, but noise in the UI -_FRAMEWORK_SUFFIXES: Final = ( - ".wrap_model_call", - ".wrap_tool_call", - ".before_agent", - ".after_agent", - ".before_model", - ".after_model", -) - - -def _loads(value: str) -> object: - try: - return json.loads(value) - except (ValueError, TypeError): - return None - - -def _span_type(row: SpanRow, attributes: Mapping[str, str]) -> SpanType: - kind: Final = attributes.get("langsmith.span.kind", "chain") - name: Final = row["SpanName"] - if not row["ParentSpanId"] or name == attributes.get("langsmith.metadata.lc_agent_name"): - return "agent" - if kind in ("llm", "tool"): - return kind - if name.endswith(_FRAMEWORK_SUFFIXES): - return "framework" - return "chain" - - -def _tool_output(completion: object) -> object: - raw: Final = completion.get("output", completion) if isinstance(completion, dict) else completion - update: Final = raw.get("update") if isinstance(raw, dict) else None - update_messages: Final = update.get("messages") or () if isinstance(update, dict) else () - is_command: Final = isinstance(raw, dict) and "update" in raw - # LangGraph Command (e.g. the Deep Agents `task` tool): the result is the last update message - output: Final = update_messages[-1] if is_command and update_messages else raw - return output.get("content", output) if isinstance(output, dict) else output - - -def _set_agent_io(row: SpanRow, attributes: Mapping[str, str], prompt: object, completion: object) -> None: - input_messages: Final = prompt.get("messages") if isinstance(prompt, dict) else None - output_messages: Final = completion.get("messages") if isinstance(completion, dict) else None - # agents built with @traceable take arbitrary args, not a message list: keep the raw payload then - row["Input"] = ( - json.dumps(tuple(lc_message(m) for m in input_messages if isinstance(m, dict))) - if input_messages - else attributes.get("gen_ai.prompt", "") - ) - row["Output"] = ( - json.dumps(lc_message(output_messages[-1])) - if output_messages and isinstance(output_messages[-1], dict) - else attributes.get("gen_ai.completion", "") - ) - - -def _set_io(row: SpanRow, attributes: Mapping[str, str]) -> None: - prompt: Final = _loads(attributes.get("gen_ai.prompt", "")) - completion: Final = _loads(attributes.get("gen_ai.completion", "")) - if row["ObservationType"] == "llm" and isinstance(completion, dict): - prompt_payload: Final = prompt if isinstance(prompt, dict) else MappingProxyType({}) - messages: Final = prompt_payload.get("messages") or ((),) - batch: Final = messages[0] if messages and isinstance(messages[0], list) else messages - row["Input"] = ( - json.dumps(tuple(lc_message(m) for m in batch if isinstance(m, dict))) - if isinstance(batch, (list, tuple)) - else "" - ) - generations: Final = completion.get("generations") - first: Final = generations[0] if isinstance(generations, list) and generations else None - item: Final = first[0] if isinstance(first, list) and first else None - message: Final = item.get("message") if isinstance(item, dict) else None - generation: Final = message.get("kwargs") if isinstance(message, dict) else None - if isinstance(generation, dict): - row["Output"] = json.dumps(lc_message(generation)) - metadata: Final = generation.get("response_metadata") - row["LiteLLMRequestId"] = metadata.get("id", "") if isinstance(metadata, dict) else "" - return - row["Output"] = attributes.get("gen_ai.completion", "") - return - if row["ObservationType"] == "tool": - output: Final = _tool_output(completion) - row["Input"] = attributes.get("gen_ai.prompt", "") - row["Output"] = output if isinstance(output, str) else json.dumps(output) - return - if row["ObservationType"] == "agent": - _set_agent_io(row, attributes, prompt, completion) - return - row["Input"] = attributes.get("gen_ai.prompt", "") - row["Output"] = attributes.get("gen_ai.completion", "") - - -@dataclass(frozen=True, slots=True) -class LangSmithNormalizer: - name: str = "langsmith" - - def matches(self, scope_name: str, attributes: Mapping[str, str]) -> bool: - return scope_name == "langsmith" or "langsmith.span.kind" in attributes - - def normalize(self, row: SpanRow, attributes: Mapping[str, str]) -> None: - row["ObservationType"] = _span_type(row, attributes) - row["AgentName"] = attributes.get("langsmith.metadata.lc_agent_name", "") - row["Model"] = attributes.get("gen_ai.request.model", "") - _set_io(row, attributes) diff --git a/litellm/tracing/normalizers/openinference.py b/litellm/tracing/normalizers/openinference.py deleted file mode 100644 index f9e1295148c4..000000000000 --- a/litellm/tracing/normalizers/openinference.py +++ /dev/null @@ -1,27 +0,0 @@ -from collections.abc import Mapping -from dataclasses import dataclass -from types import MappingProxyType -from typing import Final - -from litellm.tracing.normalizers.base import to_int -from litellm.tracing.types import SpanRow, SpanType - -_OPENINFERENCE_TYPES: Final[Mapping[str, SpanType]] = MappingProxyType({"AGENT": "agent", "LLM": "llm", "TOOL": "tool"}) - - -@dataclass(frozen=True, slots=True) -class OpenInferenceNormalizer: - name: str = "openinference" - - def matches(self, scope_name: str, attributes: Mapping[str, str]) -> bool: - return "openinference.span.kind" in attributes - - def normalize(self, row: SpanRow, attributes: Mapping[str, str]) -> None: - kind: Final = attributes.get("openinference.span.kind", "").upper() - row["ObservationType"] = _OPENINFERENCE_TYPES.get(kind, "agent" if not row["ParentSpanId"] else "chain") - row["AgentName"] = attributes.get("agent.name", "") - row["Model"] = attributes.get("llm.model_name", "") - row["Input"] = attributes.get("input.value", "") - row["Output"] = attributes.get("output.value", "") - row["InputTokens"] = to_int(attributes.get("llm.token_count.prompt")) - row["OutputTokens"] = to_int(attributes.get("llm.token_count.completion")) diff --git a/litellm/tracing/ui_format.py b/litellm/tracing/ui_format.py index d7ecf48078f1..51ec2876fbbc 100644 --- a/litellm/tracing/ui_format.py +++ b/litellm/tracing/ui_format.py @@ -7,7 +7,7 @@ from pydantic import BaseModel, ConfigDict, JsonValue, TypeAdapter, ValidationError from typing_extensions import NotRequired, ReadOnly, TypedDict -from litellm.tracing.normalizers.messages import MESSAGE_ROLES, ChatRole, content_text +from litellm.tracing.messages import MESSAGE_ROLES, ChatRole, content_text class UIToolCall(TypedDict): diff --git a/tests/test_litellm/tracing/normalizers/test_registry.py b/tests/test_litellm/tracing/normalizers/test_registry.py deleted file mode 100644 index 4c2fd051d6cd..000000000000 --- a/tests/test_litellm/tracing/normalizers/test_registry.py +++ /dev/null @@ -1,68 +0,0 @@ -from collections.abc import Mapping -from dataclasses import dataclass -from types import MappingProxyType -from typing import Final - -from litellm.tracing.normalizers import ( - NORMALIZERS, - GenAISemconvNormalizer, - LangSmithNormalizer, - OpenInferenceNormalizer, - select_normalizer, -) -from litellm.tracing.types import SpanRow - -_NO_ATTRIBUTES: Final[Mapping[str, str]] = MappingProxyType({}) - - -def test_langsmith_scope_selects_langsmith_without_any_attributes(): - assert isinstance(select_normalizer("langsmith", _NO_ATTRIBUTES), LangSmithNormalizer) - - -def test_langsmith_kind_attribute_selects_langsmith_under_any_scope(): - assert isinstance(select_normalizer("other", MappingProxyType({"langsmith.span.kind": "llm"})), LangSmithNormalizer) - - -def test_langsmith_wins_over_openinference_when_both_markers_present(): - attributes: Final = MappingProxyType({"langsmith.span.kind": "llm", "openinference.span.kind": "LLM"}) - assert isinstance(select_normalizer("other", attributes), LangSmithNormalizer) - - -def test_openinference_kind_attribute_selects_openinference(): - assert isinstance( - select_normalizer("other", MappingProxyType({"openinference.span.kind": "LLM"})), OpenInferenceNormalizer - ) - - -def test_unmarked_span_falls_back_to_genai(): - assert isinstance( - select_normalizer("other", MappingProxyType({"gen_ai.operation.name": "chat"})), GenAISemconvNormalizer - ) - - -def test_empty_registry_falls_back_to_genai(): - assert isinstance(select_normalizer("langsmith", _NO_ATTRIBUTES, registry=()), GenAISemconvNormalizer) - - -def test_registry_names_are_unique(): - names: Final = tuple(n.name for n in NORMALIZERS) - assert len(names) == len(frozenset(names)) - - -@dataclass(frozen=True, slots=True) -class _CustomNormalizer: - name: str = "custom" - - def matches(self, scope_name: str, attributes: Mapping[str, str]) -> bool: - return scope_name == "custom-sdk" - - def normalize(self, row: SpanRow, attributes: Mapping[str, str]) -> None: - return None - - -def test_normalizer_inserted_ahead_in_custom_registry_wins_only_where_it_matches(): - registry: Final = (_CustomNormalizer(), *NORMALIZERS) - assert isinstance( - select_normalizer("custom-sdk", MappingProxyType({"langsmith.span.kind": "llm"}), registry), _CustomNormalizer - ) - assert isinstance(select_normalizer("langsmith", _NO_ATTRIBUTES, registry), LangSmithNormalizer) From 8d939e810602bde043889a448e110d277834c69c Mon Sep 17 00:00:00 2001 From: Yujong Lee Date: Thu, 1 Oct 2026 13:55:55 -0700 Subject: [PATCH 2/7] refactor(tracing): generate dashboard trace types from API --- litellm/proxy/tracing_endpoints.py | 6 +- .../view_logs/TraceView/DetailPane.test.tsx | 1 + .../view_logs/TraceView/RunDrawer.test.tsx | 2 + .../view_logs/TraceView/traceTypes.ts | 127 ++--------- .../view_logs/TraceView/traceUtils.test.ts | 1 + ui/litellm-dashboard/src/lib/http/schema.d.ts | 197 +++++++++++++++++- 6 files changed, 218 insertions(+), 116 deletions(-) diff --git a/litellm/proxy/tracing_endpoints.py b/litellm/proxy/tracing_endpoints.py index b2b10ebfbc6a..c963bab2fcde 100644 --- a/litellm/proxy/tracing_endpoints.py +++ b/litellm/proxy/tracing_endpoints.py @@ -97,7 +97,7 @@ async def ingest_otlp_traces( return Response(content=body, media_type=media_type) -@router.get("/v1/traces", response_model=None) +@router.get("/v1/traces", response_model=TracePage) async def list_agent_traces( user_api_key_dict: Annotated[UserAPIKeyAuth, Depends(user_api_key_auth)], start_ms: Annotated[int | None, Query(description="Window start, unix ms. Default: 24h ago")] = None, @@ -116,7 +116,7 @@ async def list_agent_traces( raise HTTPException(status_code=400, detail=str(error)) from error -@router.get("/v1/traces/{trace_id}", response_model=None) +@router.get("/v1/traces/{trace_id}", response_model=Trace) async def get_agent_trace( trace_id: str, user_api_key_dict: Annotated[UserAPIKeyAuth, Depends(user_api_key_auth)], @@ -128,7 +128,7 @@ async def get_agent_trace( return trace -@router.get("/v1/traces/{trace_id}/spans/{span_id}", response_model=None) +@router.get("/v1/traces/{trace_id}/spans/{span_id}", response_model=SpanDetail) async def get_agent_trace_span( trace_id: str, span_id: str, diff --git a/ui/litellm-dashboard/src/components/view_logs/TraceView/DetailPane.test.tsx b/ui/litellm-dashboard/src/components/view_logs/TraceView/DetailPane.test.tsx index 1adcda0f6cc8..06fee682276f 100644 --- a/ui/litellm-dashboard/src/components/view_logs/TraceView/DetailPane.test.tsx +++ b/ui/litellm-dashboard/src/components/view_logs/TraceView/DetailPane.test.tsx @@ -31,6 +31,7 @@ const span = (overrides: SpanFields): Span => ({ input_tokens: 0, output_tokens: 0, litellm_request_id: null, + spend: null, ...overrides, }); diff --git a/ui/litellm-dashboard/src/components/view_logs/TraceView/RunDrawer.test.tsx b/ui/litellm-dashboard/src/components/view_logs/TraceView/RunDrawer.test.tsx index 5ec0df55dab5..8f205de441a0 100644 --- a/ui/litellm-dashboard/src/components/view_logs/TraceView/RunDrawer.test.tsx +++ b/ui/litellm-dashboard/src/components/view_logs/TraceView/RunDrawer.test.tsx @@ -19,12 +19,14 @@ const run = (trace_id: string): TraceSummary => ({ status: "ok", span_count: 1, agent_count: 1, + agent_invocations: 1, llm_calls: 0, tool_calls: 0, error_count: 0, input_tokens: 0, output_tokens: 0, models: [], + spend: null, }); const mockReducedMotion = (reduce: boolean) => diff --git a/ui/litellm-dashboard/src/components/view_logs/TraceView/traceTypes.ts b/ui/litellm-dashboard/src/components/view_logs/TraceView/traceTypes.ts index 4711799bd1a6..66a450d61bdc 100644 --- a/ui/litellm-dashboard/src/components/view_logs/TraceView/traceTypes.ts +++ b/ui/litellm-dashboard/src/components/view_logs/TraceView/traceTypes.ts @@ -1,113 +1,20 @@ -/** - * Agent tracing types. Mirrors `litellm/tracing/types.py` exactly. - * - * A trace is one agent run made of spans (agent / llm / tool / chain / framework). - */ - -export type SpanType = "agent" | "llm" | "tool" | "chain" | "framework"; -export type SpanStatus = "ok" | "error" | "unset"; - -export interface Span { - span_id: string; - parent_span_id: string | null; - name: string; - type: SpanType; - /** The agent this span runs inside, e.g. "researcher". */ - agent: string; - /** Relative to trace start. */ - start_offset_ms: number; - duration_ms: number; - status: SpanStatus; - /** Exception message when status is "error". */ - error?: string | null; - input_preview: string; - model: string | null; - input_tokens: number; - output_tokens: number; - litellm_request_id: string | null; - spend?: number | null; -} - -/** One distinct agent in a trace. 200 invocations of `researcher` = one node. */ -export interface AgentNode { - name: string; - parent_agent: string | null; - invocations: number; - llm_calls: number; - tool_calls: number; - duration_ms: number; - spend?: number | null; -} - -export interface TraceSummary { - trace_id: string; - trace_ref?: string; - name: string; - service: string; - input_preview: string; - /** ISO 8601 */ - start_time: string; - duration_ms: number; - status: SpanStatus; - span_count: number; - agent_count: number; - llm_calls: number; - tool_calls: number; - /** Spans with an error status; > 0 means the run shows as failed. */ - error_count: number; - input_tokens: number; - output_tokens: number; - models: string[]; - spend?: number | null; -} - -export interface Trace { - summary: TraceSummary; - agents: AgentNode[]; - spans: Span[]; -} - -export interface TracePage { - data: TraceSummary[]; - next_cursor: string | null; -} - -/** Mirrors `litellm/tracing/ui_format.py`: span content already reduced to what the UI renders. */ -export interface UIToolCall { - name: string; - /** JSON-encoded arguments. */ - arguments: string; -} - -export interface UIMessage { - role: "system" | "user" | "assistant" | "tool"; - content: string; - name?: string; - tool_calls?: UIToolCall[]; -} - -export interface UIField { - key: string; - value: string; -} - -export type UIContent = - | { kind: "messages"; messages: UIMessage[] } - | { kind: "fields"; fields: UIField[] } - | { kind: "text"; text: string }; - -/** - * `input` / `output` are the raw stored JSON strings. `input_ui` / `output_ui` are the - * standard rendering; optional because older proxies don't send them. - */ -export interface SpanDetail { - span_id: string; - input: string; - output: string; - input_ui?: UIContent; - output_ui?: UIContent; - attributes: Record; -} +import type { components, paths } from "@/lib/http/schema"; + +export type Trace = paths["/v1/traces/{trace_id}"]["get"]["responses"][200]["content"]["application/json"]; +export type TracePage = paths["/v1/traces"]["get"]["responses"][200]["content"]["application/json"]; +type ApiSpanDetail = + paths["/v1/traces/{trace_id}/spans/{span_id}"]["get"]["responses"][200]["content"]["application/json"]; +export type Span = Trace["spans"][number]; +export type SpanType = Span["type"]; +export type SpanStatus = Span["status"]; +export type AgentNode = Trace["agents"][number]; +export type TraceSummary = Trace["summary"]; +export type SpanDetail = Omit & + Partial>; +export type UIToolCall = components["schemas"]["UIToolCall"]; +export type UIMessage = components["schemas"]["UIMessage"]; +export type UIField = components["schemas"]["UIField"]; +export type UIContent = ApiSpanDetail["input_ui"]; export interface TraceToolCall { name: string; diff --git a/ui/litellm-dashboard/src/components/view_logs/TraceView/traceUtils.test.ts b/ui/litellm-dashboard/src/components/view_logs/TraceView/traceUtils.test.ts index 878ac58ab2ff..9353f2024c8d 100644 --- a/ui/litellm-dashboard/src/components/view_logs/TraceView/traceUtils.test.ts +++ b/ui/litellm-dashboard/src/components/view_logs/TraceView/traceUtils.test.ts @@ -50,6 +50,7 @@ const span = (overrides: SpanOverrides): Span => ({ input_tokens: 0, output_tokens: 0, litellm_request_id: null, + spend: null, ...overrides, }); diff --git a/ui/litellm-dashboard/src/lib/http/schema.d.ts b/ui/litellm-dashboard/src/lib/http/schema.d.ts index fcb2805c767c..bd72b2c09245 100644 --- a/ui/litellm-dashboard/src/lib/http/schema.d.ts +++ b/ui/litellm-dashboard/src/lib/http/schema.d.ts @@ -24961,6 +24961,26 @@ export interface components { /** Updated By */ updated_by: string; }; + /** + * AgentNode + * @description One distinct agent in a trace. 200 invocations of `researcher` = one node. + */ + AgentNode: { + /** Duration Ms */ + duration_ms: number; + /** Invocations */ + invocations: number; + /** Llm Calls */ + llm_calls: number; + /** Name */ + name: string; + /** Parent Agent */ + parent_agent: string | null; + /** Spend */ + spend: number | null; + /** Tool Calls */ + tool_calls: number; + }; /** AgentObjectPermission */ AgentObjectPermission: { /** Agents */ @@ -44114,6 +44134,62 @@ export interface components { /** Version */ version?: string; }; + /** Span */ + Span: { + /** Agent */ + agent: string; + /** Duration Ms */ + duration_ms: number; + /** Error */ + error: string | null; + /** Input Preview */ + input_preview: string; + /** Input Tokens */ + input_tokens: number; + /** Litellm Request Id */ + litellm_request_id: string | null; + /** Model */ + model: string | null; + /** Name */ + name: string; + /** Output Tokens */ + output_tokens: number; + /** Parent Span Id */ + parent_span_id: string | null; + /** Span Id */ + span_id: string; + /** Spend */ + spend: number | null; + /** Start Offset Ms */ + start_offset_ms: number; + /** + * Status + * @enum {string} + */ + status: "ok" | "error" | "unset"; + /** + * Type + * @enum {string} + */ + type: "agent" | "llm" | "tool" | "chain" | "framework"; + }; + /** SpanDetail */ + SpanDetail: { + /** Attributes */ + attributes: { + [key: string]: string; + }; + /** Input */ + input: string; + /** Input Ui */ + input_ui: components["schemas"]["UIMessages"] | components["schemas"]["UIFields"] | components["schemas"]["UIText"]; + /** Output */ + output: string; + /** Output Ui */ + output_ui: components["schemas"]["UIMessages"] | components["schemas"]["UIFields"] | components["schemas"]["UIText"]; + /** Span Id */ + span_id: string; + }; /** SpendAnalyticsPaginatedResponse */ SpendAnalyticsPaginatedResponse: { metadata?: components["schemas"]["DailySpendMetadata"]; @@ -45906,6 +45982,21 @@ export interface components { } & { [key: string]: unknown; }; + /** Trace */ + Trace: { + /** Agents */ + agents: components["schemas"]["AgentNode"][]; + /** Spans */ + spans: components["schemas"]["Span"][]; + summary: components["schemas"]["TraceSummary"]; + }; + /** TracePage */ + TracePage: { + /** Data */ + data: components["schemas"]["TraceSummary"][]; + /** Next Cursor */ + next_cursor: string | null; + }; /** TracePart */ TracePart: { /** Content */ @@ -45929,6 +46020,48 @@ export interface components { */ truncated: boolean; }; + /** TraceSummary */ + TraceSummary: { + /** Agent Count */ + agent_count: number; + /** Agent Invocations */ + agent_invocations: number; + /** Duration Ms */ + duration_ms: number; + /** Error Count */ + error_count: number; + /** Input Preview */ + input_preview: string; + /** Input Tokens */ + input_tokens: number; + /** Llm Calls */ + llm_calls: number; + /** Models */ + models: string[]; + /** Name */ + name: string; + /** Output Tokens */ + output_tokens: number; + /** Service */ + service: string; + /** Span Count */ + span_count: number; + /** Spend */ + spend: number | null; + /** Start Time */ + start_time: string; + /** + * Status + * @enum {string} + */ + status: "ok" | "error" | "unset"; + /** Tool Calls */ + tool_calls: number; + /** Trace Id */ + trace_id: string; + /** Trace Ref */ + trace_ref?: string; + }; /** TrainedTierArtifact */ TrainedTierArtifact: { /** @@ -46003,6 +46136,47 @@ export interface components { } & { [key: string]: unknown; }; + /** UIField */ + UIField: { + /** Key */ + key: string; + /** Value */ + value: string; + }; + /** UIFields */ + UIFields: { + /** Fields */ + fields: components["schemas"]["UIField"][]; + /** + * Kind + * @constant + */ + kind: "fields"; + }; + /** UIMessage */ + UIMessage: { + /** Content */ + content: string; + /** Name */ + name?: string; + /** + * Role + * @enum {string} + */ + role: "system" | "user" | "assistant" | "tool"; + /** Tool Calls */ + tool_calls?: components["schemas"]["UIToolCall"][]; + }; + /** UIMessages */ + UIMessages: { + /** + * Kind + * @constant + */ + kind: "messages"; + /** Messages */ + messages: components["schemas"]["UIMessage"][]; + }; /** * UISettingsResponse * @description Response model for UI settings @@ -46021,6 +46195,16 @@ export interface components { [key: string]: unknown; }; }; + /** UIText */ + UIText: { + /** + * Kind + * @constant + */ + kind: "text"; + /** Text */ + text: string; + }; /** * UIThemeConfig * @description Configuration for UI theme customization @@ -46056,6 +46240,13 @@ export interface components { [key: string]: unknown; }; }; + /** UIToolCall */ + UIToolCall: { + /** Arguments */ + arguments: string; + /** Name */ + name: string; + }; /** UiDiscoveryEndpoints */ UiDiscoveryEndpoints: { /** Admin Ui Disabled */ @@ -77224,7 +77415,7 @@ export interface operations { [name: string]: unknown; }; content: { - "application/json": unknown; + "application/json": components["schemas"]["TracePage"]; }; }; /** @description Validation Error */ @@ -77277,7 +77468,7 @@ export interface operations { [name: string]: unknown; }; content: { - "application/json": unknown; + "application/json": components["schemas"]["Trace"]; }; }; /** @description Validation Error */ @@ -77311,7 +77502,7 @@ export interface operations { [name: string]: unknown; }; content: { - "application/json": unknown; + "application/json": components["schemas"]["SpanDetail"]; }; }; /** @description Validation Error */ From 68b3f1bcd60542ef219f611753440246e7b4e7bb Mon Sep 17 00:00:00 2001 From: Yujong Lee Date: Thu, 1 Oct 2026 14:22:56 -0700 Subject: [PATCH 3/7] test(tracing): use complete trace response fixtures --- tests/unit/proxy/test_tracing_endpoints.py | 40 +++++++++++++++++++--- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/tests/unit/proxy/test_tracing_endpoints.py b/tests/unit/proxy/test_tracing_endpoints.py index aa1403b8db96..287092c9f912 100644 --- a/tests/unit/proxy/test_tracing_endpoints.py +++ b/tests/unit/proxy/test_tracing_endpoints.py @@ -23,6 +23,37 @@ TEAM_KEY = UserAPIKeyAuth( token="hashed-key", team_id="team-research", org_id="org-1", user_role=LitellmUserRoles.INTERNAL_USER ) +TRACE_RESPONSE: Final = { + "summary": { + "trace_id": "t1", + "name": "trace", + "service": "test", + "input_preview": "", + "start_time": "2026-01-01T00:00:00Z", + "duration_ms": 0, + "status": "ok", + "span_count": 0, + "agent_count": 0, + "agent_invocations": 0, + "llm_calls": 0, + "tool_calls": 0, + "error_count": 0, + "input_tokens": 0, + "output_tokens": 0, + "models": [], + "spend": None, + }, + "agents": [], + "spans": [], +} +SPAN_DETAIL_RESPONSE: Final = { + "span_id": "s1", + "input": "", + "output": "", + "input_ui": {"kind": "text", "text": ""}, + "output_ui": {"kind": "text", "text": ""}, + "attributes": {}, +} @pytest.mark.parametrize( @@ -171,17 +202,16 @@ def test_list_traces_defaults_to_last_24h(client, receiver): def test_get_trace_404_and_200(client, receiver): assert client.get("/v1/traces/missing").status_code == 404 - trace = {"summary": {"trace_id": "t1"}, "agents": [], "spans": []} - receiver.get_trace.return_value = trace + receiver.get_trace.return_value = TRACE_RESPONSE response = client.get("/v1/traces/t1") assert response.status_code == 200 - assert response.json() == trace + assert response.json() == TRACE_RESPONSE receiver.get_trace.assert_awaited_with("t1", {"team_ids": ("team-research",), "api_key_hash": ""}, "") def test_get_span_404_and_200(client, receiver): assert client.get("/v1/traces/t1/spans/s1").status_code == 404 - receiver.get_span.return_value = {"span_id": "s1", "input": "", "output": "", "attributes": {}} + receiver.get_span.return_value = SPAN_DETAIL_RESPONSE response = client.get("/v1/traces/t1/spans/s1") assert response.status_code == 200 assert response.json()["span_id"] == "s1" @@ -207,7 +237,7 @@ def test_get_span_serves_ui_content_from_stored_payloads(client): def test_trace_detail_passes_scoped_reference(client, receiver): - receiver.get_trace.return_value = {"summary": {"trace_id": "t1"}, "agents": [], "spans": []} + receiver.get_trace.return_value = TRACE_RESPONSE assert client.get("/v1/traces/t1?trace_ref=run-one").status_code == 200 receiver.get_trace.assert_awaited_with("t1", {"team_ids": ("team-research",), "api_key_hash": ""}, "run-one") From c4f506ad6cda09c17c9c2aec257f13183ed5e214 Mon Sep 17 00:00:00 2001 From: Yujong Lee Date: Thu, 1 Oct 2026 14:33:43 -0700 Subject: [PATCH 4/7] fix(ui): expose generated span error response type --- .../src/components/view_logs/TraceView/traceTypes.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ui/litellm-dashboard/src/components/view_logs/TraceView/traceTypes.ts b/ui/litellm-dashboard/src/components/view_logs/TraceView/traceTypes.ts index 66a450d61bdc..390a296aa001 100644 --- a/ui/litellm-dashboard/src/components/view_logs/TraceView/traceTypes.ts +++ b/ui/litellm-dashboard/src/components/view_logs/TraceView/traceTypes.ts @@ -2,6 +2,8 @@ import type { components, paths } from "@/lib/http/schema"; export type Trace = paths["/v1/traces/{trace_id}"]["get"]["responses"][200]["content"]["application/json"]; export type TracePage = paths["/v1/traces"]["get"]["responses"][200]["content"]["application/json"]; +export type SpanErrorPage = + paths["/v1/traces/{trace_id}/spans/{span_id}/error"]["get"]["responses"][200]["content"]["application/json"]; type ApiSpanDetail = paths["/v1/traces/{trace_id}/spans/{span_id}"]["get"]["responses"][200]["content"]["application/json"]; export type Span = Trace["spans"][number]; From b48343e3ff6ba9c587fd7585696e5080c82cfa9f Mon Sep 17 00:00:00 2001 From: Yujong Lee Date: Thu, 1 Oct 2026 14:39:12 -0700 Subject: [PATCH 5/7] fix(tracing): retain full tool call payloads --- litellm-rust/Cargo.lock | 1 + litellm-rust/crates/traces/Cargo.toml | 1 + litellm-rust/crates/traces/src/normalize/langsmith.rs | 7 +++---- litellm-rust/crates/traces/tests/otlp.rs | 2 ++ 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/litellm-rust/Cargo.lock b/litellm-rust/Cargo.lock index ff0eafee47ee..ed1aba818426 100644 --- a/litellm-rust/Cargo.lock +++ b/litellm-rust/Cargo.lock @@ -4387,6 +4387,7 @@ dependencies = [ "base64 0.22.1", "criterion", "flate2", + "indexmap 2.14.0", "litellm-http", "litellm-storage-clickhouse", "opentelemetry-proto", diff --git a/litellm-rust/crates/traces/Cargo.toml b/litellm-rust/crates/traces/Cargo.toml index 74de400764c6..f3f9cf73083f 100644 --- a/litellm-rust/crates/traces/Cargo.toml +++ b/litellm-rust/crates/traces/Cargo.toml @@ -8,6 +8,7 @@ repository.workspace = true [dependencies] base64.workspace = true flate2.workspace = true +indexmap = { version = "2", features = ["serde"] } opentelemetry-proto = { workspace = true, features = ["gen-tonic-messages", "trace", "with-serde"] } prost.workspace = true time = { workspace = true, features = ["formatting"] } diff --git a/litellm-rust/crates/traces/src/normalize/langsmith.rs b/litellm-rust/crates/traces/src/normalize/langsmith.rs index e68208056019..bfe7a2c17965 100644 --- a/litellm-rust/crates/traces/src/normalize/langsmith.rs +++ b/litellm-rust/crates/traces/src/normalize/langsmith.rs @@ -1,5 +1,6 @@ use std::{collections::BTreeMap, io}; +use indexmap::IndexMap; use serde::{Deserialize, Deserializer, Serialize, de::DeserializeOwned}; use serde_json::{Value, ser::Formatter}; @@ -59,10 +60,8 @@ enum HiddenBlock { } #[derive(Deserialize, Serialize)] -struct RawToolCall { - name: Option, - args: Option, -} +#[serde(transparent)] +struct RawToolCall(IndexMap); #[derive(Deserialize)] struct ResponseMetadata { diff --git a/litellm-rust/crates/traces/tests/otlp.rs b/litellm-rust/crates/traces/tests/otlp.rs index a0161759bfed..3baa820d312e 100644 --- a/litellm-rust/crates/traces/tests/otlp.rs +++ b/litellm-rust/crates/traces/tests/otlp.rs @@ -368,6 +368,8 @@ fn normalizes_langsmith_fixture() { serde_json::from_str(&llm.normalized.output).expect("message output"); assert_eq!(output["role"], "assistant"); assert!(output["tool_calls"][0]["name"].is_string()); + assert!(output["tool_calls"][0]["id"].is_string()); + assert_eq!(output["tool_calls"][0]["type"], "tool_call"); let root = spans .iter() .find(|span| span.name == "deep_research_agent") From df06d36652d0ad935661d3825cab998ceb2bff82 Mon Sep 17 00:00:00 2001 From: Yujong Lee Date: Thu, 1 Oct 2026 14:54:24 -0700 Subject: [PATCH 6/7] fix(tracing): preserve decoded attribute tuple shape --- litellm-rust/crates/python-bridge/src/routes/traces.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/litellm-rust/crates/python-bridge/src/routes/traces.rs b/litellm-rust/crates/python-bridge/src/routes/traces.rs index 9bbb0253458b..06d3f987f270 100644 --- a/litellm-rust/crates/python-bridge/src/routes/traces.rs +++ b/litellm-rust/crates/python-bridge/src/routes/traces.rs @@ -240,7 +240,10 @@ fn spans_to_py<'py>( "normalized", litellm_host_python::Pythonized(&span.normalized).into_pyobject(py)?, )?; - row.set_item("consumed_attributes", span.consumed_attributes)?; + row.set_item( + "consumed_attributes", + litellm_host_python::Pythonized(&span.consumed_attributes).into_pyobject(py)?, + )?; result.append(row)?; } Ok(result) From 246bf316654b0d6c21b2c5986394aa8546b74fd9 Mon Sep 17 00:00:00 2001 From: Yujong Lee Date: Thu, 1 Oct 2026 15:17:11 -0700 Subject: [PATCH 7/7] fix(tracing): type consumed attributes as tuple --- litellm/rust_bridge/traces.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/litellm/rust_bridge/traces.py b/litellm/rust_bridge/traces.py index e14cc7b1e3aa..7fc1ef2eb383 100644 --- a/litellm/rust_bridge/traces.py +++ b/litellm/rust_bridge/traces.py @@ -52,7 +52,7 @@ class DecodedSpan(TypedDict): status_message: ReadOnly[str] events: ReadOnly[list[DecodedEvent]] normalized: ReadOnly[NormalizedSpan] - consumed_attributes: ReadOnly[list[str]] + consumed_attributes: ReadOnly[tuple[str, str]] ReadQueryName = Literal["list_traces", "trace_spans", "span_detail", "span_error", "spend_by_response_ids"]