-
Notifications
You must be signed in to change notification settings - Fork 482
Description
Describe the bug
There is a type inference issue with the stream property on the GenerateStreamResponse interface. The generateStream method is defined as:
generateStream<O extends z.ZodTypeAny = z.ZodTypeAny, CustomOptions extends z.ZodTypeAny = typeof GenerationCommonConfigSchema>(parts: GenerateOptions<O, CustomOptions> | PromiseLike<GenerateOptions<O, CustomOptions>>): GenerateStreamResponse<z.infer<O>>;
It correctly returns a GenerateStreamResponse<z.infer>. However, the definition of GenerateStreamResponse is:
interface GenerateStreamResponse<O extends z.ZodTypeAny = z.ZodTypeAny> {
get stream(): AsyncIterable<GenerateResponseChunk>; // Lacks the generic <O>
get response(): Promise<GenerateResponse<O>>;
}
The stream getter returns an AsyncIterable<GenerateResponseChunk>
, but the GenerateResponseChunk
itself is not correctly typed with the generic O
. This results in the stream's chunks always being of type GenerateResponseChunk<unknown>
, forcing users to perform type assertions and losing the benefit of the schema provided.
To Reproduce
The issue can be observed in the typings when using the generateStream method with a Zod schema.
import { Genkit } from 'genkit';
import { z } from 'zod';
const JokeSchema = z.object({
joke: z.string(),
punchline: z.string(),
});
async function getJokeStream() {
// Assume genkit is configured
const genkit = new Genkit();
const streamResponse = await genkit.generateStream({
prompt: 'Tell me a short, funny joke.',
output: {
schema: JokeSchema,
},
});
// Problem: `chunk` is incorrectly typed as `GenerateResponseChunk<unknown>`
// instead of `GenerateResponseChunk<{ joke: string; punchline: string; }>`
for await (const chunk of streamResponse.stream) {
// Accessing chunk properties lacks type safety.
console.log(chunk.text);
}
}
Expected behavior
The stream property on GenerateStreamResponse should propagate the generic type O to GenerateResponseChunk. This would correctly type the chunks within the async iterable, providing proper type safety and autocompletion based on the Zod schema passed to the generateStream method.
The resulting type for the stream should be AsyncIterable<GenerateResponseChunk<z.infer>>.
Runtime
OS: Windows 11 Home
Version: 24H2
Node version
v22.18.0
Additional context
This issue can be resolved with a minor change to the GenerateStreamResponse interface.
Current Definition:
interface GenerateStreamResponse<O extends z.ZodTypeAny = z.ZodTypeAny> {
get stream(): AsyncIterable<GenerateResponseChunk>;
get response(): Promise<GenerateResponse<O>>;
}
Proposed Fix:
interface GenerateStreamResponse<O extends z.ZodTypeAny = z.ZodTypeAny> {
get stream(): AsyncIterable<GenerateResponseChunk<O>>; // Propagate the <O> generic
get response(): Promise<GenerateResponse<O>>;
}
Metadata
Metadata
Assignees
Labels
Type
Projects
Status