From 5f728d53b107781696ac91c66b2503b698e084a1 Mon Sep 17 00:00:00 2001 From: Junhyuk Lee Date: Fri, 22 May 2026 00:15:46 -0500 Subject: [PATCH] fix(streaming): align non-beta accumulator with beta implementation - Use to_dict() instead of model_dump() in content_block_start handler to match the beta version and the message_start handler in the same file. to_dict() uses API-compatible key names (use_api_names=True) and excludes unset fields by default, which is the correct behavior for construct_type(). - Use builtins.type() instead of bare type() in the error message to prevent potential shadowing, consistent with the beta implementation. - Update messages_stream.py shebang from rye to uv, consistent with all other examples and CONTRIBUTING.md guidelines. --- examples/messages_stream.py | 2 +- src/anthropic/lib/streaming/_messages.py | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/examples/messages_stream.py b/examples/messages_stream.py index 19260b075..d897a6e04 100755 --- a/examples/messages_stream.py +++ b/examples/messages_stream.py @@ -1,4 +1,4 @@ -#!/usr/bin/env -S rye run python +#!/usr/bin/env -S uv run python import asyncio diff --git a/src/anthropic/lib/streaming/_messages.py b/src/anthropic/lib/streaming/_messages.py index 5c0da9992..259dac41e 100644 --- a/src/anthropic/lib/streaming/_messages.py +++ b/src/anthropic/lib/streaming/_messages.py @@ -1,5 +1,6 @@ from __future__ import annotations +import builtins from types import TracebackType from typing import TYPE_CHECKING, Any, Type, Generic, Callable, cast from typing_extensions import Self, Iterator, Awaitable, AsyncIterator, assert_never @@ -445,7 +446,9 @@ def accumulate_event( ), ) if not isinstance(cast(Any, event), BaseModel): - raise TypeError(f"Unexpected event runtime type, after deserialising twice - {event} - {type(event)}") + raise TypeError( + f"Unexpected event runtime type, after deserialising twice - {event} - {builtins.type(event)}" + ) if current_snapshot is None: if event.type == "message_start": @@ -458,7 +461,7 @@ def accumulate_event( current_snapshot.content.append( cast( Any, # Pydantic does not support generic unions at runtime - construct_type(type_=ParsedContentBlock, value=event.content_block.model_dump()), + construct_type(type_=ParsedContentBlock, value=event.content_block.to_dict()), ), ) elif event.type == "content_block_delta":