merge_streaming_text drops characters for pure-delta streaming producers

未关闭
#9 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

@Xuxchloris 已经在做这个了。

开始于 2026年8月14日。

  • #16 来自 @Xuxchloris —— 未关闭

评估

难度
4/5
预计耗时
3-5 天
新手友好度
58/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
冷清
技术栈
python
领域
backend

调研方向

从 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; only 1.0.0 / 1.1.0 / 1.2.0 exist, 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:

  1. if prev.startswith(chunk): return prev — a single-character delta such as "4"
    easily matches the tail state and gets discarded.
  2. 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,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

larksuite/channel-sdk-python 的其他 Issue

查看 larksuite/channel-sdk-python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。