You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Backport: feat(ai): introduce experimental callbacks for embed function (#13637)
This is an automated backport of #13478 to the release-v6.0 branch. FYI
@aayush-kapoor
This backport has conflicts that need to be resolved manually.
### `git cherry-pick` output
```
Auto-merging content/docs/03-ai-sdk-core/65-event-listeners.mdx
Auto-merging packages/ai/src/embed/embed-many.test.ts
Auto-merging packages/ai/src/embed/embed.test.ts
CONFLICT (content): Merge conflict in packages/ai/src/embed/embed.test.ts
Auto-merging packages/ai/src/generate-text/callback-events.ts
error: could not apply ff9ce30... feat(ai): introduce experimental callbacks for embed function (#13478)
hint: After resolving the conflicts, mark them with
hint: "git add/rm <pathspec>", then run
hint: "git cherry-pick --continue".
hint: You can instead skip this commit with "git cherry-pick --skip".
hint: To abort and get back to the state before "git cherry-pick",
hint: run "git cherry-pick --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
```
---------
Co-authored-by: Aayush Kapoor <83492835+aayush-kapoor@users.noreply.github.com>
Co-authored-by: Aayush Kapoor <aayushkapoor34@gmail.com>
description: Subscribe to lifecycle events in generateTextand streamText calls
3
+
description: Subscribe to lifecycle events in generateText, streamText, embed, and embedMany calls
4
4
---
5
5
6
6
# Event Callbacks
7
7
8
-
The AI SDK provides per-call event callbacks that you can pass to `generateText`and `streamText` to observe lifecycle events. This is useful for building observability tools, logging systems, analytics, and debugging utilities.
8
+
The AI SDK provides per-call event callbacks that you can pass to `generateText`, `streamText`, `embed`, and `embedMany` to observe lifecycle events. This is useful for building observability tools, logging systems, analytics, and debugging utilities.
9
9
10
10
## Basic Usage
11
11
12
-
Pass callbacks directly to `generateText`or `streamText`:
12
+
Pass callbacks directly to `generateText`, `streamText`, `embed`, or `embedMany`:
13
13
14
14
```ts
15
15
import { generateText } from'ai';
@@ -28,6 +28,8 @@ const result = await generateText({
28
28
29
29
## Available Callbacks
30
30
31
+
### `generateText` / `streamText`
32
+
31
33
<PropertiesTable
32
34
content={[
33
35
{
@@ -65,9 +67,30 @@ const result = await generateText({
'Called when the embedding operation completes, after the embedding model returns.',
85
+
},
86
+
]}
87
+
/>
88
+
68
89
## Event Reference
69
90
70
-
### `experimental_onStart`
91
+
### `generateText` / `streamText`
92
+
93
+
#### `experimental_onStart`
71
94
72
95
Called when the generation operation begins, before any LLM calls are made.
73
96
@@ -221,7 +244,7 @@ const result = await generateText({
221
244
]}
222
245
/>
223
246
224
-
### `experimental_onStepStart`
247
+
####`experimental_onStepStart`
225
248
226
249
Called before each step (LLM call) begins. Useful for tracking multi-step generations.
227
250
@@ -335,7 +358,7 @@ const result = await generateText({
335
358
]}
336
359
/>
337
360
338
-
### `experimental_onToolCallStart`
361
+
####`experimental_onToolCallStart`
339
362
340
363
Called before a tool's `execute` function runs.
341
364
@@ -427,7 +450,7 @@ const result = await generateText({
427
450
]}
428
451
/>
429
452
430
-
### `experimental_onToolCallFinish`
453
+
####`experimental_onToolCallFinish`
431
454
432
455
Called after a tool's `execute` function completes or errors. Uses a discriminated union on the `success` field.
433
456
@@ -548,7 +571,7 @@ const result = await generateText({
548
571
]}
549
572
/>
550
573
551
-
### `onStepFinish`
574
+
####`onStepFinish`
552
575
553
576
Called after each step (LLM call) completes. Provides the full `StepResult`.
554
577
@@ -689,7 +712,7 @@ const result = await generateText({
689
712
]}
690
713
/>
691
714
692
-
### `onFinish`
715
+
####`onFinish`
693
716
694
717
Called when the entire generation completes (all steps finished). Includes aggregated data.
695
718
@@ -841,6 +864,164 @@ const result = await generateText({
841
864
]}
842
865
/>
843
866
867
+
### `embed` / `embedMany`
868
+
869
+
#### `experimental_onStart`
870
+
871
+
Called when the embedding operation begins, before the embedding model is called. Both `embed` and `embedMany` share the same event interface; the `operationId` field distinguishes them (`'ai.embed'` vs `'ai.embedMany'`), and the `value` field is a single string for `embed` or an array of strings for `embedMany`.
'Identifier from telemetry settings for grouping related operations.',
935
+
},
936
+
{
937
+
name: 'metadata',
938
+
type: 'Record<string, JSONValue> | undefined',
939
+
description: 'Additional metadata from telemetry settings.',
940
+
},
941
+
]}
942
+
/>
943
+
944
+
#### `experimental_onFinish`
945
+
946
+
Called when the embedding operation completes. For `embed`, `embedding` is a single vector and `response` is a single response object. For `embedMany`, `embedding` is an array of vectors and `response` is an array of response objects (one per chunk).
Errors thrown inside callbacks are caught and do not break the generation flow. This ensures that monitoring code cannot disrupt your application:
1107
+
Errors thrown inside callbacks are caught and do not break the generation or embedding flow. This ensures that monitoring code cannot disrupt your application:
0 commit comments