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
14 changes: 7 additions & 7 deletions src/anthropic/lib/streaming/_beta_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,18 +469,18 @@ def accumulate_event(
if current_snapshot is None:
if event.type == "message_start":
return cast(
ParsedBetaMessage[ResponseFormatT], ParsedBetaMessage.construct(**cast(Any, event.message.to_dict()))
ParsedBetaMessage[ResponseFormatT],
construct_type(type_=ParsedBetaMessage, value=event.message.to_dict()),
)

raise RuntimeError(f'Unexpected event order, got {event.type} before "message_start"')

if event.type == "content_block_start":
# TODO: check index
current_snapshot.content.append(
cast(
Any, # Pydantic does not support generic unions at runtime
construct_type(type_=ParsedBetaContentBlock, value=event.content_block.to_dict()),
),
while len(current_snapshot.content) <= event.index:
current_snapshot.content.append(None) # type: ignore[arg-type]
current_snapshot.content[event.index] = cast(
Any, # Pydantic does not support generic unions at runtime
construct_type(type_=ParsedBetaContentBlock, value=event.content_block.to_dict()),
)
if event.content_block.type == "fallback":
# the final hop's fallback block names the model that served the response —
Expand Down
16 changes: 9 additions & 7 deletions src/anthropic/lib/streaming/_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,17 +449,19 @@ def accumulate_event(

if current_snapshot is None:
if event.type == "message_start":
return cast(ParsedMessage[ResponseFormatT], ParsedMessage.construct(**cast(Any, event.message.to_dict())))
return cast(
ParsedMessage[ResponseFormatT],
construct_type(type_=ParsedMessage, value=event.message.to_dict()),
)

raise RuntimeError(f'Unexpected event order, got {event.type} before "message_start"')

if event.type == "content_block_start":
# TODO: check index
current_snapshot.content.append(
cast(
Any, # Pydantic does not support generic unions at runtime
construct_type(type_=ParsedContentBlock, value=event.content_block.model_dump()),
),
while len(current_snapshot.content) <= event.index:
current_snapshot.content.append(None) # type: ignore[arg-type]
current_snapshot.content[event.index] = cast(
Any, # Pydantic does not support generic unions at runtime
construct_type(type_=ParsedContentBlock, value=event.content_block.model_dump()),
)
elif event.type == "content_block_delta":
content = current_snapshot.content[event.index]
Expand Down