Skip to content

fix: validate empty user messages to prevent Anthropic API 400 errors#3322

Merged
teknium1 merged 1 commit intomainfrom
hermes/hermes-5ef8201d
Mar 27, 2026
Merged

fix: validate empty user messages to prevent Anthropic API 400 errors#3322
teknium1 merged 1 commit intomainfrom
hermes/hermes-5ef8201d

Conversation

@teknium1
Copy link
Copy Markdown
Contributor

Summary

Salvaged from PR #3149 by @Bartok9 — cherry-picked onto current main with original authorship preserved.

Root cause

Empty user messages (Discord @mention-only, unrecognized attachments) pass through convert_messages_to_anthropic() without validation. Anthropic rejects with 400: user messages must have non-empty content. The session breaks permanently until /reset.

The adapter already validated empty assistant (→ "(empty)") and tool (→ "(no output)") messages, but not user messages.

Fix

  • anthropic_adapter.py: Validate empty/whitespace user messages for both string and list content formats → "(empty message)" placeholder
  • discord.py: Defense-in-depth — catch @mention-only messages at gateway layer before they enter session history
  • 4 regression tests: empty string, whitespace-only, empty list, empty text blocks

Validation

  • python -m pytest tests/test_anthropic_adapter.py -n0 -q → 81 passed

Closes #3149
Fixes #3143

Co-authored-by: Bartok9 bartok9@users.noreply.github.com

When user messages have empty content (e.g., Discord @mention-only
messages, unrecognized attachments), the Anthropic API rejects the
request with 'user messages must have non-empty content'.

Changes:
- anthropic_adapter.py: Add empty content validation for user messages
  (string and list formats), matching the existing pattern for assistant
  and tool messages. Empty content gets '(empty message)' placeholder.

- discord.py: Defense-in-depth check at gateway layer to catch empty
  messages before they enter session history.

- Add 4 regression tests covering empty string, whitespace-only,
  empty list, and empty text block scenarios.

Fixes #3143
@teknium1 teknium1 merged commit 3f95e74 into main Mar 27, 2026
2 checks passed
StreamOfRon pushed a commit to StreamOfRon/hermes-agent that referenced this pull request Mar 29, 2026
…NousResearch#3322)

When user messages have empty content (e.g., Discord @mention-only
messages, unrecognized attachments), the Anthropic API rejects the
request with 'user messages must have non-empty content'.

Changes:
- anthropic_adapter.py: Add empty content validation for user messages
  (string and list formats), matching the existing pattern for assistant
  and tool messages. Empty content gets '(empty message)' placeholder.

- discord.py: Defense-in-depth check at gateway layer to catch empty
  messages before they enter session history.

- Add 4 regression tests covering empty string, whitespace-only,
  empty list, and empty text block scenarios.

Fixes NousResearch#3143

Co-authored-by: Bartok9 <bartok9@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants