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

Conversation

cpsievert
Copy link
Collaborator

@cpsievert cpsievert commented Apr 22, 2025

Closes #10

Also, mirrors the approach from tidyverse/ellmer#393

@cpsievert cpsievert requested a review from Copilot April 22, 2025 16:45
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR improves the handling of whitespace-only responses in ChatAnthropic, closing #10 and mirroring a previous approach.

  • Added a test case to verify that whitespace-only responses are handled as expected.
  • Updated the ChatAnthropic implementation to replace empty or whitespace-only content with a placeholder.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
tests/test_provider_anthropic.py Added a new test case (test_anthropic_empty_response) to verify correct handling of whitespace.
chatlas/_anthropic.py Modified the content block logic to replace empty or whitespace-only responses with a placeholder.
Comments suppressed due to low confidence (2)

chatlas/_anthropic.py:455

  • [nitpick] Consider extracting the placeholder value "[empty string]" into a named constant to enhance maintainability and readability.
if text == "" or text.isspace():

tests/test_provider_anthropic.py:94

  • [nitpick] Consider adding additional test cases for different whitespace variants (e.g., spaces, tabs) to ensure robust handling of all edge cases.
def test_anthropic_empty_response():

@cpsievert cpsievert merged commit 96b8fec into main Apr 22, 2025
6 checks passed
@cpsievert cpsievert deleted the fix/whitespace branch May 13, 2025 20:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

claude sonnet latest: "all messages must have non-empty content except for the optional final assistant message"
1 participant