How to use streamObject in a NextJS API route? #1962
-
I was following the documentation example here: https://sdk.vercel.ai/examples/next-app/basics/streaming-object-generation and set up similar code within a NextJS API Route like so,
But how should I consume this stream from the client? Trying to use I don't see an equivalent documentation page under NextJS pages router, so is streaming objects from an API route just not supported yet? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 2 replies
-
There is no hook / data transport for |
Beta Was this translation helpful? Give feedback.
-
We have a |
Beta Was this translation helpful? Give feedback.
-
@DavidMelnychuk Hey, please can you send the full code, I am trying this but stuck with the errors. |
Beta Was this translation helpful? Give feedback.
-
Hi there, I wanted to share an alternative approach using the latest Vercel AI SDK v4.0 release. You can now use the experimental useObject hook from ai/react, which simplifies state management and API interaction. Here’s an example implementation based on the official docs: ` import { experimental_useObject as useObject } from 'ai/react'; export default function Page() { return ( <button onClick={() => submit('example input')}>Generate {object?.content && {object.content} }); } ` This method provides a structured way to handle API responses with a Zod schema, ensuring type safety. It could be a good alternative depending on the use case. |
Beta Was this translation helpful? Give feedback.
We have a
useObject
hook now: https://sdk.vercel.ai/docs/reference/ai-sdk-ui/use-object