Skip to content

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.

feat(tracedetail): format OpenRouter answers and Vercel AI SDK prompts (#13139)

Nityananda Gohain8c4db94ef795
diff --git a/pkg/types/aiobservabilitytypes/message_format.go b/pkg/types/aiobservabilitytypes/message_format.go
index 06cf580673..37c1dff911 100644
--- a/pkg/types/aiobservabilitytypes/message_format.go
+++ b/pkg/types/aiobservabilitytypes/message_format.go
@@ -15,6 +15,7 @@ const (
FormatterSemconv = "semconv"
FormatterOpenInference = "openinference"
FormatterLangChain = "langchain"
+ FormatterOpenRouter = "openrouter"
FormatterOpenAIChat = "openai.chat"
FormatterOpenAIResponses = "openai.responses"
FormatterText = "text"
@@ -33,7 +34,8 @@ var dataURL = regexp.MustCompile(`^data:([^;,]+);base64,(.+)$`)
// https://github.com/vercel/ai/blob/a5e92fe3c692f8e152fe1eb9ef50a4e734ad0ffb/packages/provider/src/language-model/v2/language-model-v2-finish-reason.ts
var finishReasonAliases = map[string]FinishReason{
"tool_calls": FinishReasonToolCall, "function_call": FinishReasonToolCall, "tool-calls": FinishReasonToolCall, "tool_use": FinishReasonToolCall,
- "end_turn": FinishReasonStop, "stop_sequence": FinishReasonStop, "completed": FinishReasonStop,
+ "content-filter": FinishReasonContentFilter,
+ "end_turn": FinishReasonStop, "stop_sequence": FinishReasonStop, "completed": FinishReasonStop,
"max_tokens": FinishReasonLength, "max_output_tokens": FinishReasonLength,
"failed": FinishReasonError, "cancelled": FinishReasonError, "incomplete": FinishReasonError,
}
@@ -49,8 +51,8 @@ type FormattedMessages struct {
// FormatMessages builds the OTel GenAI view of a span's messages from the formats SDKs write. attributes
// is the span's JSON attribute document, nested by key segment, so an object value is whole. Each
// message value is a JSON string or a structured value. OpenInference's flattened messages come first,
-// then a tool call and its result, then the message attributes, then openllmetry's flattened prompts
-// and completions. gen_ai.system_instructions leads the
+// then a tool call and its result, then the AI SDK's ai.* keys, then the message attributes, then
+// openllmetry's flattened prompts and completions. gen_ai.system_instructions leads the
// input and gen_ai.response.finish_reasons overrides the finish reasons the converter found, whichever
// converter ran.
func FormatMessages(attributes map[string]any) FormattedMessages {
@@ -59,6 +61,7 @@ func FormatMessages(attributes map[string]any) FormattedMessages {
case f.openInference():
case attribute(f.attributes, GenAIToolCallArguments) != nil || attribute(f.attributes, GenAIToolCallResult) != nil:
f.tool()
+ case f.vercel():
case attribute(f.attributes, GenAIInputMessages) != nil || attribute(f.attributes, GenAIOutputMessages) != nil:
f.messages()
case f.traceloop():
@@ -92,8 +95,12 @@ func (f *formatting) messages() {
f.warnMixedFormats(inputLabel, outputLabel)
}
+// OpenRouter's answer object beside the OpenAI chat request it answers is one format, not a mix.
func (f *formatting) warnMixedFormats(inputLabel, outputLabel string) {
- if inputLabel != "" && outputLabel != "" && outputLabel != inputLabel {
+ switch {
+ case inputLabel == "" || outputLabel == "" || outputLabel == inputLabel:
+ case inputLabel == FormatterOpenAIChat && outputLabel == FormatterOpenRouter:
+ default:
f.warn("output formatted as %s, input as %s", outputLabel, inputLabel)
}
}
@@ -154,6 +161,8 @@ func (f *formatting) side(value any, role Role) (OutputMessages, string) {
switch {
case isLangChainRun(v):
return f.langChainRun(v), FormatterLangChain
+ case isOpenRouterCompletion(v):
+ return f.openRouterCompletion(v), FormatterOpenRouter
case isResponsesResponse(v):
return f.responsesResponse(v), FormatterOpenAIResponses
case isResponsesRequest(v):
@@ -433,6 +442,19 @@ func filePart(block map[string]any) (Part, bool) {
return Part{}, false
}
+// modalityOf maps a MIME type to the schema modality, document for anything that is not media.
+func modalityOf(mimeType string) Modality {
+ switch {
+ case strings.HasPrefix(mimeType, "image/"):
+ return ModalityImage
+ case strings.HasPrefix(mimeType, "audio/"):
+ return ModalityAudio
+ case strings.HasPrefix(mimeType, "video/"):
+ return ModalityVideo
+ }
+ return ModalityDocument
+}
+
// mediaPart places a data URL in a BlobPart, any other URL in a UriPart.
func mediaPart(url string, modality Modality) any {
if m := dataURL.FindStringSubmatch(url); m != nil {
diff --git a/pkg/types/aiobservabilitytypes/message_format_openrouter.go b/pkg/types/aiobservabilitytypes/message_format_openrouter.go
new file mode 100644
index 0000000000..96586e8ef4
--- /dev/null
+++ b/pkg/types/aiobservabilitytypes/message_format_openrouter.go
@@ -0,0 +1,47 @@
+package aiobservabilitytypes
+
+import "strings"
+
+// Format: the {completion, reasoning, tools, rawRequest} answer object OpenRouter's broadcast writes as
+// gen_ai.completion on its LLM Generation spans, beside the OpenAI chat {messages} request it writes as
+// gen_ai.prompt; the span mapper moves both onto gen_ai.*.messages. A tool call arrives as the tool name
+// followed by its JSON arguments in completion, so it is split against the names in tools.
+// https://openrouter.ai/docs/guides/features/broadcast/otel-collector
+
+func isOpenRouterCompletion(m map[string]any) bool {
+ _, ok := m["completion"].(string)
+ return ok
+}
+
+func (f *formatting) openRouterCompletion(m map[string]any) OutputMessages {
+ msg := OutputMessage{Role: RoleAssistant, Parts: Parts{}}
+ if reasoning := stringOf(m["reasoning"]); reasoning != "" {
+ msg.Parts = append(msg.Parts, part(ReasoningPart{Type: PartTypeReasoning, Content: reasoning}))
+ }
+ completion := stringOf(m["completion"])
+ if call, ok := openRouterToolCall(completion, m["tools"]); ok {
+ msg.Parts = append(msg.Parts, call)
+ msg.FinishReason = finishReason("tool_calls")
+ } else if completion != "" {
+ msg.Parts = append(msg.Parts, part(textPart(completion)))
+ }
+ return OutputMessages{msg}
+}
+
+func openRouterToolCall(completion string, tools any) (Part, bool) {
+ list, _ := tools.([]any)
+ for _, item := range list {
+ tool, _ := item.(map[string]any)
+ fn, _ := tool["function"].(map[string]any)
+ name := stringOf(fn["name"])
+ if name == "" || !strings.HasPrefix(completion, name) {
+ continue
+ }
+ args, ok := decode(strings.TrimPrefix(completion, name)).(map[string]any)
+ if !ok {
+ continue
+ }
+ return part(ToolCallRequestPart{Type: PartTypeToolCall, Name: name, Arguments: args}), true
+ }
+ return Part{}, false
+}
diff --git a/pkg/types/aiobservabilitytypes/message_format_openrouter_test.go b/pkg/types/aiobservabilitytypes/message_format_openrouter_test.go
new file mode 100644
index 0000000000..54fd39c9ba
--- /dev/null
+++ b/pkg/types/aiobservabilitytypes/message_format_openrouter_test.go
@@ -0,0 +1,35 @@
+package aiobservabilitytypes
+
+import "testing"
+
+// Captured cases are gen_ai.input.messages and gen_ai.output.messages as the collector stored them for
+// the SigNoz/scripts openrouter-gateway capture, LLM Generation spans.
+func TestFormat_OpenRouter(t *testing.T) {
+ runFormatCases(t, []formatCase{
+ {
+ // the completion is the tool name followed by the JSON arguments
+ name: "CompletionIsToolNameAndArguments_ToolCallPart",
+ input: `{"messages":[{"role":"user","content":"What's the weather in Bengaluru? Use the tool."}]}`,
+ output: `{"completion":"get_current_weather{\"city\":\"Bengaluru\"}","reasoning":null,"tools":[{"type":"function","function":{"name":"get_current_weather","description":"Get the current weather for a city.","parameters":{"type":"object","properties":{"city":{"type":"string"}},"required":["city"]}}}],"rawRequest":{"model":"openai/gpt-4o-mini","max_tokens":80,"tool_choice":"auto"}}`,
+ wantInput: `[{"role": "user", "parts": [{"type": "text", "content": "What's the weather in Bengaluru? Use the tool."}]}]`,
+ wantOutput: `[{"role": "assistant", "parts": [{"type": "tool_call", "name": "get_current_weather", "arguments": {"city": "Bengaluru"}}], "finish_reason": "tool_call"}]`,
+ },
+ {
+ name: "CompletionWithReasoning_ReasoningThenText",
+ input: `{"messages":[{"role":"system","content":"You are concise."},{"role":"user","content":"Say hello in five words."}]}`,
+ output: `{"completion":"Hello there, how are you?","reasoning":"A greeting of exactly five words.","tools":[],"rawRequest":{"model":"openai/gpt-4o-mini"}}`,
+ wantInput: `[
+ {"role": "system", "parts": [{"type": "text", "content": "You are concise."}]},
+ {"role": "user", "parts": [{"type": "text", "content": "Say hello in five words."}]}
+ ]`,
+ wantOutput: `[{"role": "assistant", "parts": [{"type": "reasoning", "content": "A greeting of exactly five words."}, {"type": "text", "content": "Hello there, how are you?"}]}]`,
+ },
+ {
+ // a completion that only starts with a tool name stays text
+ name: "CompletionStartsWithToolNameButNoArguments_Text",
+ output: `{"completion":"get_current_weather is the tool to call.","reasoning":null,"tools":[{"type":"function","function":{"name":"get_current_weather"}}]}`,
+ wantInput: `[]`,
+ wantOutput: `[{"role": "assistant", "parts": [{"type": "text", "content": "get_current_weather is the tool to call."}]}]`,
+ },
+ })
+}
diff --git a/pkg/types/aiobservabilitytypes/message_format_vercel.go b/pkg/types/aiobservabilitytypes/message_format_vercel.go
new file mode 100644
index 0000000000..ed7ae2eb3a
--- /dev/null
+++ b/pkg/types/aiobservabilitytypes/message_format_vercel.go
@@ -0,0 +1,187 @@
+package aiobservabilitytypes
+
+import (
+ "encoding/base64"
+ "strings"
+)
+
+// Format: the AI SDK's legacy telemetry attributes. ai.prompt.messages on the doGenerate and doStream
+// spans is the provider prompt, ai.prompt on the parent generateText and streamText spans is {system,
+// prompt} or {messages}, ai.response.text and ai.response.toolCalls hold the answer:
+// https://ai-sdk.dev/docs/ai-sdk-core/telemetry
+// The prompt parts in the provider's v1 spelling, image with mimeType, tool-call args and tool-result
+// result, or v2, file with mediaType, tool-call input and tool-result output:
+// https://github.com/vercel/ai/blob/47bd16dd50283c82a5fef4c839d7f4102c45f5b9/packages/provider/src/language-model/v1/language-model-v1-prompt.ts#L120
+// https://github.com/vercel/ai/blob/a5e92fe3c692f8e152fe1eb9ef50a4e734ad0ffb/packages/provider/src/language-model/v2/language-model-v2-prompt.ts#L93
+// The span mapper moves ai.prompt.messages and one of the response keys onto gen_ai.*.messages; the other
+// response key and ai.prompt are read where they are.
+// Written by: the Vercel AI SDK, Mastra.
+// Handled: text, reasoning, tool-call, tool-result, image and file parts as a data URL, a URL, bare base64
+// or a serialised Buffer; ai.response.finishReason.
+
+const (
+ vercelPrompt = "ai.prompt"
+ vercelOperation = "ai.operationId"
+ vercelResponseText = "ai.response.text"
+ vercelResponseCalls = "ai.response.toolCalls"
+ vercelResponseFinished = "ai.response.finishReason"
+)
+
+func (f *formatting) vercel() bool {
+ if attribute(f.attributes, vercelOperation) == nil && attribute(f.attributes, vercelPrompt) == nil && attribute(f.attributes, "ai.response") == nil {
+ return false
+ }
+ switch input := decode(attribute(f.attributes, GenAIInputMessages)).(type) {
+ case []any:
+ f.out.Input = inputMessages(vercelMessages(input))
+ case nil:
+ if prompt, ok := decode(attribute(f.attributes, vercelPrompt)).(map[string]any); ok {
+ f.out.Input = inputMessages(vercelParentPrompt(prompt))
+ }
+ default:
+ f.out.Input = inputMessages(OutputMessages{genericMessage(input)})
+ f.warn("input is not a message list, kept as generic")
+ }
+
+ 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
+ }
+ msg := OutputMessage{Role: RoleAssistant, Parts: Parts{}, FinishReason: finishReason(stringOf(attribute(f.attributes, vercelResponseFinished)))}
+ if s := stringOf(text); s != "" {
+ msg.Parts = append(msg.Parts, part(textPart(s)))
+ }
+ if list, ok := calls.([]any); ok {
+ for _, item := range list {
+ if call, ok := item.(map[string]any); ok {
+ msg.Parts = append(msg.Parts, vercelToolCall(call))
+ }
+ }
+ }
+ if len(msg.Parts) > 0 {
+ f.out.Output = OutputMessages{msg}
+ }
+ return true
+}
+
+// vercelParentPrompt reads the parent span's ai.prompt: the system prompt with either a bare prompt or
+// the messages.
+func vercelParentPrompt(prompt map[string]any) OutputMessages {
+ var msgs OutputMessages
+ if parts := systemParts(prompt["system"]); len(parts) > 0 {
+ msgs = append(msgs, OutputMessage{Role: RoleSystem, Parts: parts})
+ }
+ if messages, ok := prompt["messages"].([]any); ok {
+ return append(msgs, vercelMessages(messages)...)
+ }
+ if text := stringOf(prompt["prompt"]); text != "" {
+ msgs = append(msgs, OutputMessage{Role: RoleUser, Parts: Parts{part(textPart(text))}})
+ }
+ return msgs
+}
+
+func vercelMessages(list []any) OutputMessages {
+ out := make(OutputMessages, 0, len(list))
+ for _, item := range list {
+ m, ok := item.(map[string]any)
+ if !ok {
+ out = append(out, genericMessage(item))
+ continue
+ }
+ msg := OutputMessage{Role: role(stringOf(m["role"])), Parts: Parts{}}
+ switch content := m["content"].(type) {
+ case string:
+ if content != "" {
+ msg.Parts = append(msg.Parts, part(textPart(content)))
+ }
+ case []any:
+ for _, item := range content {
+ msg.Parts = append(msg.Parts, vercelPart(item))
+ }
+ }
+ out = append(out, msg)
+ }
+ return out
+}
+
+func vercelPart(item any) Part {
+ p, ok := item.(map[string]any)
+ if !ok {
+ return contentPart(item)
+ }
+ switch stringOf(p["type"]) {
+ case "text":
+ return part(textPart(stringOf(p["text"])))
+ case "reasoning":
+ return part(ReasoningPart{Type: PartTypeReasoning, Content: stringOf(p["text"])})
+ case "redacted-reasoning":
+ return part(ReasoningPart{Type: PartTypeReasoning})
+ case "tool-call":
+ return vercelToolCall(p)
+ case "tool-result":
+ response := p["result"]
+ if output, ok := p["output"].(map[string]any); ok {
+ response = output["value"]
+ }
+ return part(ToolCallResponsePart{Type: PartTypeToolCallResponse, ID: optionalString(p["toolCallId"]), Response: response})
+ case "image":
+ mime := stringOf(p["mimeType"])
+ if mime == "" {
+ mime = stringOf(p["mediaType"])
+ }
+ if media, ok := vercelMedia(p["image"], mime, ModalityImage); ok {
+ return media
+ }
+ case "file":
+ mime := stringOf(p["mediaType"])
+ if mime == "" {
+ mime = stringOf(p["mimeType"])
+ }
+ if media, ok := vercelMedia(p["data"], mime, modalityOf(mime)); ok {
+ return media
+ }
+ }
+ return part(GenericPart(p))
+}
+
+// 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#L126
+// https://github.com/vercel/ai/blob/47bd16dd50283c82a5fef4c839d7f4102c45f5b9/packages/provider/src/language-model/v1/language-model-v1-prompt.ts#L175
+func vercelToolCall(call map[string]any) Part {
+ args := call["input"]
+ if args == nil {
+ args = call["args"]
+ }
+ return part(ToolCallRequestPart{Type: PartTypeToolCall, Name: stringOf(call["toolName"]), ID: optionalString(call["toolCallId"]), Arguments: decode(args)})
+}
+
+// vercelMedia reads a data URL, a URL, bare base64 or a serialised Node Buffer {type: Buffer,
+// data: [bytes]}, which is how the parent span's ai.prompt carries an image given as bytes.
+func vercelMedia(value any, mimeType string, modality Modality) (Part, bool) {
+ switch v := value.(type) {
+ case string:
+ if v == "" {
+ return Part{}, false
+ }
+ if strings.Contains(v, "://") || strings.HasPrefix(v, "data:") {
+ return part(mediaPart(v, modality)), true
+ }
+ return part(BlobPart{Type: PartTypeBlob, Modality: modality, Content: v, MimeType: optionalString(mimeType)}), true
+ case map[string]any:
+ bytes, ok := v["data"].([]any)
+ if !ok {
+ return Part{}, false
+ }
+ raw := make([]byte, 0, len(bytes))
+ for _, b := range bytes {
+ n, _ := b.(float64)
+ raw = append(raw, byte(n))
+ }
+ return part(BlobPart{Type: PartTypeBlob, Modality: modality, Content: base64.StdEncoding.EncodeToString(raw), MimeType: optionalString(mimeType)}), true
+ }
+ return Part{}, false
+}
diff --git a/pkg/types/aiobservabilitytypes/message_format_vercel_test.go b/pkg/types/aiobservabilitytypes/message_format_vercel_test.go
new file mode 100644
index 0000000000..de10f4949f
--- /dev/null
+++ b/pkg/types/aiobservabilitytypes/message_format_vercel_test.go
@@ -0,0 +1,159 @@
+package aiobservabilitytypes
+
+import "testing"
+
+// Captured cases are the ai.* attributes as the collector stored them for the SigNoz/scripts vercel and
+// mastra captures, the AI SDK v4 shapes; the v5 cases follow the provider's v2 prompt types at the
+// linked lines.
+func TestFormat_Vercel(t *testing.T) {
+ runFormatCases(t, []formatCase{
+ {
+ // vercel capture, ai.generateText.doGenerate span after a tool round: v1 tool-call args and
+ // tool-result result
+ // https://github.com/vercel/ai/blob/47bd16dd50283c82a5fef4c839d7f4102c45f5b9/packages/provider/src/language-model/v1/language-model-v1-prompt.ts#L175
+ name: "V4ToolRound_ToolCallAndResultParts",
+ attributes: map[string]any{
+ "ai.operationId": "ai.generateText.doGenerate",
+ "ai.response.finishReason": "stop",
+ GenAIInputMessages: `[{"role":"user","content":[{"type":"text","text":"What is the weather in Paris and in London, in celsius? Use the tool for each."}]},{"role":"assistant","content":[{"type":"tool-call","toolCallId":"call_isBPb3o3cqhY8OpugQntLOgy","toolName":"get_current_weather","args":{"city":"Paris","unit":"c"}},{"type":"tool-call","toolCallId":"call_sH9xd5EWFkiSaWmYvg4CeWTl","toolName":"get_current_weather","args":{"city":"London","unit":"c"}}]},{"role":"tool","content":[{"type":"tool-result","toolCallId":"call_isBPb3o3cqhY8OpugQntLOgy","toolName":"get_current_weather","result":{"city":"Paris","unit":"c","temperature":18,"condition":"clear"}},{"type":"tool-result","toolCallId":"call_sH9xd5EWFkiSaWmYvg4CeWTl","toolName":"get_current_weather","result":{"city":"London","unit":"c","temperature":18,"condition":"clear"}}]}]`,
+ GenAIOutputMessages: "The current weather is as follows:\n\n- **Paris**: 18°C, clear\n- **London**: 18°C, clear",
+ },
+ wantInput: `[
+ {"role": "user", "parts": [{"type": "text", "content": "What is the weather in Paris and in London, in celsius? Use the tool for each."}]},
+ {"role": "assistant", "parts": [
+ {"type": "tool_call", "name": "get_current_weather", "id": "call_isBPb3o3cqhY8OpugQntLOgy", "arguments": {"city": "Paris", "unit": "c"}},
+ {"type": "tool_call", "name": "get_current_weather", "id": "call_sH9xd5EWFkiSaWmYvg4CeWTl", "arguments": {"city": "London", "unit": "c"}}
+ ]},
+ {"role": "tool", "parts": [
+ {"type": "tool_call_response", "id": "call_isBPb3o3cqhY8OpugQntLOgy", "response": {"city": "Paris", "unit": "c", "temperature": 18, "condition": "clear"}},
+ {"type": "tool_call_response", "id": "call_sH9xd5EWFkiSaWmYvg4CeWTl", "response": {"city": "London", "unit": "c", "temperature": 18, "condition": "clear"}}
+ ]}
+ ]`,
+ wantOutput: `[{"role": "assistant", "parts": [{"type": "text", "content": "The current weather is as follows:\n\n- **Paris**: 18°C, clear\n- **London**: 18°C, clear"}], "finish_reason": "stop"}]`,
+ },
+ {
+ // vercel capture, ai.streamText.doStream span: the mapper moved ai.response.toolCalls onto the output
+ // and left the empty ai.response.text behind
+ name: "V4ToolCalls_ToolCallParts_EmptyTextIgnored",
+ attributes: map[string]any{
+ "ai.operationId": "ai.streamText.doStream",
+ "ai.response.finishReason": "tool-calls",
+ "ai.response.text": "",
+ GenAIInputMessages: `[{"role":"user","content":[{"type":"text","text":"What is the weather in Bengaluru in celsius?"}]}]`,
+ GenAIOutputMessages: `[{"type":"tool-call","toolCallId":"call_l0ycKcmt1uPCdGWR4SkwoLBv","toolName":"get_current_weather","args":{"city":"Bengaluru","unit":"c"}}]`,
+ },
+ 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", "id": "call_l0ycKcmt1uPCdGWR4SkwoLBv", "arguments": {"city": "Bengaluru", "unit": "c"}}], "finish_reason": "tool_call"}]`,
+ },
+ {
+ // vercel capture, ai.generateText parent span: ai.prompt is unmapped, the answer text was moved
+ name: "ParentPromptWithSystem_SystemAndUserMessages",
+ attributes: map[string]any{
+ "ai.operationId": "ai.generateText",
+ "ai.prompt": `{"system":"You are concise.","prompt":"Say hello in five words."}`,
+ "ai.response.finishReason": "stop",
+ GenAIOutputMessages: "Hello! How are you today?",
+ },
+ wantInput: `[
+ {"role": "system", "parts": [{"type": "text", "content": "You are concise."}]},
+ {"role": "user", "parts": [{"type": "text", "content": "Say hello in five words."}]}
+ ]`,
+ wantOutput: `[{"role": "assistant", "parts": [{"type": "text", "content": "Hello! How are you today?"}], "finish_reason": "stop"}]`,
+ },
+ {
+ // vercel capture, ai.generateText parent span of a vision call: the image is a serialised Node Buffer
+ name: "ParentPromptMessages_BufferImage_BlobPart",
+ attributes: map[string]any{
+ "ai.operationId": "ai.generateText",
+ "ai.prompt": `{"messages":[{"role":"user","content":[{"type":"text","text":"What animal is in this image? One sentence."},{"type":"image","image":{"type":"Buffer","data":[137,80,78,71]},"mediaType":"image/png"}]}]}`,
+ GenAIOutputMessages: "A duck.",
+ },
+ wantInput: `[{"role": "user", "parts": [
+ {"type": "text", "content": "What animal is in this image? One sentence."},
+ {"type": "blob", "modality": "image", "mime_type": "image/png", "content": "iVBORw=="}
+ ]}]`,
+ wantOutput: `[{"role": "assistant", "parts": [{"type": "text", "content": "A duck."}]}]`,
+ },
+ {
+ // the v2 prompt after a tool round: tool-call input and tool-result output
+ // https://github.com/vercel/ai/blob/a5e92fe3c692f8e152fe1eb9ef50a4e734ad0ffb/packages/provider/src/language-model/v2/language-model-v2-prompt.ts#L126
+ // https://github.com/vercel/ai/blob/a5e92fe3c692f8e152fe1eb9ef50a4e734ad0ffb/packages/provider/src/language-model/v2/language-model-v2-prompt.ts#L161
+ name: "V5ToolRound_InputAndOutputSpellings",
+ attributes: map[string]any{
+ "ai.operationId": "ai.generateText.doGenerate",
+ "ai.response.finishReason": "stop",
+ GenAIInputMessages: `[{"role":"system","content":"Use the tool."},{"role":"user","content":[{"type":"text","text":"Weather in Paris?"}]},{"role":"assistant","content":[{"type":"tool-call","toolCallId":"call_1","toolName":"getWeather","input":{"city":"Paris"}}]},{"role":"tool","content":[{"type":"tool-result","toolCallId":"call_1","toolName":"getWeather","output":{"type":"json","value":{"city":"Paris","tempC":18,"summary":"Clear"}}}]}]`,
+ GenAIOutputMessages: "Paris is clear at 18C.",
+ },
+ wantInput: `[
+ {"role": "system", "parts": [{"type": "text", "content": "Use the tool."}]},
+ {"role": "user", "parts": [{"type": "text", "content": "Weather in Paris?"}]},
+ {"role": "assistant", "parts": [{"type": "tool_call", "name": "getWeather", "id": "call_1", "arguments": {"city": "Paris"}}]},
+ {"role": "tool", "parts": [{"type": "tool_call_response", "id": "call_1", "response": {"city": "Paris", "tempC": 18, "summary": "Clear"}}]}
+ ]`,
+ wantOutput: `[{"role": "assistant", "parts": [{"type": "text", "content": "Paris is clear at 18C."}], "finish_reason": "stop"}]`,
+ },
+ {
+ // ai.response.toolCalls in the v2 spelling, the arguments under input; the text the mapper left
+ // behind in ai.response.text comes first
+ // https://github.com/vercel/ai/blob/a5e92fe3c692f8e152fe1eb9ef50a4e734ad0ffb/packages/provider/src/language-model/v2/language-model-v2-prompt.ts#L126
+ name: "V5ToolCallsBesideText_TextThenToolCall",
+ attributes: map[string]any{
+ "ai.operationId": "ai.streamText.doStream",
+ "ai.response.finishReason": "tool-calls",
+ "ai.response.text": "Let me check.",
+ GenAIInputMessages: `[{"role":"user","content":[{"type":"text","text":"Weather in Paris? Use the tool."}]}]`,
+ GenAIOutputMessages: `[{"toolCallId":"call_2","toolName":"getWeather","input":{"city":"Paris"}}]`,
+ },
+ wantInput: `[{"role": "user", "parts": [{"type": "text", "content": "Weather in Paris? Use the tool."}]}]`,
+ wantOutput: `[{"role": "assistant", "parts": [
+ {"type": "text", "content": "Let me check."},
+ {"type": "tool_call", "name": "getWeather", "id": "call_2", "arguments": {"city": "Paris"}}
+ ], "finish_reason": "tool_call"}]`,
+ },
+ {
+ // the v2 file part, data as bare base64 or a URL with its mediaType, a reasoning part, and a tool
+ // result given as text
+ // https://github.com/vercel/ai/blob/a5e92fe3c692f8e152fe1eb9ef50a4e734ad0ffb/packages/provider/src/language-model/v2/language-model-v2-prompt.ts#L93
+ // https://github.com/vercel/ai/blob/a5e92fe3c692f8e152fe1eb9ef50a4e734ad0ffb/packages/provider/src/language-model/v2/language-model-v2-prompt.ts#L187
+ name: "V5FileParts_BlobsByMediaType_URLAsUri",
+ attributes: map[string]any{
+ "ai.operationId": "ai.generateText.doGenerate",
+ GenAIInputMessages: `[{"role":"user","content":[{"type":"text","text":"What is in these?"},{"type":"file","mediaType":"image/png","data":"iVBORw0KGgo="},{"type":"file","mediaType":"application/pdf","filename":"a.pdf","data":"JVBERi0xLjQgc3R1Yg=="},{"type":"file","mediaType":"audio/wav","data":"https://example.com/a.wav"},{"type":"reasoning","text":"thinking"}]},{"role":"tool","content":[{"type":"tool-result","toolCallId":"call_1","toolName":"read","output":{"type":"text","value":"done"}}]}]`,
+ },
+ wantInput: `[
+ {"role": "user", "parts": [
+ {"type": "text", "content": "What is in these?"},
+ {"type": "blob", "modality": "image", "mime_type": "image/png", "content": "iVBORw0KGgo="},
+ {"type": "blob", "modality": "document", "mime_type": "application/pdf", "content": "JVBERi0xLjQgc3R1Yg=="},
+ {"type": "uri", "modality": "audio", "uri": "https://example.com/a.wav"},
+ {"type": "reasoning", "content": "thinking"}
+ ]},
+ {"role": "tool", "parts": [{"type": "tool_call_response", "id": "call_1", "response": "done"}]}
+ ]`,
+ wantOutput: `[]`,
+ },
+ {
+ // mastra capture, ai.generateText.doGenerate span: the v4 shapes with the arguments as a JSON string
+ name: "MastraToolCalls_ToolCallPart",
+ attributes: map[string]any{
+ "ai.operationId": "ai.generateText.doGenerate",
+ "ai.response.finishReason": "tool-calls",
+ GenAIInputMessages: `[{"role":"system","content":"You are a weather assistant. When asked about weather, call get-weather once per city and summarise."},{"role":"user","content":[{"type":"text","text":"What is the weather in Bengaluru?"}]}]`,
+ GenAIOutputMessages: `[{"toolCallType":"function","toolCallId":"call_526phoykytobsvLouzES805J","toolName":"getWeather","args":"{\"city\":\"Bengaluru\"}"}]`,
+ },
+ wantInput: `[
+ {"role": "system", "parts": [{"type": "text", "content": "You are a weather assistant. When asked about weather, call get-weather once per city and summarise."}]},
+ {"role": "user", "parts": [{"type": "text", "content": "What is the weather in Bengaluru?"}]}
+ ]`,
+ wantOutput: `[{"role": "assistant", "parts": [{"type": "tool_call", "name": "getWeather", "id": "call_526phoykytobsvLouzES805J", "arguments": {"city": "Bengaluru"}}], "finish_reason": "tool_call"}]`,
+ },
+ {
+ // vercel capture, ai.generateText span of a failed call
+ name: "PromptOnlyNoResponse_InputOnly",
+ attributes: map[string]any{"ai.operationId": "ai.generateText", "ai.prompt": `{"prompt":"this should fail"}`},
+ wantInput: `[{"role": "user", "parts": [{"type": "text", "content": "this should fail"}]}]`,
+ wantOutput: `[]`,
+ },
+ })
+}
diff --git a/tests/integration/tests/tracedetail/02_thread.py b/tests/integration/tests/tracedetail/02_thread.py
index bfc36ffece..25bf338c02 100644
--- a/tests/integration/tests/tracedetail/02_thread.py
+++ b/tests/integration/tests/tracedetail/02_thread.py
@@ -21,7 +21,7 @@ def test_thread_returns_gen_ai_spans_in_order(
seed_attribute_evolution("traces", ATTRIBUTE_JSON_ROLLOUT_TIME)
now = datetime.now(tz=UTC).replace(microsecond=0)
trace_id = TraceIdGenerator.trace_id()
- root_id, first_llm_id, tool_id, agent_id, second_llm_id, legacy_llm_id, third_llm_id, flattened_llm_id, responses_llm_id, langchain_llm_id = (TraceIdGenerator.span_id() for _ in range(10))
+ root_id, first_llm_id, tool_id, agent_id, second_llm_id, legacy_llm_id, third_llm_id, flattened_llm_id, responses_llm_id, langchain_llm_id, openrouter_llm_id, vercel_llm_id = (TraceIdGenerator.span_id() for _ in range(12))
resources = {"service.name": "tracedetail-thread"}
first_input = json.dumps([{"role": "user", "parts": [{"type": "text", "content": "weather in Bangalore?"}]}])
first_output = json.dumps([{"role": "assistant", "parts": [{"type": "tool_call", "id": "call_1", "name": "get_weather", "arguments": {"city": "Bangalore"}}]}])
@@ -111,6 +111,36 @@ def test_thread_returns_gen_ai_spans_in_order(
},
attribute_write_mode="json_only",
),
+ Traces(
+ timestamp=now + timedelta(seconds=2),
+ trace_id=trace_id,
+ span_id=openrouter_llm_id,
+ parent_span_id=root_id,
+ name="LLM Generation",
+ resources=resources,
+ attributes={
+ "gen_ai.request.model": "openai/gpt-4o-mini",
+ "gen_ai.input.messages": json.dumps({"messages": [{"role": "user", "content": "one word"}]}),
+ "gen_ai.output.messages": json.dumps({"completion": "Yes.", "reasoning": None, "rawRequest": {"model": "openai/gpt-4o-mini"}}),
+ },
+ attribute_write_mode="json_only",
+ ),
+ Traces(
+ timestamp=now + timedelta(seconds=3),
+ trace_id=trace_id,
+ span_id=vercel_llm_id,
+ parent_span_id=root_id,
+ name="ai.generateText.doGenerate",
+ resources=resources,
+ attributes={
+ "gen_ai.request.model": "gpt-4o-mini",
+ "ai.operationId": "ai.generateText.doGenerate",
+ "ai.response.finishReason": "stop",
+ "gen_ai.input.messages": json.dumps([{"role": "user", "content": [{"type": "text", "text": "last one"}]}]),
+ "gen_ai.output.messages": "Done.",
+ },
+ attribute_write_mode="json_only",
+ ),
]
)
@@ -119,10 +149,10 @@ def test_thread_returns_gen_ai_spans_in_order(
assert response.status_code == HTTPStatus.OK, response.text
thread = response.json()["data"]
- assert [span["span_id"] for span in thread["spans"]] == [first_llm_id, tool_id, agent_id, second_llm_id, legacy_llm_id, third_llm_id, flattened_llm_id, responses_llm_id, langchain_llm_id]
+ assert [span["span_id"] for span in thread["spans"]] == [first_llm_id, tool_id, agent_id, second_llm_id, legacy_llm_id, third_llm_id, flattened_llm_id, responses_llm_id, langchain_llm_id, openrouter_llm_id, vercel_llm_id]
assert "nextCursor" not in thread
- first, tool, agent, input_only, legacy, output_only, flattened, responses, langchain = thread["spans"]
+ first, tool, agent, input_only, legacy, output_only, flattened, responses, langchain, openrouter, vercel = thread["spans"]
assert agent["attributes"]["gen_ai.agent.name"] == "planner"
assert agent["formattedInput"] == []
assert agent["formattedOutput"] == []
@@ -158,6 +188,10 @@ def test_thread_returns_gen_ai_spans_in_order(
assert responses["formattedOutput"] == [{"role": "assistant", "parts": [{"type": "text", "content": "Bye!"}], "finish_reason": "stop"}]
assert langchain["formattedInput"] == [{"role": "user", "parts": [{"type": "text", "content": "see you"}]}]
assert langchain["formattedOutput"] == [{"role": "assistant", "parts": [{"type": "text", "content": "See you!"}], "finish_reason": "stop"}]
+ assert openrouter["formattedInput"] == [{"role": "user", "parts": [{"type": "text", "content": "one word"}]}]
+ assert openrouter["formattedOutput"] == [{"role": "assistant", "parts": [{"type": "text", "content": "Yes."}]}]
+ assert vercel["formattedInput"] == [{"role": "user", "parts": [{"type": "text", "content": "last one"}]}]
+ assert vercel["formattedOutput"] == [{"role": "assistant", "parts": [{"type": "text", "content": "Done."}], "finish_reason": "stop"}]
def test_thread_paginates_with_cursors(