Bug: _chunk_bytes_to_sse raises AttributeError when chunk.get('bytes') is None in Bedrock stream decoder

未关闭
#1,975 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

@chauvuusvn 已经在做这个了。

开始于 2026年10月5日。

  • #1976 来自 @chauvuusvn —— 未关闭
  • #1996 来自 @iosayin —— 已关闭,未合并

评估

难度
1/5
预计耗时
1-3 小时
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
停滞
技术栈
aws, python
领域
backend

调研方向

Start with anthropic/lib/bedrock/_stream_decoder.py, especially _chunk_bytes_to_sse and _parse_message_from_event; the issue provides a direct reproduction by passing None. Check the existing stream decoder tests and run the relevant tests. Done means empty or missing chunk bytes no longer crash and are dropped as described; a linked pull request is already open.

由索引模型根据 Issue 内容生成。

描述

Describe the bug

In anthropic.lib.bedrock._stream_decoder._chunk_bytes_to_sse(raw: bytes), raw.decode() is called unconditionally on line 72:

def _chunk_bytes_to_sse(raw: bytes) -> ServerSentEvent | None:
    decoded = raw.decode()

In _parse_message_from_event, _chunk_bytes_to_sse is invoked with chunk.get("bytes"):

chunk = parsed_response.get("chunk")
if not chunk:
    return None

return _chunk_bytes_to_sse(chunk.get("bytes"))

When AWS Bedrock sends an empty chunk frame, a control payload, or a chunk dictionary where the "bytes" key is missing or None (e.g. chunk = {"bytes": None} or chunk = {}), chunk.get("bytes") evaluates to None.

Passing None to _chunk_bytes_to_sse raises AttributeError: 'NoneType' object has no attribute 'decode', immediately crashing the active Bedrock stream.

To Reproduce
from anthropic.lib.bedrock._stream_decoder import _chunk_bytes_to_sse

_chunk_bytes_to_sse(None)
# Traceback (most recent call last):
#   File ".../anthropic/lib/bedrock/_stream_decoder.py", line 72, in _chunk_bytes_to_sse
#     decoded = raw.decode()
# AttributeError: 'NoneType' object has no attribute 'decode'
Expected Behavior

_chunk_bytes_to_sse should safely guard against None or non-bytes payloads and return None (dropping the empty frame) rather than crashing with an AttributeError.

主要语言
Python
星标
4.1k
派生
915
PR 合并指标
30 天内没有已合并 PR

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

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

anthropics/anthropic-sdk-python 的其他 Issue

查看 anthropics/anthropic-sdk-python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

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