Skip to content

Commit 96b8fec

Browse files
authored
fix(Anthropic): Better handling of whitespace-only content in ChatAnthropic() (#86)
* fix(Anthropic): Better handling of whitespace-only content in ChatAnthropic() * Update changelog * Update types
1 parent 72ec555 commit 96b8fec

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2525
* `.extract_data()` is now supported.
2626
* `async` methods are now supported. (#81)
2727
* Fixed an issue with more than one session being active at once. (#83)
28+
* `ChatAnthropic()` no longer choke after receiving an output that consists only of whitespace. (#86)
2829

2930
### Changes
3031

chatlas/_anthropic.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,10 @@ def _as_message_params(self, turns: list[Turn]) -> list["MessageParam"]:
451451
@staticmethod
452452
def _as_content_block(content: Content) -> "ContentBlockParam":
453453
if isinstance(content, ContentText):
454-
return {"text": content.text, "type": "text"}
454+
text = content.text
455+
if text == "" or text.isspace():
456+
text = "[empty string]"
457+
return {"type": "text", "text": text}
455458
elif isinstance(content, ContentJson):
456459
return {"text": "<structured data/>", "type": "text"}
457460
elif isinstance(content, ContentPDF):

chatlas/types/openai/_submit.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ class SubmitInputArgs(TypedDict, total=False):
4444
"gpt-4.1-2025-04-14",
4545
"gpt-4.1-mini-2025-04-14",
4646
"gpt-4.1-nano-2025-04-14",
47+
"o4-mini",
48+
"o4-mini-2025-04-16",
49+
"o3",
50+
"o3-2025-04-16",
4751
"o3-mini",
4852
"o3-mini-2025-01-31",
4953
"o1",
@@ -125,7 +129,7 @@ class SubmitInputArgs(TypedDict, total=False):
125129
openai.NotGiven,
126130
]
127131
seed: Union[int, None, openai.NotGiven]
128-
service_tier: Union[Literal["auto", "default"], None, openai.NotGiven]
132+
service_tier: Union[Literal["auto", "default", "flex"], None, openai.NotGiven]
129133
stop: Union[str, None, list[str], openai.NotGiven]
130134
store: Union[bool, None, openai.NotGiven]
131135
stream: Union[Literal[False], None, Literal[True], openai.NotGiven]

tests/test_provider_anthropic.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,10 @@ def test_anthropic_images():
8989
def test_anthropic_pdfs():
9090
chat_fun = ChatAnthropic
9191
assert_pdf_local(chat_fun)
92+
93+
94+
def test_anthropic_empty_response():
95+
chat = ChatAnthropic()
96+
chat.chat("Respond with only two blank lines")
97+
resp = chat.chat("What's 1+1? Just give me the number")
98+
assert "2" == str(resp).strip()

0 commit comments

Comments
 (0)