Skip to content

gen_ai.response.time_to_first_chunk fires on metadata and start events instead of first output chunk #27043

Done

Description

@adityareddy-dev

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/node

SDK Version

11.22.0

Framework Version

Node.js v26.13.1, openai 7.66.0

Link to Sentry event

No response

Reproduction Example/SDK Setup

import * as Sentry from '@sentry/node';
import OpenAI from 'openai';

Sentry.init({
  dsn: 'https://examplePublicKey@o0.ingest.sentry.io/0',
  tracesSampleRate: 1.0,
  transport: () => ({
    send: async () => ({}),
    flush: async () => true,
  }),
  beforeSendSpan(span) {
    console.log(span.name, span.attributes?.['gen_ai.response.time_to_first_chunk']);
    return span;
  },
});

const client = Sentry.instrumentOpenAiClient(
  new OpenAI({ apiKey: 'test', baseURL: 'http://127.0.0.1:4123/v1' }),
);
const stream = await client.chat.completions.create({
  model: 'gpt-4o-mini',
  stream: true,
  messages: [{ role: 'user', content: 'hi' }],
});
for await (const _chunk of stream) {
  // consume stream
}

Steps to Reproduce

Point baseURL at a local OpenAI Chat Completions SSE server for three cases:

  1. Metadata-only stream: send { choices: [{ delta: { role: 'assistant', content: '' } }] } at +30 ms, finish_reason: 'stop' at +130 ms, and usage at +180 ms (0 output tokens).
  2. Delayed first content: send the role-only chunk at +30 ms, then { delta: { content: 'Hello' } } at +280 ms and ' world' at +400 ms.
  3. Tool-call-only stream: send the role-only chunk at +30 ms, then delta.tool_calls at +220 ms and +340 ms (content: null).

Expected Result

In getsentry/sentry-conventions v0.34.0 (gen_ai__response__time_to_first_chunk.json, PR #795):

A chunk is a decoded provider stream event carrying new, non-empty output: text, refusals, reasoning (including opaque payloads), audio, images, or tool-call names or arguments. Empty start events or events carrying only usage, roles, IDs, signatures, keep-alives, progress/status updates, or end markers are not chunks. Chunk boundaries follow provider events, not TCP reads, HTTP frames, or extra SDK callbacks.

[...]

Set this attribute when the first chunk is received and do not set it if no chunk is received.

  • Case 1 (metadata-only): gen_ai.response.time_to_first_chunk is omitted.
  • Case 2 (delayed content): gen_ai.response.time_to_first_chunk is ~0.28 s.
  • Case 3 (tool-call-only): gen_ai.response.time_to_first_chunk is ~0.22 s.

Actual Result

On @sentry/node 11.22.0 (same behavior back to 11.15.0, introduced in #26465):

  • Case 1 (metadata-only): gen_ai.response.time_to_first_chunk is set to 0.0451 s even with 0 output tokens.
  • Case 2 (delayed content): gen_ai.response.time_to_first_chunk is 0.0342 s (~8x too low, recorded on the +30 ms role chunk).
  • Case 3 (tool-call-only): gen_ai.response.time_to_first_chunk is 0.0342 s (~6.5x too low, recorded on the +30 ms role chunk).

Additional Context

The same early recordTimeToFirstChunk(span) call shows up across other streaming wrappers in packages/server-utils/src/:

  • ai/openai/streaming.ts (line 250): fires on every first event in for await, including empty role/usage chunks and Responses API response.created / response.in_progress events.
  • ai/anthropic-ai/streaming.ts (line 211): fires before handleMessageMetadata, so message_start and ping record TTFC before any output content block or content_block_delta.
  • integrations/vercel-ai/util.ts (lines 120-121): only skips stream-start, still recording on response-metadata, text-start, reasoning-start, tool-input-start, and finish.
  • ai/pi-ai/chat.ts (lines 135-136): only skips start.
  • ai/google-genai/streaming.ts (line 116), ai/mistral/streaming.ts (line 257), and ai/workers-ai/streaming.ts (line 238, which fires on raw byte reads before SSE event decoding).

For reference, ai/tanstack-ai/index.ts (lines 204-205) already gates recordTimeToFirstChunk on OUTPUT_CHUNK_TYPES.

If you want this fixed, I can open a PR against develop to only call recordTimeToFirstChunk on real output chunks, with unit tests for these providers.

Priority

React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding +1 or me too, to help us triage it.

Activity

  1. linear-code commented on Dec 1, 2026

    @linear-code
  2. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Dec 1, 2026
  3. added
    javascript
    on Dec 1, 2026
  4. andreiborza commented on Dec 2, 2026

    @andreiborza
    Member

    Hi @adityareddy-dev, thanks for filing. We'll fix it.

  5. self-assigned this
    on Dec 2, 2026
  6. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Dec 2, 2026
  7. github-actions commented on Dec 2, 2026

    @github-actions
    Contributor

    A PR closing this issue has just been released 🚀

    This issue was referenced by PR #27052, which was included in the 11.23.0 release.

  8. added a commit that references this issue on Dec 2, 2026
    d2c2a5a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

BugNode.jsSpansjavascript

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions