Skip to content

fix(Anthropic): Better handling of whitespace-only content in ChatAnthropic() #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 22, 2025
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* `.extract_data()` is now supported.
* `async` methods are now supported. (#81)
* Fixed an issue with more than one session being active at once. (#83)
* `ChatAnthropic()` no longer choke after receiving an output that consists only of whitespace. (#86)

### Changes

Expand Down
5 changes: 4 additions & 1 deletion chatlas/_anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,10 @@ def _as_message_params(self, turns: list[Turn]) -> list["MessageParam"]:
@staticmethod
def _as_content_block(content: Content) -> "ContentBlockParam":
if isinstance(content, ContentText):
return {"text": content.text, "type": "text"}
text = content.text
if text == "" or text.isspace():
text = "[empty string]"
return {"type": "text", "text": text}
elif isinstance(content, ContentJson):
return {"text": "<structured data/>", "type": "text"}
elif isinstance(content, ContentPDF):
Expand Down
6 changes: 5 additions & 1 deletion chatlas/types/openai/_submit.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class SubmitInputArgs(TypedDict, total=False):
"gpt-4.1-2025-04-14",
"gpt-4.1-mini-2025-04-14",
"gpt-4.1-nano-2025-04-14",
"o4-mini",
"o4-mini-2025-04-16",
"o3",
"o3-2025-04-16",
"o3-mini",
"o3-mini-2025-01-31",
"o1",
Expand Down Expand Up @@ -125,7 +129,7 @@ class SubmitInputArgs(TypedDict, total=False):
openai.NotGiven,
]
seed: Union[int, None, openai.NotGiven]
service_tier: Union[Literal["auto", "default"], None, openai.NotGiven]
service_tier: Union[Literal["auto", "default", "flex"], None, openai.NotGiven]
stop: Union[str, None, list[str], openai.NotGiven]
store: Union[bool, None, openai.NotGiven]
stream: Union[Literal[False], None, Literal[True], openai.NotGiven]
Expand Down
7 changes: 7 additions & 0 deletions tests/test_provider_anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,10 @@ def test_anthropic_images():
def test_anthropic_pdfs():
chat_fun = ChatAnthropic
assert_pdf_local(chat_fun)


def test_anthropic_empty_response():
chat = ChatAnthropic()
chat.chat("Respond with only two blank lines")
resp = chat.chat("What's 1+1? Just give me the number")
assert "2" == str(resp).strip()