Skip to content

Conversation

@Sameerlite
Copy link
Collaborator

@Sameerlite Sameerlite commented Oct 23, 2025

Title

Fix empty assistant message handling in AWS Bedrock Converse API to prevent 400 Bad Request errors

Relevant issues

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have Added testing in the tests/litellm/ directory, Adding at least 1 test is a hard requirement - see details
  • I have added a screenshot of my new test passing locally
  • My PR passes all unit tests on make test-unit
  • My PR's scope is as isolated as possible, it only solves 1 specific problem

Type

🐛 Bug Fix

Changes

Problem

AWS Bedrock Converse API returns a 400 Bad Request error when assistant messages contain empty or whitespace-only text content. The error message is:

"The text field in the ContentBlock object at messages.X.content.0 is blank. Add text to the text field, and try again."

This occurs when users pass messages like:

messages = [
    {"role": "user", "content": "Hello"},
    {"role": "assistant", "content": ""},  # Empty content causes 400 error
    {"role": "user", "content": "How are you?"}
]

Solution

Modified the _bedrock_converse_messages_pt function in litellm/litellm_core_utils/prompt_templates/factory.py to handle empty assistant content by replacing it with a non-empty placeholder text.

Key Changes:

  1. String content handling (line 3833): Replace empty or whitespace-only strings with "."
  2. List content handling (line 3807): Replace empty text elements in content lists with "."

Code Changes

# Before
text_content = element["text"] if element["text"].strip() else "."
assistant_content.append(BedrockContentBlock(text=text_content))

# After  
text_content = _assistant_content if _assistant_content.strip() else "."
assistant_content.append(BedrockContentBlock(text=text_content))

Screenshot

image

@vercel
Copy link

vercel bot commented Oct 23, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
litellm Ready Ready Preview Comment Oct 23, 2025 5:12pm

Copy link
Contributor

@ishaan-jaff ishaan-jaff left a comment

Choose a reason for hiding this comment

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

lgtm

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.

2 participants