From f5b5cb1092095c6b0f2a446be3b7c91e22be67a1 Mon Sep 17 00:00:00 2001 From: Robert Craigie Date: Fri, 17 Jan 2025 15:16:51 +0000 Subject: [PATCH] chore(streaming): add runtime type check for better error messages (#826) --- src/anthropic/lib/streaming/_messages.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/anthropic/lib/streaming/_messages.py b/src/anthropic/lib/streaming/_messages.py index 590a99d5..67fe8f0b 100644 --- a/src/anthropic/lib/streaming/_messages.py +++ b/src/anthropic/lib/streaming/_messages.py @@ -5,6 +5,7 @@ from typing_extensions import Self, Iterator, Awaitable, AsyncIterator, assert_never import httpx +from pydantic import BaseModel from ._types import ( TextEvent, @@ -346,6 +347,9 @@ def accumulate_event( event: RawMessageStreamEvent, current_snapshot: Message | None, ) -> Message: + if not isinstance(event, BaseModel): # pyright: ignore[reportUnnecessaryIsInstance] + raise TypeError(f'Unexpected event runtime type - {event}') + if current_snapshot is None: if event.type == "message_start": return Message.construct(**cast(Any, event.message.to_dict()))