Skip to content

fix(vercel-ai-sdk): preserve streaming finish and response metadata - #445

Merged
brianstrauch merged 2 commits into
temporalio:mainfrom
pcbeingused333:fix/stream-provider-metadata-c1948
Nov 8, 2026
Merged

brianstrauch merged 2 commits into
temporalio:mainfrom
pcbeingused333:fix/stream-provider-metadata-c1948

Conversation

@pcbeingused333

Copy link
Copy Markdown
Contributor

Streaming model calls lose finish-level provider metadata between the model activity and the workflow. The activity publishes the raw finish event, but omits its providerMetadata from the accumulated result; the workflow then reconstructs a finish event without it. The reconstructed stream also omits the response-metadata event, so AI SDK consumers cannot recover the original response ID, model ID or timestamp from the stream.

Preserve finish providerMetadata in the activity result and replay it on the finish event. Emit response-metadata before the reconstructed content, converting the serialized timestamp back to a Date.

Testing and validation

Added activity regressions for finish and text provider metadata and extended the workflow streaming regression to assert finish metadata and response identity, model and timestamp. The expected workflow result uses an ISO timestamp because the workflow result crosses the JSON data converter.

Validated on Node 22.22 in typescript/vercel-ai-sdk with pnpm run build && pnpm run test --match='*metadata*': with the main versions of activities.ts and provider.ts and the new tests, two of the three tests fail; with the fix, all three pass. pnpm run lint passes.

This change and its regression tests were generated with OpenAI Codex.

@pcbeingused333
pcbeingused333 requested a review from a team as a code owner November 8, 2026 03:28

@brianstrauch brianstrauch left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@brianstrauch
brianstrauch enabled auto-merge (squash) November 8, 2026 19:42
@brianstrauch
brianstrauch merged commit 028dd85 into temporalio:main Nov 8, 2026
15 checks passed

@moetemp moetemp left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Fixes are in #466 and #485.

type: 'response-metadata',
id: result.response.id,
modelId: result.response.modelId,
timestamp: result.response.timestamp ? new Date(result.response.timestamp) : undefined,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth reusing the isNaN check from doGenerate here? An invalid timestamp string gives the consumer an Invalid Date, and then the AI SDK can't fall back to its own value.

Applied in #485.

controller.enqueue({ type: 'stream-start', warnings: result.warnings ?? [] });
if (result.response !== undefined) {
controller.enqueue({
type: 'response-metadata',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part goes out whenever response exists. For OpenAI that's every call, because of headers, even when id, modelId and timestamp are all undefined. Is it worth sending it only when one of them is set? It's replay-safe as is, since streamText merges with ??. It's only extra noise for code that counts parts.

Did this in #485.

let providerMetadata: SharedV4ProviderMetadata | undefined;
let sawFinish = false;
let sawError = false;
let streamError: unknown;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not from this PR, but it's close by. Error parts are often plain objects, so the failure message below reads [object Object]. Could we use getErrorMessage from @ai-sdk/provider-utils when we build it?

Done in #466.

moetemp added a commit that referenced this pull request Nov 13, 2026
…esponse metadata (#485)

### What changed?

This PR fixes the points from my reviews of #162, #445 and #386.

- The duplicate Activity error says where the other Activity is from and
how to fix it. For two `AiSdkPlugin`s, it suggests one `modelProvider`
built with `customProvider`.
- The duplicate error still shows if `nativeWorker.initiateShutdown` is
missing.
- `AiSdkPlugin` checks its own `createActivities` result, not a cast of
`options.activities`.
- Two `AiSdkPlugin`s with `workflowsPath` don't fail in webpack anymore.
- `doStream` drops an invalid `timestamp`.

Fixes #AI-963

### Why?

The duplicate error didn't tell a user with two providers how to keep
both. The rest are small gaps next to that code.

The stream keeps the same parts as before, so Workflow history doesn't
change.

### How did you test it?

- [x] Unit tests. `make lint` and `make test` pass in
`typescript/vercel-ai-sdk`.
brianstrauch pushed a commit that referenced this pull request Nov 14, 2026
… boundary (#538)

No separate issue: sibling of #445, which keeps finish metadata for
streaming in the Vercel integration.

**Cause.** `ChatModelActivityImpl.toOutput` maps each Spring AI
`Generation` to the activity's `Generation(message)` record and drops
`getMetadata().getFinishReason()`. On the workflow side
`ActivityChatModel.toResponse` rebuilds `Generation`s without
`ChatGenerationMetadata`, so
`response.getResult().getMetadata().getFinishReason()` is always null in
workflow code. Callers cannot tell a `length` truncation or
`content_filter` from a normal `stop`.

**Fix.** Add an optional `finish_reason` field to
`ChatModelActivityOutput.Generation` (the one-argument constructor is
kept; the field is omitted when null and ignored when absent, so
previously recorded histories still deserialize). `toOutput` fills it
from the generation metadata and `toResponse` restores it as
`ChatGenerationMetadata`.

**Test.** `FinishReasonTest` runs a workflow against a stub model
returning `finishReason("length")` and asserts the workflow sees
`length`: fails on main (null) and passes with this change;
`ChatModelActivityImplTest` still passes.

**Recorded histories.** New chat activity results carry an extra
`finish_reason` field. This does not affect the replay of in-flight
runs: results recorded before the change deserialize with a null finish
reason, and workers without the change ignore the unknown field
(`@JsonIgnoreProperties(ignoreUnknown = true)` on `Generation`). No
workflow commands change.

This change was prepared with the help of an AI assistant (Claude) and
submitted by the account owner.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants