merge_streaming_text drops characters for pure-delta streaming producers
评估
调研方向
从 lark_channel/channel/outbound/streaming/merge_text.py 开始检查 merge_streaming_text,然后跟踪 MarkdownStreamController.append 如何调用它。复现文档中记录的 40+4 和 210+0 情况,并验证纯增量流式传输会保留重复的边界字符,同时选定的 producer 模式按预期运行。
由索引模型根据 Issue 内容生成。
描述
merge_streaming_text drops characters for pure-delta streaming producers
Summary
merge_streaming_text (used by MarkdownStreamController.append) corrupts streamed
text when the producer emits pure incremental deltas. Whenever a new chunk's
leading character equals the trailing character of the already-accumulated content,
that character is silently dropped.
This surfaces in Feishu streaming cards as missing digits/characters, e.g. 404
rendered as 40, an account id 2100138470 rendered as 210138470, and
2026-07-23 rendered as 2026-07-2.
Version
lark-channel-sdk == 1.2.0(latest on PyPI; only1.0.0 / 1.1.0 / 1.2.0exist, so
there is no newer release to upgrade to).
Minimal Reproduction
Verified directly against the SDK's own function:
from lark_channel.channel.outbound.streaming.merge_text import merge_streaming_text as orig
orig("40", "4") # => "40" expected "404" (hits `prev.startswith(chunk)`, drops "4")
orig("210", "0") # => "210" expected "2100" (trailing "0" overlaps leading "0", dropped)
Actual output:
orig 40+4 => '40'
orig 210+0 => '210'
Expected vs. Actual
Input (prev, chunk) |
Expected | Actual | Dropped | Branch hit |
|---|---|---|---|---|
("40", "4") |
"404" |
"40" |
trailing 4 |
prev.startswith(chunk) |
("210", "0") |
"2100" |
"210" |
one 0 |
longest suffix/prefix overlap |
Root Cause
lark_channel/channel/outbound/streaming/merge_text.py:
def merge_streaming_text(prev: str, chunk: str) -> str:
if not chunk:
return prev
if not prev:
return chunk
if chunk.startswith(prev):
return chunk
if prev.startswith(chunk): # (1) drops a delta that is a prefix of prev
return prev
# Find the longest suffix of prev that is a prefix of chunk.
max_overlap = min(len(prev), len(chunk))
for size in range(max_overlap, 0, -1):
if prev[-size:] == chunk[:size]: # (2) drops the "overlap" region
return prev + chunk[size:]
return prev + chunk
The function tries to auto-detect accumulated vs. delta vs. mixed producers from the
text content alone. Two of its heuristics are invalid for pure delta producers:
if prev.startswith(chunk): return prev— a single-character delta such as"4"
easily matches the tail state and gets discarded.- The "longest suffix-of-prev that is a prefix-of-chunk" dedup deletes the overlap,
so any delta whose first char equals the accumulated tail loses that char.
As a result, repeated digits (00, 404) and boundary characters are the most
frequently corrupted.
Impact
Google ADK's LiteLlm streaming yields pure deltas (each SSE event carries only the
newly generated text, not the running total):
# google/adk/models/lite_llm.py
elif isinstance(chunk, TextChunk):
text += chunk.text
yield _message_to_generate_content_response(
ChatCompletionAssistantMessage(role="assistant", content=chunk.text), # delta only
is_partial=True,
model_version=part.model,
)
Any integration that forwards these deltas to MarkdownStreamController.append will
lose characters in the rendered card.
Suggested Fix
Do not infer the producer mode from content. Provide an explicit mode (e.g.
append_mode="delta" | "accumulated") so delta producers use plain concatenation:
def merge_streaming_text(prev: str, chunk: str) -> str:
return (prev or "") + (chunk or "") # correct for pure-delta producers
Workaround
Monkeypatch the merge function to a plain concatenation before streaming:
from lark_channel.channel.outbound.streaming import markdown_stream
def _concat(prev: str, chunk: str) -> str:
return (prev or "") + (chunk or "")
markdown_stream.merge_streaming_text = _concat
Verified after patching:
orig 40+4 => '40' # buggy
orig 210+0 => '210' # buggy
patched 40+4 => '404' # correct
patched 210+0 => '2100' # correct
- 主要语言
- Python
- 星标
- 18
- 派生
- 9
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
larksuite/channel-sdk-python 的其他 Issue
-
难度 3/5 1-2 天 新手友好度 58/100
-
webhook: signature verification silently no-ops without encrypt_key; no timestamp-freshness or replay checks可能已有人在做 @Xuxchloris 于 83 天前认领。 未关闭
难度 5/5 一周以上 新手友好度 35/100
-
Long threaded reply leaks out of a topic/thread chat: reply_to dropped on chunks after the first可能已有人在做 @Ydz0616 于 120 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 35/100
查看 larksuite/channel-sdk-python 的全部 Issue
相似的 Issue
-
follow-up needs-triage S3: minor
难度 2/5 1-3 小时 新手友好度 72/100
manaflow-ai/cmux#36856 · 1 条评论 ·
维护者通常 1 天内回复
-
documentation good first issue help wanted
难度 1/5 1-3 小时 新手友好度 90/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 73/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
-
Shell framework build fails at "copy the shader libraries": the iOS shader libraries are now one file b-<chain>.img可能已有人在做 @xiaofeng-han 今天认领。 未关闭build ios
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复