feat(telemetry): Add OTel GenAI semantic conventions compliance #12121
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fixes: #6673
This PR makes the AI SDK telemetry compliant with OpenTelemetry GenAI Semantic Conventions.
Target Version: AI SDK v6 (uses
LanguageModelV3types)Changes
SpanKind.CLIENTfor LLM API call spans (doGenerate/doStream){operation} {model}format (e.g.,chat gpt-4)gen_ai.operation.nameattribute (chat,text_completion,embeddings)gen_ai.provider.nameattribute (alongside deprecatedgen_ai.system)gen_ai.input.messageswith OTel message schema (role/parts format)gen_ai.output.messageswith OTel message schemagen_ai.tool.definitionswith OTel tool schemaFiles Modified
packages/ai/src/telemetry/record-span.ts- Addedkindparameterpackages/ai/src/telemetry/convert-to-otel-genai-messages.ts- NEW: conversion helperspackages/ai/src/telemetry/convert-to-otel-genai-messages.test.ts- NEW: testspackages/ai/src/generate-text/stream-text.tspackages/ai/src/generate-text/generate-text.tspackages/ai/src/generate-object/generate-object.tspackages/ai/src/generate-object/stream-object.tsOTel Message Schema
Messages now follow the OTel GenAI format:
[ {"role": "user", "parts": [{"type": "text", "content": "Hello"}]}, {"role": "assistant", "parts": [ {"type": "text", "content": "Hi!"}, {"type": "tool_call", "id": "call_123", "name": "get_weather", "arguments": {...}} ]}, {"role": "tool", "parts": [{"type": "tool_call_response", "id": "call_123", "result": "..."}]} ]