-
Hi there 👋, Most examples (express, Next.js) make use of the "toTextStreamResponse()" that directly works along the "useObject" React hook. Docs: Here's a working repo in Next.js: Thanks so much in advance ❤️ |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
I think just returning it as normal to use app.get("/ai", () => {
const result = await streamObject({
schema: vacationSchema,
output: "array",
model: openai("gpt-4o-mini"),
prompt: "Generate 3 vacation destinations",
});
return result.toTextStreamResponse();
}) |
Beta Was this translation helpful? Give feedback.
-
You can stream the object like below: export const GET = createRoute(async (c) => {
if (!openai) {
openai = createOpenAI({
baseURL: c.env.OPENAI_PROXY_BASE_URL,
apiKey: c.env.OPENAI_PROXY_API_KEY,
});
}
const result = streamObject({
model: openai("gpt-4o"),
prompt: "Generate a lasagna recipe",
schema: z.object({
recipe: z.object({
name: z.string(),
ingredients: z.array(z.string()),
steps: z.array(z.string()),
}),
}),
output: "object",
});
// Mark the response as a v1 data stream:
c.header("X-Vercel-AI-Data-Stream", "v1");
c.header("Content-Type", "application/json; charset=utf-8");
return stream(c, async (stream) => {
for await (const chunk of result.partialObjectStream) {
await stream.write(JSON.stringify(chunk));
}
});
}); |
Beta Was this translation helpful? Give feedback.
-
Pd: As a side note, I had to disabled compression for those routes returning a |
Beta Was this translation helpful? Give feedback.
I think just returning it as normal to use
c.body
seems to work.