Question presented to candidate:
"We are building a chat page in Next.js with the Vercel AI SDK 5. Walk me through the server route and the React side: how the tokens get from streamText into the component, what the messages look like, and how you handle tools, stopping, errors and saving the conversation."
What a strong answer should cover:
- Server: a route handler that receives
UIMessage[], converts them withconvertToModelMessages, callsstreamText({ model, system, messages, tools, stopWhen, abortSignal })and returnsresult.toUIMessageStreamResponse() - The wire format: the UI message stream protocol, SSE
data:frames of typed chunks (start,text-start,text-delta,tool-input-available,tool-output-available,finish, ...) ending indata: [DONE], with ax-vercel-ai-ui-message-stream: v1header - Client:
useChatfrom@ai-sdk/react(default transport POSTs to/api/chat) returningmessages,sendMessage,status(submitted,streaming,ready,error),stop,regenerate,error; renderingmessage.partsby type, including typed tool parts with states - Production concerns: persisting with
originalMessages+onFinish, stable chat and message ids, propagating abort, error masking (the default sends only "An error occurred."), throttling re-renders, and not trusting client-sent history blindly - Version awareness: v5 introduced this
UIMessage/ transport design; v6 madeconvertToModelMessagesasync; v7 deprecates the result helpers in favour ofcreateUIMessageStreamResponse+toUIMessageStream
Clarifying questions expected:
- Do conversations need to be saved and reloaded, and do answers need to survive a page refresh mid-stream?
- Do tools run on the server, or do some need the browser or a human confirmation?
- Which AI SDK major version and Next.js router is the app on?
Code / implementation expected: Yes โ a route handler and a client component, or the protocol and message-parts reducer they rely on.