Streamdown is a program developed by Vercel. React component, which can be used as a well-known library react-markdown
This tool is a direct replacement for the Markdown rendering of AI model-generated streaming text. The core design goal of this tool is to address Markdown rendering issues encountered when processing streaming text generated by AI models. In applications such as AI chat, text is generated verbatim or chunk by chunk, which often results in Markdown syntax that is incomplete in transit (e.g., a bolded markup **
Streamdown gracefully handles these incomplete or unclosed chunks of Markdown syntax and renders them correctly on-the-fly. This greatly improves the user experience, as users can see the formatted text in real time instead of waiting for the entire response to be generated. It integrates support for GitHub style Markdown (GFM), math formulas, code highlighting, and diagrams, with a focus on performance and security.
Function List
- Optimized for streaming: The core advantage is the ability to smoothly process and render a continuously incoming, incomplete stream of Markdown text.
- compatibility
react-markdown
: Available asreact-markdown
is a direct replacement for the - Unclosed Grammar Explanation:: Ability to correctly parse and style unclosed bold, italic, code, links and headings syntax.
- Support GFM: Built-in support for GitHub-style Markdown, including tables, task lists, and strikethrough.
- Rendering of math formulas: Support for rendering LaTeX mathematical equations through KaTeX.
- Mermaid Chart: Can render the Mermaid syntax in a code block to a chart and provide a render toggle button.
- Code syntax highlighting: Use the Shiki library to provide aesthetically pleasing syntax highlighting for code blocks.
- safety first: Based on
harden-react-markdown
constructed to ensure the security of the rendered content. - performance optimization: Uses Memoized rendering to improve update efficiency and reduce unnecessary re-rendering.
Using Help
Streamdown is an NPM package primarily used in React projects. It has a very simple installation and usage process designed to allow developers to integrate quickly.
1. Installation
First, you need to install in your project the streamdown
. It is possible to use npm
,yarn
maybe pnpm
etc. package managers for installation.
utilization npm
Installation:
npm i streamdown
utilization pnpm
Installation:
pnpm add streamdown
2. Configuration styles
In order for the rendered HTML elements to have the correct styling (e.g., the background color of the code block, the borders of the reference block, etc.), Streamdown needs you to bring in its CSS file. You'll need to add the following code to your project's global CSS file, usually the globals.css
or similar document.
@import "../node_modules/streamdown/dist/index.css";
Please ensure that @import
in your project structure to point to the correct path in the node_modules
Folder.
3. Basic use
In your React component, import the Streamdown
component and pass the Markdown string to be rendered as its child element.
This is a most basic example:
import { Streamdown } from 'streamdown';
export default function MyPage() {
const markdownContent = "# 你好,世界!\n\n这是一个 **流式传输** 的 *Markdown* 文本。";
return (
<article>
<Streamdown>{markdownContent}</Streamdown>
</article>
);
}
In the example above, themarkdownContent
The string is parsed by Streamdown and rendered into the corresponding HTML tag.
4. Used in conjunction with the Vercel AI SDK
Streamdown's greatest value comes in processing real-time generated text streams. The following example shows how it can be used with the Vercel AI SDK's useChat
Hooks are used in combination to render the AI's chat replies in real time.
'use client';
import { useChat } from '@ai-sdk/react';
import { useState } from 'react';
import { Streamdown } from 'streamdown';
export default function ChatComponent() {
// useChat 钩子管理聊天状态和消息
const { messages, sendMessage, status } = useChat();
const [input, setInput] = useState('');
// 处理表单提交
const handleSubmit = (e) => {
e.preventDefault();
if (input.trim()) {
sendMessage({ role: 'user', content: input });
setInput('');
}
};
return (
<div>
{/* 遍历并显示所有消息 */}
<div>
{messages.map(message => (
<div key={message.id}>
<strong>{message.role === 'user' ? 'You: ' : 'AI: '}</strong>
{/* AI 的回复内容会通过 Streamdown 实时渲染 */}
<Streamdown>{message.content}</Streamdown>
</div>
))}
</div>
{/* 用户输入表单 */}
<form onSubmit={handleSubmit}>
<input
value={input}
onChange={e => setInput(e.target.value)}
disabled={status !== 'idle'}
placeholder="说点什么..."
/>
<button type="submit" disabled={status !== 'idle'}>
发送
</button>
</form>
</div>
);
}
In this example, theuseChat
returned messages
The array is updated in real time as the AI generates responses.Streamdown
Component wrapped message.content
So whenever a new content fragment (token) arrives, it is immediately rendered into formatted HTML, and the user sees the text and formatting appear in tandem.
5. Component Properties (Props)
Streamdown Support react-markdown
and added some options specific to streaming:
children
(string): The Markdown string to be rendered.parseIncompleteMarkdown
(boolean, default value.true
): Whether to parse and render unclosed Markdown syntax. This is a core feature of Streamdown and it is recommended to keep it turned on.components
(object): Used to override the default HTML element renderer. For example, you can provide a custom<a>
tag component to handle links.remarkPlugins
(array).remark
An array of plugins to extend the Markdown syntax. The default integration of theremarkGfm
(for supporting forms, task lists, etc.) andremarkMath
TherehypePlugins
(array).rehype
An array of plug-ins for processing HTML, with the default integration of therehypeKatex
(for rendering math equations).shikiTheme
(string, default value.github-light
): Shiki topic name for code block syntax highlighting.
application scenario
- AI Chatbots
When interacting with large language models such as GPT, the model's responses are generated verbatim or word-by-word. Using Streamdown, these fragmented streams of text can be rendered in real-time as fully formatted paragraphs, such as code blocks, lists, and quotes, dramatically improving the immediacy and experience of user interactions. - Real-time collaboration Markdown editor
In applications that allow multiple users to edit the same Markdown document at the same time, Streamdown can be used to generate a real-time preview view for each user. Even if the Markdown syntax entered by the user is incomplete, the preview area shows the style as correctly as possible. - Live preview of online documents and notes applications
For any application that includes a Markdown input box, such as a blog publishing system, knowledge base, or personal note-taking software, Streamdown provides a real-time WYSIWYG preview window that lets users see the final rendering as they type.
QA
- Q: What is the difference between Streamdown and
react-markdown
What is the core difference between the
A: The core difference lies in the ability to handle "streams".react-markdown
Designed to render a complete Markdown document in a single pass. Streamdown is optimized to handle incomplete, continuously typed text streams, parsing and pre-rendering unclosed syntax (such as the**文本
), which isreact-markdown
Can't do it. - Q: Why is streaming rendering important for AI applications?
A: In AI applications, waiting for a model to generate a full length response can take seconds or longer. Streaming rendering allows users to start reading as soon as the first word is generated and see real-time formatting as the text is produced, which creates a more natural, immediate conversational experience that avoids the uncertainty of waiting. - Q: What Markdown extensions does it support?
A: It natively supports GitHub style Markdown (GFM), including tables, task lists, and strikethrough. It also integrates support for LaTeX math formulas (using KaTeX) and Mermaid charts via plugins. - Q: What are the system requirements or considerations for using Streamdown?
A: The project requires Node.js 18 or later and React 19.1.1 or later. Also, to ensure that the styles are displayed correctly, be sure to introduce Streamdown's stylesheet in your global CSS file.