diff --git a/CHANGELOG.md b/CHANGELOG.md index 28e247ae..2f2bb3aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/chatlas/_anthropic.py b/chatlas/_anthropic.py index 1d09b7dd..3c21cfe9 100644 --- a/chatlas/_anthropic.py +++ b/chatlas/_anthropic.py @@ -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": "", "type": "text"} elif isinstance(content, ContentPDF): diff --git a/chatlas/types/openai/_submit.py b/chatlas/types/openai/_submit.py index 75efd761..a7137d48 100644 --- a/chatlas/types/openai/_submit.py +++ b/chatlas/types/openai/_submit.py @@ -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", @@ -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] diff --git a/tests/test_provider_anthropic.py b/tests/test_provider_anthropic.py index f6862ce6..e4ae1b64 100644 --- a/tests/test_provider_anthropic.py +++ b/tests/test_provider_anthropic.py @@ -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()