How to get UIMessagePart creation timestamp with streamText?
can i know when a UIMessagePart gets created? like its createdAt? onStepFinish? but that's a "step"... i'm using streamText.
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