AI › AI Tools

Streaming release-note summaries in React with TanStack AI

Connect TanStack AI to a local Ollama model through Next.js and verify that its release-note summary streams into React.

Streaming release-note summaries in React with TanStack AI

I sent the same Jekyll release notes to a local model and watched the answer appear in React while the request was still open. The browser trace shows how that response made it from Ollama through a Next.js route to the page.

I ran this on Node.js 22.23.3 with Next.js 16.4.6, React 19.3.1, @tanstack/ai 1.10.1, @tanstack/ai-react 1.3.0, and @tanstack/ai-ollama 1.1.1. Ollama 0.40.18 ran SmolLM2 135M locally.

Give React an HTTP route to connect to

I kept the Next.js shell from my Vercel AI SDK streaming example. This time the route returned TanStack AI events, and React read them through fetchServerSentEvents('/api/chat'). That gave me a concrete place to wire up the hook. On Next.js App Router, the URL maps to app/api/chat/route.ts. The browser posts the conversation there and reads the stream from the response to that same request. TanStack AI quick start

On the server, the Ollama adapter talks to the model. chat() returns an iterable of events and toServerSentEventsResponse() encodes those AG-UI events as SSE frames. In the browser, the connection adapter reads the events and the hook updates messages. The route is the place to check which event format an existing backend returns before plugging in a new hook. Transport overview

If you need to…Start here
Show part of an answer as it is writtenConnect useChat to the SSE route.
Run a batch job that consumes a finished summaryCall chat() on the server with stream: false.
Reuse an existing streaming backendCheck its event format against the client transport contract.

Server-only quick start · Transport options

Send the Ollama response as server-sent events

I used the same public Jekyll 4.4.0 excerpt as input. It covers the Ruby 2.7 minimum, the ability to select the lines Liquid highlights in a code block, and three runtime dependencies, csv, base64, and json. I asked for three short bullets drawn from that excerpt. That gave me three details to look for independently of whether the stream finished successfully. Jekyll 4.4.0 release notes

I started a local Ollama listener with OLLAMA_HOST=127.0.0.1:4422 ollama serve and used the same host when running ollama pull smollm2:135m-instruct-q4_K_M. Next.js and Ollama ran on the same machine, so the route could reach the model over loopback. Ollama adapter · Model tag

The excerpt belongs in app/api/chat/route.ts. Import chat, chatParamsFromRequest, and toServerSentEventsResponse from @tanstack/ai, and createOllamaChat from @tanstack/ai-ollama. The catch block returns the response that the parser throws when the body is invalid. I sent malformed JSON and checked that Next.js returned HTTP 400. Handling malformed requests in Next.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const model = 'smollm2:135m-instruct-q4_K_M'

export async function POST(request: Request) {
  try {
    const { messages, threadId, runId } = await chatParamsFromRequest(request)
    const stream = chat({
      stream: true,
      adapter: createOllamaChat(model, 'http://127.0.0.1:4422'),
      messages,
      threadId,
      runId,
      modelOptions: { model, options: { temperature: 0, num_predict: 192 } },
    })
    return toServerSentEventsResponse(stream)
  } catch (error) {
    if (error instanceof Response) return error
    throw error
  }
}

Render text parts as they arrive

In the client component, I imported fetchServerSentEvents and useChat from @tanstack/ai-react. The button calls sendMessage(prompt) with the release-note excerpt and the instructions. The response area renders assistant text parts. The one-shot demo joins them into a single string. In a multi-turn chat I would render each message in its own block so the next answer gets a separate place on the page. React quick start

1
2
3
4
5
6
7
8
9
10
11
const liveConnection = fetchServerSentEvents('/api/chat')

// Inside the React client component
const { messages, sendMessage, isLoading, error } = useChat({
  connection: liveConnection,
  threadId: 'release-summary',
})
const text = messages.filter(({ role }) => role === 'assistant')
  .flatMap(({ parts }) => parts)
  .map((part) => part.type === 'text' ? part.content : '')
  .join('')

The fixed thread ID belongs to the demo. When a service stores chats for multiple users, the application needs a conversation ID and a server-side check that the current user can access it. The demo reads isLoading for the in-flight label and handles error independently of the text. That matters when a request produces some text and then fails. Chat persistence

Check the browser trace against the rendered answer

I clicked the button in Chrome and checked the response to POST /api/chat. It returned HTTP 200 with text/event-stream. This run had 45 TEXT_MESSAGE_CONTENT events. Concatenating their delta fields produced the same string as the final text in the page. I also saw several shorter versions render before the run ended with RUN_FINISHED.

Next.js and React demo showing the Jekyll source beside a live SmolLM2 response with an unfinished dependency sentence Figure 1. In this capture the dependency sentence had reached csv.

Completed response showing the Liquid change and the three runtime dependencies, with the Ruby requirement visible only in the source excerpt Figure 2. The completed response from the same request. The Ruby 2.7 requirement appears only in the source on the left.

The finished response included the Liquid change and all three dependency names. It skipped the Ruby requirement, even though the prompt asked for it. There are two checks here. The SSE trace verifies delivery, while comparing the answer with the excerpt verifies coverage. Both screenshots use real local inference. In the draft approval exercise, I used a fixed model response to test the browser’s approval and storage paths.

What I check when wiring up a stream

  • Check the response media type and watch for text before the request closes. Repeat through the deployed route if a proxy sits between the browser and the app.
  • Render text parts and track the run status separately so an interrupted answer doesn’t look complete.
  • Compare the finished answer with the original release notes when versions and dependency names matter.
This post is licensed under CC BY 4.0 by the author.