8000
Skip to content

bug: Streaming markdown formatting breaks code fences and whitespace #421

@228998098

Description

@228998098

问题描述

When using streaming responses, markdown formatting can break in a few ways:

  • fenced code blocks may lose their expected structure
  • whitespace-only tokens are dropped, which removes meaningful spaces/newlines/indentation
  • some plain text may get rendered as part of a code block
    This is especially visible when asking the model to output multiple markdown code block examples.

复现步骤

A simple prompt that reproduces the issue:
‘‘‘最常见的ai回复的代码块有哪些,举例输出5个’’’

预期行为

  1. Python(最常见,AI 经常输出算法/脚本)
print("Hello, World!")
  1. JavaScript(前端/Node.js 常用)
console.log("Hello, World!");
  1. HTML(网页结构最常见)
<!DOCTYPE html>
<html>
<body>
  <h1>Hello, World!</h1>
</body>
</html>
  1. CSS(样式代码高频出现)
body {
  color: blue;
  font-family: Arial;
}
  1. SQL(数据库查询最常见)
SELECT * FROM users WHERE id = 1;

实际行为

  1. Python代码块```pythondef
    greet(name):
    return f"Hello, {name}!"
    print(greet("AI"))

2. JavaScript代码块```javascriptconst
 message = "Hello from JS!";
console.log(message);
  1. HTML代码块```html

AI回复示例

```
  1. CSS代码块```css.example
    { color: blue;
    font-size:16px;
    }

5. SQL查询代码块```sqlSELECT
 * FROM usersWHERE created_at > '2025-01-01';

部署方式

Docker Compose

版本/提交号

2bead4b

相关日志/报错

The main issue appears to be that whitespace-only tokens are discarded in streaming mode:

<PYTHON>
if not in_think and not filtered.strip():
    continue
This removes meaningful spaces/newlines/indentation and breaks markdown/code formatting.

There was also a related issue where tag filtering could remove surrounding text too aggressively.

请求样例(可选)

Fix direction
The following changes fixed the issue in my local test:

Preserve whitespace-only tokens in stream output
Strip only the tag itself instead of dropping the whole token
Repair fragmented fenced code block starts in streaming mode

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0