# Data streams

Match the decoder to your server’s stream format.

## Read a current UI-message stream

Use DefaultChatTransport in the [Vercel recipe](/libraries/ai-runtime-vercel) for a current UI-message stream. The SDK assembles messages and tool parts as they arrive and handles cancellation. Server bridge examples are available for [OpenAI Agents](/libraries/ai-runtime-openai), [Mastra](/libraries/ai-runtime-mastra), and [Pydantic AI](/libraries/ai-runtime-pydantic).

AI SDK v7 is the checked hook path. Older v6/v5 wire compatibility does not establish that every older hook API matches; use the matching upstream migration guide. Plain text uses [TextStreamChatTransport](/libraries/ai-runtime-local).

## Legacy v4 data stream

If your server still uses the v4 line-based protocol, install the maintained decoder and accumulator.

```bash
npm install assistant-stream@0.3.55
```

```tsx
import { DataStreamDecoder, AssistantMessageAccumulator, AssistantMessageStream } from "assistant-stream";
import { normalizeAssistantStreamMessage } from "@collectivecreation/ghost-ui/chat-normalizers";
export async function* readLegacy(response: Response, assistantMessageId: string) {
  if (!response.ok || !response.body) throw new Error(`Stream failed: ${response.status}`);
  const messages = response.body
    .pipeThrough(new DataStreamDecoder({ strict: true }))
    .pipeThrough(new AssistantMessageAccumulator({ throttle: false, strict: true }));
  let terminal = false;
  for await (const message of new AssistantMessageStream(messages)) {
    terminal = message.status.type !== "running" && !(message.status.type === "complete" && message.status.reason === "unknown");
    yield normalizeAssistantStreamMessage(assistantMessageId, message);
  }
  if (!terminal) throw new Error("Legacy stream ended before a terminal message.");
}

```

The application supplies a stable assistant message ID for this response, preserves user history and replaces that same assistant record as snapshots arrive. Initiate fetch with the owner's AbortSignal; cancellation, network errors and incomplete streams must reach the host state. Render the canonical message through [plain-data views](/libraries/ai#installation). Preliminary tool results remain data rather than completed execution.

## Limits and troubleshooting

Keep authentication, storage, replay, and tool execution in your app. The helper rejects a stream with no known terminal result. The accumulator can report “complete” with an unknown outcome at EOF, so check the actual finish status. Strict mode validates decoded messages and order. The decoder can still log and skip malformed lines. Check the bytes and headers when a stream fails, and set size limits that fit your app.

For arbitrary agent state carried alongside messages, use [Assistant Transport](/libraries/ai-runtime-state). See [assistant-stream source and protocol codecs](https://github.com/assistant-ui/assistant-ui/tree/main/packages/assistant-stream) for exhaustive formats; Ghost adds projection into canonical views, not a competing wire format.
