SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.
TypeScript apmapplication-monitoringdistributed-tracinggogood-first-issuejaegerloglogsmetricsmonitoringnextjsobservabilityopen-sourceopentelemetryprometheusreactreactjsself-hostedtracingtypescript
fix(tracedetail): keep litellm tool calls and AI SDK JSON answers, mark redacted media (#13166)
Nityananda Gohain531504c52385
diff --git a/pkg/types/aiobservabilitytypes/message_format.go b/pkg/types/aiobservabilitytypes/message_format.goindex 1a40842475..e5235a40dc 100644--- a/pkg/types/aiobservabilitytypes/message_format.go+++ b/pkg/types/aiobservabilitytypes/message_format.go@@ -24,7 +24,10 @@ const (FormatterGeneric = "generic")-var dataURL = regexp.MustCompile(`^data:([^;,]+);base64,(.+)$`)+var (+ dataURL = regexp.MustCompile(`^data:([^;,]+);base64,(.+)$`)+ urlScheme = regexp.MustCompile(`^[A-Za-z][A-Za-z0-9+.-]*:`)+)// Provider spellings of the finish reasons; the enum values themselves pass through. tool_calls and// function_call are OpenAI's, completed, failed, cancelled, incomplete and max_output_tokens the Responses@@ -77,6 +80,7 @@ func FormatMessages(attributes map[string]any) FormattedMessages {}f.prependSystemInstructions()f.fillFinishReasons()+ f.genericMedia()return f.out}@@ -268,12 +272,6 @@ func indexed(doc map[string]any, key string) []map[string]any {return out}-// indexedAt reads one entry of a flattened list by its index.-func indexedAt(doc map[string]any, key string, i int) map[string]any {- m, _ := attribute(doc, key+"."+strconv.Itoa(i)).(map[string]any)- return m-}-func firstObject(list []any) (map[string]any, bool) {if len(list) == 0 {return nil, false@@ -390,6 +388,24 @@ func textPart(content string) TextPart {return TextPart{Type: PartTypeText, Content: content}}+func hasToolCall(parts Parts) bool {+ for _, p := range parts {+ if _, ok := p.Value.(ToolCallRequestPart); ok {+ return true+ }+ }+ return false+}++func onlyText(parts Parts) bool {+ for _, p := range parts {+ if _, ok := p.Value.(TextPart); !ok {+ return false+ }+ }+ return true+}+// textOf joins the text parts, for comparing a system message with gen_ai.system_instructions.func textOf(parts Parts) string {var texts []string@@ -513,14 +529,46 @@ func modalityOf(mimeType string) Modality {return ModalityDocument}-// mediaPart places a data URL in a BlobPart, any other URL in a UriPart.+// mediaPart places a data URL in a BlobPart and any other URL in a UriPart. A value without a URL scheme,+// such as a redaction placeholder, is kept as media without a URL.func mediaPart(url string, modality Modality) any {if m := dataURL.FindStringSubmatch(url); m != nil {return BlobPart{Type: PartTypeBlob, Modality: modality, MimeType: &m[1], Content: m[2]}}+ if !urlScheme.MatchString(url) {+ return mediaWithoutURL{"type": FormatterGeneric, "modality": modality.StringValue(), "content": url}+ }return UriPart{Type: PartTypeURI, Modality: modality, URI: url}}+// mediaWithoutURL is a generic part that FormatMessages warns about once the converters ran.+type mediaWithoutURL GenericPart++// genericMedia turns media without a URL into generic parts, with one warning.+func (f *formatting) genericMedia() {+ found := false+ for i := range f.out.Input {+ found = genericMediaParts(f.out.Input[i].Parts) || found+ }+ for i := range f.out.Output {+ found = genericMediaParts(f.out.Output[i].Parts) || found+ }+ if found {+ f.warn("media without a URL kept as generic")+ }+}++func genericMediaParts(parts Parts) bool {+ found := false+ for i, p := range parts {+ if media, ok := p.Value.(mediaWithoutURL); ok {+ parts[i] = part(GenericPart(media))+ found = true+ }+ }+ return found+}+// stringOf renders nil as "" and non-strings as compact JSON.func stringOf(value any) string {switch v := value.(type) {diff --git a/pkg/types/aiobservabilitytypes/message_format_openai_chat_test.go b/pkg/types/aiobservabilitytypes/message_format_openai_chat_test.goindex ec64b127fe..9e3eece652 100644--- a/pkg/types/aiobservabilitytypes/message_format_openai_chat_test.go+++ b/pkg/types/aiobservabilitytypes/message_format_openai_chat_test.go@@ -214,5 +214,16 @@ func TestFormat_OpenAIChat(t *testing.T) {{"role": "assistant", "parts": [{"type": "text", "content": "hi"}], "finish_reason": "stop"}]`,},+ {+ // openrouter-gateway capture: OpenRouter replaces the image with a placeholder, which is no URL+ name: "RedactedImageURL_GenericPart_Warns",+ input: `{"messages":[{"role":"user","content":[{"type":"text","text":"What animal is in this image? One sentence."},{"type":"image_url","image_url":{"url":"[OR_REDACTED]"}}]}]}`,+ warnings: []string{"media without a URL kept as generic"},+ wantInput: `[{"role": "user", "parts": [+ {"type": "text", "content": "What animal is in this image? One sentence."},+ {"type": "generic", "modality": "image", "content": "[OR_REDACTED]"}+ ]}]`,+ wantOutput: `[]`,+ },})}diff --git a/pkg/types/aiobservabilitytypes/message_format_openinference_test.go b/pkg/types/aiobservabilitytypes/message_format_openinference_test.goindex 156f0febd8..7901cc5b9f 100644--- a/pkg/types/aiobservabilitytypes/message_format_openinference_test.go+++ b/pkg/types/aiobservabilitytypes/message_format_openinference_test.go@@ -320,5 +320,18 @@ func TestFormat_OpenInference(t *testing.T) {wantInput: `[{"role": "tool", "name": "multiply", "parts": [{"type": "tool_call_response", "id": "call_62136355", "response": "2001"}]}]`,wantOutput: `[]`,},+ {+ // OpenInference replaces a base64 image over 32,000 characters with __REDACTED__:+ // https://github.com/Arize-ai/openinference/blob/12f2cc035edfb00b60b656d977f07abfb7a69900/python/openinference-instrumentation/src/openinference/instrumentation/config.py#L431+ name: "RedactedImage_GenericPart_Warns",+ attributes: map[string]any{+ "llm.input_messages.0.message.role": "user",+ "llm.input_messages.0.message.contents.0.message_content.type": "image",+ "llm.input_messages.0.message.contents.0.message_content.image.image.url": "__REDACTED__",+ },+ warnings: []string{"media without a URL kept as generic"},+ wantInput: `[{"role": "user", "parts": [{"type": "generic", "modality": "image", "content": "__REDACTED__"}]}]`,+ wantOutput: `[]`,+ },})}diff --git a/pkg/types/aiobservabilitytypes/message_format_semconv.go b/pkg/types/aiobservabilitytypes/message_format_semconv.goindex dd286d67d7..9a204e599c 100644--- a/pkg/types/aiobservabilitytypes/message_format_semconv.go+++ b/pkg/types/aiobservabilitytypes/message_format_semconv.go@@ -12,8 +12,10 @@ import (// writes them:// https://github.com/pydantic/pydantic-ai/blob/9ac853bb84ffed7ba0edccb026c5b4f3228c1de7/pydantic_ai_slim/pydantic_ai/messages.py#L3130// https://github.com/pydantic/pydantic-ai/blob/9ac853bb84ffed7ba0edccb026c5b4f3228c1de7/pydantic_ai_slim/pydantic_ai/messages.py#L1724-// The choice finish_reason as the provider spells it, as litellm copies it:+// The choice finish_reason as the provider spells it, and the OpenAI tool_calls and tool_call_id beside parts,+// as litellm copies them:// https://github.com/BerriAI/litellm/blob/9d72bae336ec0d14c41651adcb0d2e1149721f46/litellm/integrations/opentelemetry.py#L2822+// https://github.com/BerriAI/litellm/blob/9d72bae336ec0d14c41651adcb0d2e1149721f46/litellm/integrations/opentelemetry.py#L2803// Not yet: "thinking" for "reasoning".// semconv decodes messages that already follow the schema through the genai types. A part of a@@ -41,10 +43,31 @@ func (f *formatting) semconv(list []any) OutputMessages {msgs[i].Parts[j] = contentPart(map[string]any(block))}}+ if m, ok := list[i].(map[string]any); ok {+ semconvToolFields(&msgs[i], m)+ }}return msgs}+// semconvToolFields turns the tool_call_id of a tool message into its response part, and tool_calls into+// tool call parts when the message has none.+func semconvToolFields(msg *OutputMessage, m map[string]any) {+ if id := stringOf(m["tool_call_id"]); msg.Role == RoleTool && id != "" && onlyText(msg.Parts) {+ msg.Parts = Parts{part(ToolCallResponsePart{Type: PartTypeToolCallResponse, ID: &id, Response: toolResponse(textOf(msg.Parts))})}+ return+ }+ if hasToolCall(msg.Parts) {+ return+ }+ calls, _ := m["tool_calls"].([]any)+ for _, call := range calls {+ if c, ok := call.(map[string]any); ok {+ msg.Parts = append(msg.Parts, toolCallPart(c))+ }+ }+}+// semconvRepairs copies the parts whose keys SDKs misspell into the schema's spelling. The input// is left untouched, it is also the span's raw attribute.func semconvRepairs(list []any) []any {diff --git a/pkg/types/aiobservabilitytypes/message_format_semconv_test.go b/pkg/types/aiobservabilitytypes/message_format_semconv_test.goindex 8ff1dece68..e3bf22436d 100644--- a/pkg/types/aiobservabilitytypes/message_format_semconv_test.go+++ b/pkg/types/aiobservabilitytypes/message_format_semconv_test.go@@ -186,5 +186,18 @@ func TestFormat_Semconv_Repairs(t *testing.T) {wantInput: `[]`,wantOutput: `[{"role": "assistant", "parts": [], "finish_reason": "tool_call"}]`,},+ {+ // litellm capture, litellm_request span: litellm keeps the OpenAI tool_calls and tool_call_id beside+ // parts on each message:+ // https://github.com/BerriAI/litellm/blob/9d72bae336ec0d14c41651adcb0d2e1149721f46/litellm/integrations/opentelemetry.py#L2803+ name: "LiteLLMToolFieldsBesideParts_ToolCallAndResponseParts",+ input: `[{"role": "user", "parts": [{"type": "text", "content": "What's the weather in Bengaluru? Use the tool."}]}, {"role": "assistant", "parts": [], "tool_calls": [{"function": {"arguments": "{\"city\":\"Bengaluru\"}", "name": "get_current_weather"}, "id": "call_GuU9siyYfjnx3DfHKzFbRrvD", "type": "function"}]}, {"role": "tool", "parts": [{"type": "text", "content": "{\"city\": \"Bengaluru\", \"temp_c\": 28, \"summary\": \"Partly cloudy\"}"}], "tool_call_id": "call_GuU9siyYfjnx3DfHKzFbRrvD"}]`,+ wantInput: `[+ {"role": "user", "parts": [{"type": "text", "content": "What's the weather in Bengaluru? Use the tool."}]},+ {"role": "assistant", "parts": [{"type": "tool_call", "name": "get_current_weather", "id": "call_GuU9siyYfjnx3DfHKzFbRrvD", "arguments": {"city": "Bengaluru"}}]},+ {"role": "tool", "parts": [{"type": "tool_call_response", "id": "call_GuU9siyYfjnx3DfHKzFbRrvD", "response": {"city": "Bengaluru", "temp_c": 28, "summary": "Partly cloudy"}}]}+ ]`,+ wantOutput: `[]`,+ },})}diff --git a/pkg/types/aiobservabilitytypes/message_format_traceloop.go b/pkg/types/aiobservabilitytypes/message_format_traceloop.goindex 5bd4bd5cc8..caa1f2c987 100644--- a/pkg/types/aiobservabilitytypes/message_format_traceloop.go+++ b/pkg/types/aiobservabilitytypes/message_format_traceloop.go@@ -25,15 +25,20 @@ func (f *formatting) traceloop() bool {return true}-// completionToolCalls fills an output message left without parts from the gen_ai.completion.N keys-// litellm writes beside its semconv value, whose parts never carry the tool call:-// https://github.com/BerriAI/litellm/blob/9d72bae336ec0d14c41651adcb0d2e1149721f46/litellm/integrations/opentelemetry.py#L2365+// completionToolCalls adds the gen_ai.completion.N.function_call keys litellm writes beside its semconv value,+// whose parts never carry the tool calls, to the first output message without a tool call. N numbers the+// tool calls, not the choices:// https://github.com/BerriAI/litellm/blob/9d72bae336ec0d14c41651adcb0d2e1149721f46/litellm/integrations/opentelemetry.py#L2770+// https://github.com/BerriAI/litellm/blob/9d72bae336ec0d14c41651adcb0d2e1149721f46/litellm/integrations/opentelemetry.py#L2369func (f *formatting) completionToolCalls() {for i := range f.out.Output {- if len(f.out.Output[i].Parts) == 0 {- f.out.Output[i].Parts = append(f.out.Output[i].Parts, traceloopToolCalls(indexedAt(f.attributes, traceloopCompletions, i))...)+ if hasToolCall(f.out.Output[i].Parts) {+ continue+ }+ for _, completion := range indexed(f.attributes, traceloopCompletions) {+ f.out.Output[i].Parts = append(f.out.Output[i].Parts, traceloopToolCalls(completion)...)}+ return}}diff --git a/pkg/types/aiobservabilitytypes/message_format_traceloop_test.go b/pkg/types/aiobservabilitytypes/message_format_traceloop_test.goindex a816ffa80f..7b2d848f07 100644--- a/pkg/types/aiobservabilitytypes/message_format_traceloop_test.go+++ b/pkg/types/aiobservabilitytypes/message_format_traceloop_test.go@@ -88,19 +88,33 @@ func TestFormat_Traceloop(t *testing.T) {wantOutput: `[]`,},{- // litellm capture, litellm_request span: the semconv output has no part for the tool call, the- // gen_ai.completion.0.function_call keys beside it still carry it- // https://github.com/BerriAI/litellm/blob/9d72bae336ec0d14c41651adcb0d2e1149721f46/litellm/integrations/opentelemetry.py#L2365- // https://github.com/BerriAI/litellm/blob/9d72bae336ec0d14c41651adcb0d2e1149721f46/litellm/integrations/opentelemetry.py#L2770- name: "SemconvEmptyParts_ToolCallFromCompletionKeys",+ // litellm capture, litellm_request span: the semconv output has no tool call parts; the+ // gen_ai.completion.N.function_call keys, one N per tool call, carry them+ // https://github.com/BerriAI/litellm/blob/9d72bae336ec0d14c41651adcb0d2e1149721f46/litellm/integrations/opentelemetry.py#L2369+ name: "SemconvEmptyParts_ParallelToolCallsFromCompletionKeys",attributes: map[string]any{- GenAIInputMessages: `[{"role": "user", "parts": [{"type": "text", "content": "What is the weather in Bengaluru in celsius?"}]}]`,+ GenAIInputMessages: `[{"role": "user", "parts": [{"type": "text", "content": "What's the weather in Paris and in London? Use the tool for each."}]}]`,GenAIOutputMessages: `[{"role": "assistant", "parts": [], "finish_reason": "tool_calls"}]`,"gen_ai.completion.0.function_call.name": "get_current_weather",- "gen_ai.completion.0.function_call.arguments": `{"city":"Bengaluru"}`,+ "gen_ai.completion.0.function_call.arguments": `{"city": "Paris"}`,+ "gen_ai.completion.1.function_call.name": "get_current_weather",+ "gen_ai.completion.1.function_call.arguments": `{"city": "London"}`,},- wantInput: `[{"role": "user", "parts": [{"type": "text", "content": "What is the weather in Bengaluru in celsius?"}]}]`,- wantOutput: `[{"role": "assistant", "parts": [{"type": "tool_call", "name": "get_current_weather", "arguments": {"city": "Bengaluru"}}], "finish_reason": "tool_call"}]`,+ wantInput: `[{"role": "user", "parts": [{"type": "text", "content": "What's the weather in Paris and in London? Use the tool for each."}]}]`,+ wantOutput: `[{"role": "assistant", "parts": [+ {"type": "tool_call", "name": "get_current_weather", "arguments": {"city": "Paris"}},+ {"type": "tool_call", "name": "get_current_weather", "arguments": {"city": "London"}}+ ], "finish_reason": "tool_call"}]`,+ },+ {+ name: "SemconvTextPart_ToolCallFromCompletionKeysAdded",+ attributes: map[string]any{+ GenAIOutputMessages: `[{"role": "assistant", "parts": [{"type": "text", "content": "Let me check."}], "finish_reason": "tool_calls"}]`,+ "gen_ai.completion.0.function_call.name": "get_weather",+ "gen_ai.completion.0.function_call.arguments": `{"city": "Paris"}`,+ },+ wantInput: `[]`,+ wantOutput: `[{"role": "assistant", "parts": [{"type": "text", "content": "Let me check."}, {"type": "tool_call", "name": "get_weather", "arguments": {"city": "Paris"}}], "finish_reason": "tool_call"}]`,},})}diff --git a/pkg/types/aiobservabilitytypes/message_format_vercel.go b/pkg/types/aiobservabilitytypes/message_format_vercel.goindex ed7ae2eb3a..52c2980db1 100644--- a/pkg/types/aiobservabilitytypes/message_format_vercel.go+++ b/pkg/types/aiobservabilitytypes/message_format_vercel.go@@ -44,11 +44,12 @@ func (f *formatting) vercel() bool {}text, calls := attribute(f.attributes, vercelResponseText), decode(attribute(f.attributes, vercelResponseCalls))- switch output := decode(attribute(f.attributes, GenAIOutputMessages)).(type) {- case string:- text = output- case []any:- calls = output+ if output := attribute(f.attributes, GenAIOutputMessages); output != nil {+ if list, ok := vercelToolCalls(decode(output)); ok {+ calls = list+ } else {+ text = output+ }}msg := OutputMessage{Role: RoleAssistant, Parts: Parts{}, FinishReason: finishReason(stringOf(attribute(f.attributes, vercelResponseFinished)))}if s := stringOf(text); s != "" {@@ -147,6 +148,23 @@ func vercelPart(item any) Part {return part(GenericPart(p))}+// vercelToolCalls returns the value as the recorded tool calls, objects with toolName, so an answer that is+// itself JSON stays text:+// https://github.com/vercel/ai/blob/a5e92fe3c692f8e152fe1eb9ef50a4e734ad0ffb/packages/ai/src/generate-text/generate-text.ts#L780+func vercelToolCalls(value any) ([]any, bool) {+ list, ok := value.([]any)+ if !ok || len(list) == 0 {+ return nil, false+ }+ for _, item := range list {+ call, ok := item.(map[string]any)+ if !ok || stringOf(call["toolName"]) == "" {+ return nil, false+ }+ }+ return list, true+}+// vercelToolCall reads {toolCallId, toolName, input} or the v1 {toolCallId, toolName, args}; the// arguments may be a JSON string:// https://github.com/vercel/ai/blob/a5e92fe3c692f8e152fe1eb9ef50a4e734ad0ffb/packages/provider/src/language-model/v2/language-model-v2-prompt.ts#L126diff --git a/pkg/types/aiobservabilitytypes/message_format_vercel_test.go b/pkg/types/aiobservabilitytypes/message_format_vercel_test.goindex de10f4949f..c44b5266f8 100644--- a/pkg/types/aiobservabilitytypes/message_format_vercel_test.go+++ b/pkg/types/aiobservabilitytypes/message_format_vercel_test.go@@ -155,5 +155,21 @@ func TestFormat_Vercel(t *testing.T) {wantInput: `[{"role": "user", "parts": [{"type": "text", "content": "this should fail"}]}]`,wantOutput: `[]`,},+ {+ // ai.response.text is the model's text, moved onto the output by the mapper; an answer that is JSON+ // stays text, as only the recorded tool calls are objects with toolName:+ // https://github.com/vercel/ai/blob/a5e92fe3c692f8e152fe1eb9ef50a4e734ad0ffb/packages/ai/src/generate-text/generate-text.ts#L385+ // https://github.com/vercel/ai/blob/a5e92fe3c692f8e152fe1eb9ef50a4e734ad0ffb/packages/ai/src/generate-text/generate-text.ts#L780+ name: "JSONObjectAnswer_KeptAsText",+ attributes: map[string]any{"ai.operationId": "ai.generateText.doGenerate", "ai.response.finishReason": "stop", GenAIOutputMessages: `{"answer": 42}`},+ wantInput: `[]`,+ wantOutput: `[{"role": "assistant", "parts": [{"type": "text", "content": "{\"answer\": 42}"}], "finish_reason": "stop"}]`,+ },+ {+ name: "JSONListAnswer_KeptAsText",+ attributes: map[string]any{"ai.operationId": "ai.generateText.doGenerate", "ai.response.finishReason": "stop", GenAIOutputMessages: `[{"name":"Alice"},{"name":"Bob"}]`},+ wantInput: `[]`,+ wantOutput: `[{"role": "assistant", "parts": [{"type": "text", "content": "[{\"name\":\"Alice\"},{\"name\":\"Bob\"}]"}], "finish_reason": "stop"}]`,+ },})}