How to get UIMessagePart creation timestamp with streamText?

HEAD
0927570Version Packages (#12144)
This post might have stale content, as HEAD is 126 commits ahead.

can i know when a UIMessagePart gets created? like its createdAt? onStepFinish? but that's a "step"... i'm using streamText.

Avatar of anthropic/claude-opus-4.5
Claude Opus 4.5
commented

Now I have the full picture. Let me summarize what I've found:


##Answer

Unfortunately, there's no built-in createdAt timestamp for individual UIMessagePart objects. The SDK doesn't track when each part is created during streaming.

Here's what timing information is available:

###1. Step-level timing via onStepFinish

The response.timestamp in StepResult gives you the timestamp for when the response started (not individual parts):

const result = streamText({
  model,
  prompt,
  onStepFinish(stepResult) {
    // stepResult.response.timestamp - when this step's response started
    console.log(stepResult.response.timestamp);
  },
});

###2. Chunk-level callback via onChunk

You can track when chunks arrive yourself by recording the time in the callback:

const result = streamText({
  model,
  prompt,
  onChunk({ chunk }) {
    const receivedAt = new Date();
    // chunk can be: text-delta, reasoning-delta, source, tool-call, 
    // tool-input-start, tool-input-delta, tool-result, raw
    console.log(`${chunk.type} received at ${receivedAt}`);
  },
});

###3. Stream parts have IDs but no timestamps

Looking at TextStreamPart, you get id, type, and providerMetadata but no timestamp:

// From stream-text-result.ts
type TextStreamPart = 
  | { type: 'text-start'; id: string; providerMetadata?: ProviderMetadata; }
  | { type: 'text-delta'; id: string; text: string; providerMetadata?: ProviderMetadata; }
  // ... etc

###Workaround: Track it yourself

If you need per-part creation times, you could do something like:

const partTimestamps = new Map<string, Date>();

const result = streamText({
  model,
  prompt,
  onChunk({ chunk }) {
    if ('id' in chunk && !partTimestamps.has(chunk.id)) {
      partTimestamps.set(chunk.id, new Date());
    }
  },
});

This would let you track when each part (by its id) was first received during streaming.


END OF POST

How to get UIMessagePart creation timestamp with streamText? — vercel/ai