Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/anthropic/lib/bedrock/_stream_decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ def _parse_message_from_event(self, event: EventStreamMessage) -> ServerSentEven
return _chunk_bytes_to_sse(chunk.get("bytes"))


def _chunk_bytes_to_sse(raw: bytes) -> ServerSentEvent | None:
def _chunk_bytes_to_sse(raw: bytes | None) -> ServerSentEvent | None:
if not isinstance(raw, bytes):
return None
decoded = raw.decode()
data: Any
try:
Expand Down
4 changes: 4 additions & 0 deletions tests/lib/test_bedrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,10 @@ def test_chunk_bytes_to_sse_drops_chunk_without_type_or_completion() -> None:
assert _chunk_bytes_to_sse(raw) is None


def test_chunk_bytes_to_sse_handles_none_or_non_bytes() -> None:
assert _chunk_bytes_to_sse(None) is None


def _eventstream_chunk_frame(payload: t.Mapping[str, object]) -> bytes:
"""Encode `payload` as one `chunk` event in AWS eventstream binary framing."""
body = json.dumps({"bytes": base64.b64encode(json.dumps(payload).encode()).decode()}).encode()
Expand Down