Don't send empty or whitespace-only text blocks to Bedrock

Draft
LU
1mo ago
+64-3

Closes #11122

BedrockConverseModel currently maps empty or whitespace-only user prompts and empty TextParts into {'text': ...} content blocks verbatim, which Bedrock rejects with a 400 ValidationException ("The text field in the ContentBlock object ... is blank" / "text content blocks must be non-empty" / "text content blocks must contain non-whitespace text"). The two mapping sites are the assistant-turn TextPart handling in _map_messages and _map_user_prompt.

Change

  • _map_messages (assistant turns): skip TextParts whose content is empty or whitespace-only instead of appending them as blank text blocks. An assistant turn left with no content is simply not emitted, as before.
  • _map_user_prompt (user turns): skip blank text from str prompts and str | TextContent items the same way; if that leaves the turn with no content at all, fall back to the same '.' placeholder already used for synthetic turns (see the existing _map_messages placeholders), so Bedrock still receives a valid user message.
  • Unchanged on purpose: a documents-only turn still gets its "See attached document(s)." text before the placeholder check, and a turn that contained only a CachePoint still emits no message.

Testing

  • New tests in tests/models/test_bedrock.py:
    • test_bedrock_empty_user_prompt_uses_placeholder — empty, whitespace-only and mixed-blank prompts map to the '.' placeholder, both as the opening prompt and mid-history (parametrized)
    • test_bedrock_map_user_prompt_omits_empty_text_items — blank items in a multi-item prompt are dropped while real text is kept
    • test_bedrock_map_messages_omits_empty_text_parts — empty and whitespace-only TextParts in an assistant turn are dropped
  • All five cases (including parametrize) fail on origin/main with the blank blocks present in the mapped messages, and pass with this change (5 failed → 5 passed).
  • pytest tests/models/test_bedrock.py -q — 299 passed
  • uv build --package pydantic-ai-slim --no-sources — succeeded
  • ruff format --check and ruff check on the two changed files — clean
  • git diff --check origin/main...HEAD — clean

Checklist

  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver.
  • PR title is fit for the release changelog.