Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import * as Sentry from '@sentry/node';
import { createOpenAI } from '@ai-sdk/openai';
import { stepCountIs, streamText, tool } from 'ai';
import { z } from 'zod';

const usage = {
input_tokens: 10,
input_tokens_details: { cached_tokens: 0 },
output_tokens: 20,
output_tokens_details: { reasoning_tokens: 0 },
};

const toolCallResponse = [
{
type: 'response.created',
response: { id: 'resp_1', created_at: 1700000000, model: 'gpt-test', service_tier: null },
},
{
type: 'response.output_item.added',
output_index: 0,
item: { type: 'function_call', id: 'fc_1', call_id: 'call_1', name: 'getWeather', arguments: '' },
},
{
type: 'response.output_item.done',
output_index: 0,
item: {
type: 'function_call',
id: 'fc_1',
call_id: 'call_1',
name: 'getWeather',
arguments: JSON.stringify({ location: 'San Francisco' }),
status: 'completed',
},
},
{ type: 'response.completed', response: { incomplete_details: null, service_tier: null, usage } },
];

const textResponse = [
{
type: 'response.created',
response: { id: 'resp_2', created_at: 1700000000, model: 'gpt-test', service_tier: null },
},
{ type: 'response.output_item.added', output_index: 0, item: { type: 'message', id: 'msg_1' } },
{ type: 'response.output_text.delta', item_id: 'msg_1', delta: 'Sunny, 72°F.', logprobs: [] },
{ type: 'response.output_item.done', output_index: 0, item: { type: 'message', id: 'msg_1' } },
{ type: 'response.completed', response: { incomplete_details: null, service_tier: null, usage } },
];

let callCount = 0;

// A real provider instead of MockLanguageModelV3: @ai-sdk/openai attaches a telemetry redactor to the
// request body when tools are sent, which makes `ai` publish a redacted copy of the model call result.
const openai = createOpenAI({
apiKey: 'test',
fetch: async () => {
const events = callCount++ === 0 ? toolCallResponse : textResponse;
return new Response(`${events.map(event => `data: ${JSON.stringify(event)}\n\n`).join('')}data: [DONE]\n\n`, {
headers: { 'content-type': 'text/event-stream' },
});
},
});

async function run() {
await Sentry.startSpan({ op: 'function', name: 'main' }, async () => {
const result = streamText({
model: openai.responses('gpt-test'),
tools: {
getWeather: tool({
description: 'Get the current weather for a location',
inputSchema: z.object({ location: z.string() }),
execute: async ({ location }) => `Weather in ${location}: Sunny, 72°F`,
}),
},
stopWhen: stepCountIs(5),
prompt: 'What is the weather in San Francisco?',
});

Sentry.captureMessage(`streamText returned: ${await result.text}`);
});
}

run();
Original file line number Diff line number Diff line change
Expand Up @@ -1769,3 +1769,70 @@ describe('Vercel AI calls with recording off (v7)', () => {
{ additionalDependencies: { ai: '^7.0.163' } },
);
});

// Regression test for https://github.com/getsentry/sentry-javascript/issues/26403: on `ai` 7.0.168-7.0.173,
// streamText with tools failed with "ReadableStream is locked" once Sentry was loaded, at any sample rate.
describe('Vercel AI streamText with tools on a real provider (v7)', () => {
const additionalDependencies = { ai: '^7.0.174', '@ai-sdk/openai': '^4.0.118' };

afterAll(() => {
cleanupChildProcesses();
});

createEsmTests(
__dirname,
'scenario-openai-stream-tools.mjs',
'instrument.mjs',
(createRunner, test) => {
test('returns the streamed text and records the model and tool calls', async () => {
await createRunner()
.unordered()
.expect({
event: event => {
expect(event.message).toBe('streamText returned: Sunny, 72°F.');
},
})
.expect({
span: container => {
const modelCalls = container.items.filter(
span => span.attributes[SENTRY_OP]?.value === GEN_AI_GENERATE_CONTENT,
);
expect(modelCalls).toHaveLength(2);
for (const modelCall of modelCalls) {
expect(modelCall.status).toBe('ok');
expect(modelCall.attributes[GEN_AI_USAGE_INPUT_TOKENS]?.value).toBe(10);
expect(modelCall.attributes[GEN_AI_USAGE_OUTPUT_TOKENS]?.value).toBe(20);
}

const toolSpan = container.items.find(span => span.attributes[SENTRY_OP]?.value === GEN_AI_EXECUTE_TOOL);
expect(toolSpan?.status).toBe('ok');
expect(toolSpan?.attributes[GEN_AI_TOOL_NAME]?.value).toBe('getWeather');
},
})
.start()
.completed();
});
},
{ additionalDependencies },
);

createEsmTests(
__dirname,
'scenario-openai-stream-tools.mjs',
'instrument-unsampled.mjs',
(createRunner, test) => {
test('returns the streamed text in an unsampled trace', async () => {
await createRunner()
.unordered()
.expect({
event: event => {
expect(event.message).toBe('streamText returned: Sunny, 72°F.');
},
})
.start()
.completed();
});
},
{ additionalDependencies },
);
});
Loading