Skip to content

Commit 03313cd

Browse files
authored
feat (ai): expose response id, response model, response timestamp in telemetry and api (#2893)
1 parent b93f963 commit 03313cd

File tree

62 files changed

+2605
-710
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+2605
-710
lines changed

‎.changeset/calm-walls-sin.md‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
'@ai-sdk/provider-utils': patch
3+
'@ai-sdk/anthropic': patch
4+
'@ai-sdk/provider': patch
5+
'@ai-sdk/mistral': patch
6+
'@ai-sdk/cohere': patch
7+
'@ai-sdk/openai': patch
8+
'ai': patch
9+
---
10+
11+
feat (ai): expose response id, response model, response timestamp in telemetry and api

‎content/docs/03-ai-sdk-core/60-telemetry.mdx‎

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -61,41 +61,47 @@ const result = await generateText({
6161

6262
`generateText` records 3 types of spans:
6363

64-
- `ai.generateText`: the full length of the generateText call. It contains 1 or more `ai.generateText.doGenerate` spans.
64+
- `ai.generateText` (span): the full length of the generateText call. It contains 1 or more `ai.generateText.doGenerate` spans.
6565
It contains the [basic LLM span information](#basic-llm-span-information) and the following attributes:
66+
6667
- `operation.name`: `ai.generateText` and the functionId that was set through `telemetry.functionId`
6768
- `ai.operationId`: `"ai.generateText"`
6869
- `ai.prompt`: the prompt that was used when calling `generateText`
6970
- `ai.response.text`: the text that was generated
7071
- `ai.response.toolCalls`: the tool calls that were made as part of the generation (stringified JSON)
7172
- `ai.response.finishReason`: the reason why the generation finished
7273
- `ai.settings.maxToolRoundtrips`: the maximum number of tool roundtrips that were set
73-
- `ai.generateText.doGenerate`: a provider doGenerate call. It can contain `ai.toolCall` spans.
74-
It contains the [basic LLM span information](#basic-llm-span-information) and the following attributes:
74+
75+
- `ai.generateText.doGenerate` (span): a provider doGenerate call. It can contain `ai.toolCall` spans.
76+
It contains the [call LLM span information](#call-llm-span-information) and the following attributes:
77+
7578
- `operation.name`: `ai.generateText.doGenerate` and the functionId that was set through `telemetry.functionId`
7679
- `ai.operationId`: `"ai.generateText.doGenerate"`
7780
- `ai.prompt.format`: the format of the prompt
7881
- `ai.prompt.messages`: the messages that were passed into the provider
7982
- `ai.response.text`: the text that was generated
8083
- `ai.response.toolCalls`: the tool calls that were made as part of the generation (stringified JSON)
8184
- `ai.response.finishReason`: the reason why the generation finished
82-
- `ai.toolCall`: a tool call that is made as part of the generateText call. See [Tool call spans](#tool-call-spans) for more details.
85+
86+
- `ai.toolCall` (span): a tool call that is made as part of the generateText call. See [Tool call spans](#tool-call-spans) for more details.
8387

8488
### streamText function
8589

86-
`streamText` records 3 types of spans:
90+
`streamText` records 3 types of spans and 2 types of events:
8791

88-
- `ai.streamText`: the full length of the streamText call. It contains a `ai.streamText.doStream` span.
92+
- `ai.streamText` (span): the full length of the streamText call. It contains a `ai.streamText.doStream` span.
8993
It contains the [basic LLM span information](#basic-llm-span-information) and the following attributes:
94+
9095
- `operation.name`: `ai.streamText` and the functionId that was set through `telemetry.functionId`
9196
- `ai.operationId`: `"ai.streamText"`
9297
- `ai.prompt`: the prompt that was used when calling `streamText`
9398
- `ai.response.text`: the text that was generated
9499
- `ai.response.toolCalls`: the tool calls that were made as part of the generation (stringified JSON)
95100
- `ai.response.finishReason`: the reason why the generation finished
96-
- `ai.streamText.doStream`: a provider doStream call.
101+
102+
- `ai.streamText.doStream` (span): a provider doStream call.
97103
This span contains an `ai.stream.firstChunk` event and `ai.toolCall` spans.
98-
It contains the [basic LLM span information](#basic-llm-span-information) and the following attributes:
104+
It contains the [call LLM span information](#call-llm-span-information) and the following attributes:
99105

100106
- `operation.name`: `ai.streamText.doStream` and the functionId that was set through `telemetry.functionId`
101107
- `ai.operationId`: `"ai.streamText.doStream"`
@@ -108,19 +114,23 @@ const result = await generateText({
108114
- `ai.response.avgCompletionTokensPerSecond`: the average number of completion tokens per second
109115
- `ai.response.finishReason`: the reason why the generation finished
110116

117+
- `ai.toolCall` (span): a tool call that is made as part of the generateText call. See [Tool call spans](#tool-call-spans) for more details.
118+
111119
- `ai.stream.firstChunk` (event): an event that is emitted when the first chunk of the stream is received.
120+
112121
- `ai.response.msToFirstChunk`: the time it took to receive the first chunk
122+
113123
- `ai.stream.finish` (event): an event that is emitted when the finish part of the LLM stream is received.
114-
- `ai.toolCall`: a tool call that is made as part of the generateText call. See [Tool call spans](#tool-call-spans) for more details.
115124

116125
It also records a `ai.stream.firstChunk` event when the first chunk of the stream is received.
117126

118127
### generateObject function
119128

120129
`generateObject` records 2 types of spans:
121130

122-
- `ai.generateObject`: the full length of the generateObject call. It contains 1 or more `ai.generateObject.doGenerate` spans.
131+
- `ai.generateObject` (span): the full length of the generateObject call. It contains 1 or more `ai.generateObject.doGenerate` spans.
123132
It contains the [basic LLM span information](#basic-llm-span-information) and the following attributes:
133+
124134
- `operation.name`: `ai.generateObject` and the functionId that was set through `telemetry.functionId`
125135
- `ai.operationId`: `"ai.generateObject"`
126136
- `ai.prompt`: the prompt that was used when calling `generateObject`
@@ -130,8 +140,10 @@ It also records a `ai.stream.firstChunk` event when the first chunk of the strea
130140
- `ai.response.object`: the object that was generated (stringified JSON)
131141
- `ai.settings.mode`: the object generation mode, e.g. `json`
132142
- `ai.settings.output`: the output type that was used, e.g. `object` or `no-schema`
133-
- `ai.generateObject.doGenerate`: a provider doGenerate call.
134-
It contains the [basic LLM span information](#basic-llm-span-information) and the following attributes:
143+
144+
- `ai.generateObject.doGenerate` (span): a provider doGenerate call.
145+
It contains the [call LLM span information](#call-llm-span-information) and the following attributes:
146+
135147
- `operation.name`: `ai.generateObject.doGenerate` and the functionId that was set through `telemetry.functionId`
136148
- `ai.operationId`: `"ai.generateObject.doGenerate"`
137149
- `ai.prompt.format`: the format of the prompt
@@ -142,10 +154,11 @@ It also records a `ai.stream.firstChunk` event when the first chunk of the strea
142154

143155
### streamObject function
144156

145-
`streamObject` records 2 types of spans:
157+
`streamObject` records 2 types of spans and 1 type of event:
146158

147-
- `ai.streamObject`: the full length of the streamObject call. It contains 1 or more `ai.streamObject.doStream` spans.
159+
- `ai.streamObject` (span): the full length of the streamObject call. It contains 1 or more `ai.streamObject.doStream` spans.
148160
It contains the [basic LLM span information](#basic-llm-span-information) and the following attributes:
161+
149162
- `operation.name`: `ai.streamObject` and the functionId that was set through `telemetry.functionId`
150163
- `ai.operationId`: `"ai.streamObject"`
151164
- `ai.prompt`: the prompt that was used when calling `streamObject`
@@ -155,9 +168,11 @@ It also records a `ai.stream.firstChunk` event when the first chunk of the strea
155168
- `ai.response.object`: the object that was generated (stringified JSON)
156169
- `ai.settings.mode`: the object generation mode, e.g. `json`
157170
- `ai.settings.output`: the output type that was used, e.g. `object` or `no-schema`
158-
- `ai.streamObject.doStream`: a provider doStream call.
171+
172+
- `ai.streamObject.doStream` (span): a provider doStream call.
159173
This span contains an `ai.stream.firstChunk` event.
160-
It contains the [basic LLM span information](#basic-llm-span-information) and the following attributes:
174+
It contains the [call LLM span information](#call-llm-span-information) and the following attributes:
175+
161176
- `operation.name`: `ai.streamObject.doStream` and the functionId that was set through `telemetry.functionId`
162177
- `ai.operationId`: `"ai.streamObject.doStream"`
163178
- `ai.prompt.format`: the format of the prompt
@@ -166,21 +181,25 @@ It also records a `ai.stream.firstChunk` event when the first chunk of the strea
166181
- `ai.response.object`: the object that was generated (stringified JSON)
167182
- `ai.response.msToFirstChunk`: the time it took to receive the first chunk
168183
- `ai.response.finishReason`: the reason why the generation finished
184+
169185
- `ai.stream.firstChunk` (event): an event that is emitted when the first chunk of the stream is received.
170186
- `ai.response.msToFirstChunk`: the time it took to receive the first chunk
171187

172188
### embed function
173189

174190
`embed` records 2 types of spans:
175191

176-
- `ai.embed`: the full length of the embed call. It contains 1 `ai.embed.doEmbed` spans.
192+
- `ai.embed` (span): the full length of the embed call. It contains 1 `ai.embed.doEmbed` spans.
177193
It contains the [basic embedding span information](#basic-embedding-span-information) and the following attributes:
194+
178195
- `operation.name`: `ai.embed` and the functionId that was set through `telemetry.functionId`
179196
- `ai.operationId`: `"ai.embed"`
180197
- `ai.value`: the value that was passed into the `embed` function
181198
- `ai.embedding`: a JSON-stringified embedding
182-
- `ai.embed.doEmbed`: a provider doEmbed call.
199+
200+
- `ai.embed.doEmbed` (span): a provider doEmbed call.
183201
It contains the [basic embedding span information](#basic-embedding-span-information) and the following attributes:
202+
184203
- `operation.name`: `ai.embed.doEmbed` and the functionId that was set through `telemetry.functionId`
185204
- `ai.operationId`: `"ai.embed.doEmbed"`
186205
- `ai.values`: the values that were passed into the provider (array)
@@ -190,14 +209,17 @@ It also records a `ai.stream.firstChunk` event when the first chunk of the strea
190209

191210
`embedMany` records 2 types of spans:
192211

193-
- `ai.embedMany`: the full length of the embedMany call. It contains 1 or more `ai.embedMany.doEmbed` spans.
212+
- `ai.embedMany` (span): the full length of the embedMany call. It contains 1 or more `ai.embedMany.doEmbed` spans.
194213
It contains the [basic embedding span information](#basic-embedding-span-information) and the following attributes:
214+
195215
- `operation.name`: `ai.embedMany` and the functionId that was set through `telemetry.functionId`
196216
- `ai.operationId`: `"ai.embedMany"`
197217
- `ai.values`: the values that were passed into the `embedMany` function
198218
- `ai.embeddings`: an array of JSON-stringified embedding
199-
- `ai.embedMany.doEmbed`: a provider doEmbed call.
219+
220+
- `ai.embedMany.doEmbed` (span): a provider doEmbed call.
200221
It contains the [basic embedding span information](#basic-embedding-span-information) and the following attributes:
222+
201223
- `operation.name`: `ai.embedMany.doEmbed` and the functionId that was set through `telemetry.functionId`
202224
- `ai.operationId`: `"ai.embedMany.doEmbed"`
203225
- `ai.values`: the values that were sent to the provider
@@ -219,6 +241,15 @@ Many spans that use LLMs (`ai.generateText`, `ai.generateText.doGenerate`, `ai.s
219241
- `ai.telemetry.metadata.*`: the metadata that was passed in through `telemetry.metadata`
220242
- `ai.usage.completionTokens`: the number of completion tokens that were used
221243
- `ai.usage.promptTokens`: the number of prompt tokens that were used
244+
245+
### Call LLM span information
246+
247+
Spans that correspond to individual LLM calls (`ai.generateText.doGenerate`, `ai.streamText.doStream`, `ai.generateObject.doGenerate`, `ai.streamObject.doStream`) contain
248+
[basic LLM span information](#basic-llm-span-information) and the following attributes:
249+
250+
- `ai.response.model`: the model that was used to generate the response. This can be different from the model that was requested if the provider supports aliases.
251+
- `ai.response.id`: the id of the response. Uses the ID from the provider when available.
252+
- `ai.response.timestamp`: the timestamp of the response. Uses the timestamp from the provider when available.
222253
- [Semantic Conventions for GenAI operations](https://opentelemetry.io/docs/specs/semconv/gen-ai/gen-ai-spans/)
223254
- `gen_ai.system`: the provider that was used
224255
- `gen_ai.request.model`: the model that was requested
@@ -230,6 +261,8 @@ Many spans that use LLMs (`ai.generateText`, `ai.generateText.doGenerate`, `ai.s
230261
- `gen_ai.request.top_p`: the topP parameter value that was set
231262
- `gen_ai.request.stop_sequences`: the stop sequences
232263
- `gen_ai.response.finish_reasons`: the finish reasons that were returned by the provider
264+
- `gen_ai.response.model`: the model that was used to generate the response. This can be different from the model that was requested if the provider supports aliases.
265+
- `gen_ai.response.id`: the id of the response. Uses the ID from the provider when available.
233266
- `gen_ai.usage.input_tokens`: the number of prompt tokens that were used
234267
- `gen_ai.usage.output_tokens`: the number of completion tokens that were used
235268

‎content/docs/07-reference/ai-sdk-core/01-generate-text.mdx‎

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -457,19 +457,37 @@ To see `generateText` in action, check out [these examples](#examples).
457457
],
458458
},
459459
{
460-
name: 'rawResponse',
461-
type: 'RawResponse',
460+
name: 'response',
461+
type: 'Response',
462462
optional: true,
463-
description: 'Optional raw response data.',
463+
description: 'Response metadata.',
464464
properties: [
465465
{
466-
type: 'RawResponse',
466+
type: 'Response',
467467
parameters: [
468+
{
469+
name: 'id',
470+
type: 'string',
471+
description:
472+
'The response identifier. The AI SDK uses the ID from the provider response when available, and generates an ID otherwise.',
473+
},
474+
{
475+
name: 'model',
476+
type: 'string',
477+
description:
478+
'The model that was used to generate the response. The AI SDK uses the response model from the provider response when available, and the model from the function call otherwise.',
479+
},
480+
{
481+
name: 'timestamp',
482+
type: 'Date',
483+
description:
484+
'The timestamp of the response. The AI SDK uses the response timestamp from the provider response when available, and creates a timestamp otherwise.',
485+
},
468486
{
469487
name: 'headers',
470488
optional: true,
471489
type: 'Record<string, string>',
472-
description: 'Response headers.',
490+
description: 'Optional response headers.',
473491
},
474492
],
475493
},
@@ -552,19 +570,37 @@ To see `generateText` in action, check out [these examples](#examples).
552570
],
553571
},
554572
{
555-
name: 'rawResponse',
556-
type: 'RawResponse',
573+
name: 'response',
574+
type: 'Response',
557575
optional: true,
558-
description: 'Optional raw response data.',
576+
description: 'Response metadata.',
559577
properties: [
560578
{
561-
type: 'RawResponse',
579+
type: 'Response',
562580
parameters: [
581+
{
582+
name: 'id',
583+
type: 'string',
584+
description:
585+
'The response identifier. The AI SDK uses the ID from the provider response when available, and generates an ID otherwise.',
586+
},
587+
{
588+
name: 'model',
589+
type: 'string',
590+
description:
591+
'The model that was used to generate the response. The AI SDK uses the response model from the provider response when available, and the model from the function call otherwise.',
592+
},
593+
{
594+
name: 'timestamp',
595+
type: 'Date',
596+
description:
597+
'The timestamp of the response. The AI SDK uses the response timestamp from the provider response when available, and creates a timestamp otherwise.',
598+
},
563599
{
564600
name: 'headers',
565601
optional: true,
566602
type: 'Record<string, string>',
567-
description: 'Response headers.',
603+
description: 'Optional response headers.',
568604
},
569605
],
570606
},

0 commit comments

Comments
 (0)