Repository navigation
fix(vercel-ai-sdk): preserve streaming finish and response metadata - #445
brianstrauch merged 2 commits into
Conversation
| type: 'response-metadata', | ||
| id: result.response.id, | ||
| modelId: result.response.modelId, | ||
| timestamp: result.response.timestamp ? new Date(result.response.timestamp) : undefined, |
There was a problem hiding this comment.
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', |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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.
…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`.
… 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.
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-sdkwithpnpm run build && pnpm run test --match='*metadata*': with themainversions ofactivities.tsandprovider.tsand the new tests, two of the three tests fail; with the fix, all three pass.pnpm run lintpasses.This change and its regression tests were generated with OpenAI Codex.