Skip to content

Commit f9ab57a

Browse files
committed
chore(stream): improve get_final_text() error message
1 parent b0138b1 commit f9ab57a

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/anthropic/lib/streaming/_beta_messages.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ def get_final_text(self) -> str:
101101
text_blocks.append(block.text)
102102

103103
if not text_blocks:
104-
raise RuntimeError("Expected to have received at least 1 text block")
104+
raise RuntimeError(
105+
f".get_final_text() can only be called when the API returns a `text` content block.\nThe API returned {','.join([b.type for b in message.content])} content block type(s) that you'll have to handle separately"
106+
)
105107

106108
return "".join(text_blocks)
107109

@@ -239,7 +241,10 @@ async def get_final_text(self) -> str:
239241
text_blocks.append(block.text)
240242

241243
if not text_blocks:
242-
raise RuntimeError("Expected to have received at least 1 text block")
244+
raise RuntimeError(
245+
f".get_final_text() can only be called when the API returns a `text` content block.\nThe API returned {','.join([b.type for b in message.content])} content block type(s) that you'll have to handle separately"
246+
)
247+
243248

244249
return "".join(text_blocks)
245250

src/anthropic/lib/streaming/_messages.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ def get_final_text(self) -> str:
100100
text_blocks.append(block.text)
101101

102102
if not text_blocks:
103-
raise RuntimeError("Expected to have received at least 1 text block")
103+
raise RuntimeError(
104+
f".get_final_text() can only be called when the API returns a `text` content block.\nThe API returned {','.join([b.type for b in message.content])} content block type(s) that you'll have to handle separately"
105+
)
104106

105107
return "".join(text_blocks)
106108

@@ -237,7 +239,9 @@ async def get_final_text(self) -> str:
237239
text_blocks.append(block.text)
238240

239241
if not text_blocks:
240-
raise RuntimeError("Expected to have received at least 1 text block")
242+
raise RuntimeError(
243+
f".get_final_text() can only be called when the API returns a `text` content block.\nThe API returned {','.join([b.type for b in message.content])} content block type(s) that you'll have to handle separately"
244+
)
241245

242246
return "".join(text_blocks)
243247

0 commit comments

Comments
 (0)