Skip to content

Commit 0527ee3

Browse files
authored
fix(node): Preserve synchronous return behavior for streamText and other methods for AI (#17580)
Vercel AI integration was incorrectly wrapping all patched methods in an async function, causing synchronous methods like streamText to return Promises when they should return synchronous objects. ``` // Before const result = streamText({model: "gpt-5"}); console.log(result); ``` ``` // After const result = streamText({model: "gpt-5"}); console.log(result); // DefaultStreamTextResult { ... } ✅ ``` Resolves: #17553
1 parent f742746 commit 0527ee3

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

packages/node/src/integrations/tracing/vercelai/instrumentation.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
getActiveSpan,
1010
getCurrentScope,
1111
handleCallbackErrors,
12+
isThenable,
1213
SDK_VERSION,
1314
withScope,
1415
} from '@sentry/core';
@@ -71,7 +72,7 @@ function isToolError(obj: unknown): obj is ToolError {
7172
* Check for tool errors in the result and capture them
7273
* Tool errors are not rejected in Vercel V5, it is added as metadata to the result content
7374
*/
74-
function checkResultForToolErrors(result: unknown | Promise<unknown>): void {
75+
function checkResultForToolErrors(result: unknown): void {
7576
if (typeof result !== 'object' || result === null || !('content' in result)) {
7677
return;
7778
}
@@ -236,13 +237,18 @@ export class SentryVercelAiInstrumentation extends InstrumentationBase {
236237
};
237238

238239
return handleCallbackErrors(
239-
async () => {
240+
() => {
240241
// @ts-expect-error we know that the method exists
241-
const result = await originalMethod.apply(this, args);
242+
const result = originalMethod.apply(this, args);
242243

243-
// Tool errors are not rejected in Vercel V5, it is added as metadata to the result content
244-
checkResultForToolErrors(result);
244+
if (isThenable(result)) {
245+
// check for tool errors when the promise resolves, keep the original promise identity
246+
result.then(checkResultForToolErrors, () => {});
247+
return result;
248+
}
245249

250+
// check for tool errors when the result is synchronous
251+
checkResultForToolErrors(result);
246252
return result;
247253
},
248254
error => {

0 commit comments

Comments
 (0)