Validating streamed summaries with Vercel AI SDK and Zod
Stream a structured Jekyll release summary and see when AI SDK yields partial data, validates the final object, and rejects an invalid result.
I used Vercel AI SDK and Zod to turn the Jekyll 4.4.0 release notes into a structured summary. The stream gave me enough to show a preview before the SDK had a complete object to validate.
I checked this behavior with Node.js 22.23.3, Vercel AI SDK
7.0.185, and Zod4.6.7. The SDK’sMockLanguageModelV4returned fixed JSON chunks so I could observe the SDK and Zod without a provider call. AI SDK testing
Define the shape of a release summary
I wanted the release page to show routine changes and compatibility changes in separate lists. Jekyll 4.4.0 made this a concrete example. It raised the minimum Ruby version to 2.7, added a Liquid option for choosing highlighted lines in a code block, and declared csv, base64, and json as runtime dependencies. The Ruby change belongs in the compatibility list so a reader can spot it before upgrading. Jekyll 4.4.0 release
I kept the same keys as in the OpenRouter JSON Schema exercise. The object needs a version string and two arrays of strings, features and breaking_changes. An empty array represents a list with no entries. I used z.strictObject to reject keys outside those three. This gives the application a stable shape to read and makes a one-off key like remarks a validation error. Zod object schemas
Let the SDK apply the schema
In the OpenRouter exercise I built the JSON Schema, attached it to the request, and validated the parsed response in Python. Here I gave the Zod schema to Output.object({ schema }). In the local test, I inspected the options received by the mock model. They included a JSON response format with all three required keys and a restriction against additional properties. The SDK then parsed the final model text and validated it with Zod. For a production provider, support for the requested output format depends on the selected adapter and model. AI SDK Output reference
| What the application needs | Where the result comes from |
|---|---|
| A single validated object at the end | The output property on the result from generateText |
| A preview while the response arrives | The values emitted by streamText through partialOutputStream |
| An object to persist after a streaming response | The resolved output promise on the result from streamText |
For a job that only needs to save a summary, I’d start with generateText. On the streaming summary screen, streamText lets the app show progress while the structured summary is still forming. Both paths yielded a validated final object with the valid fixture. AI SDK structured output · streamText reference
Keep the schema and the partial-output loop together in the server function that calls the model. showPreview is a callback for the current request. You can use it to send data events to the UI and then handle the returned object when the request completes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { Output, streamText, type DeepPartial, type LanguageModel } from 'ai';
import { z } from 'zod';
const schema = z.strictObject({
version: z.string(),
features: z.array(z.string()),
breaking_changes: z.array(z.string()),
});
export async function streamSummary(
model: LanguageModel,
prompt: string,
showPreview: (snapshot: DeepPartial<z.infer<typeof schema>>) => void,
) {
const result = streamText({ model, prompt, output: Output.object({ schema }) });
for await (const snapshot of result.partialOutputStream) {
showPreview(snapshot);
}
return await result.output;
}
The caller handles a rejection from result.output. For each preview snapshot, render the fields that are ready to use.
Read the preview as it arrives
The first snapshot contained version with the value 4.. The next snapshot had 4.4.0 and the beginning of the first item in features. The breaking_changes key appeared later. The complete object held two features and the Ruby compatibility change.
Each partial value is a snapshot of the JSON the SDK can read so far. The version can be a fragment. An array can grow and the last string in it can gain more characters. A key that hasn’t arrived yet tells the UI to keep waiting. It doesn’t establish that the corresponding list will be empty. I also supplied a fixture with a string in features. That string appeared in a partial output even though the SDK rejected the final object. A preview can render fields it can use, such as an array it has checked at runtime, and leave the rest in a loading state. Partial and complete output
Check what fails at the end
I kept the same input and varied only the model’s fixed output. For each case I read the partial snapshots and awaited the final output. For failed cases, NoObjectGeneratedError.isInstance identified the structured output error. Its cause distinguished the invalid shapes from the JSON truncated before completion. The bad shapes still failed when the fixture reported a normal finish. NoObjectGeneratedError reference
| Fixture change | Final SDK result |
|---|---|
| All three fields in the expected shapes | Object returned |
A single string in features | Type validation failed |
breaking_changes missing or set to null | Type validation failed |
| An extra property the schema doesn’t define | Type validation failed |
JSON cut off with the finish reason length | JSON parsing failed |
An empty breaking_changes array | Schema validation passed |
The string 4.9.0 in version | Schema validation passed |
The last two cases expose the difference between checking a value’s shape and checking its meaning. An empty compatibility list is useful for other releases, but on this fixture it would omit the Ruby minimum version change. The version 4.9.0 still satisfies the string schema. In the test I compared it with the known 4.4.0 input version and rejected the mismatch. A similar source check can use a release tag fetched by the application. It takes a separate comparison to establish whether the listed changes are supported by the release notes.
Decide when to commit the summary
For a streaming screen, I’d show a preview while partial values arrive. Once the final promise resolves, I’d replace the preview with the validated result. If the promise rejects, the screen should show a failed generation state and leave any previously saved summary intact. This keeps a plausible-looking preview from replacing a valid record when a stream ends early or one field arrives in the wrong shape.
When a summary is going to be stored or published, I’d check its version against the release metadata and review the stated changes against the source before committing it. The preview makes the wait visible. The final object and source checks determine what the application can keep.