Don't send empty or whitespace-only text blocks to Bedrock
Draft
LU
1mo ago+64+64+01234567890123456789-3-3-0123456789
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): skipTextParts 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 fromstrprompts andstr | TextContentitems 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_messagesplaceholders), 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
CachePointstill 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 kepttest_bedrock_map_messages_omits_empty_text_parts— empty and whitespace-onlyTextParts in an assistant turn are dropped
- All five cases (including parametrize) fail on
origin/mainwith 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 passeduv build --package pydantic-ai-slim --no-sources— succeededruff format --checkandruff checkon the two changed files — cleangit 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.