Skip to content

Conversation

@mike-luabase
Copy link
Contributor

  • Check for empty text content before adding to assistant messages
  • Only append messages with non-empty content to avoid API errors
  • Filter empty strings in user prompts before yielding text blocks

This fixes the invalid_request_error: "all messages must have non-empty content except for the optional final assistant message"

🤖 Generated with Claude Code

- Check for empty text content before adding to assistant messages
- Only append messages with non-empty content to avoid API errors
- Filter empty strings in user prompts before yielding text blocks

This fixes the invalid_request_error: "all messages must have non-empty content except for the optional final assistant message"

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@mike-luabase mike-luabase force-pushed the fix-anthropic-empty-messages branch from 54eea71 to 7cd8ec3 Compare June 6, 2025 13:20
mike-luabase and others added 2 commits June 12, 2025 07:38
Updated test_anthropic_empty_content_filtering to use snapshot() for better
visibility of what gets sent to the Anthropic API, as requested in PR review.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
Per PR review feedback, refactored the empty content filtering tests to:
- Use _map_message instead of testing _map_user_prompt directly
- Use snapshot testing for better visibility of the output
- Test at a higher level rather than testing private methods directly

This makes the tests more maintainable and provides clearer output.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@DouweM DouweM changed the title fix: prevent Anthropic API errors from empty message content Prevent Anthropic API errors from empty message content Jun 12, 2025
@DouweM DouweM merged commit 8aa964b into pydantic:main Jun 12, 2025
18 checks passed
@DouweM
Copy link
Collaborator

DouweM commented Jun 12, 2025

@mike-luabase Thank you, looks great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants