Open
Description
Initial Checks
- I confirm that I'm using the latest version of Pydantic AI
- I confirm that I searched for my issue in https://github.com/pydantic/pydantic-ai/issues before opening this issue
Description
While using pydantic Bedrock with Claude Model. I often reach the following issue:
botocore.errorfactory.ValidationException: An error occurred (ValidationException) when calling the ConverseStream operation: The text field in the ContentBlock object at messages.21.content.0 is blank. Add text to the text field, and try again.
It's not always messages 21, it can be another. But most of the time it happens after a number of tool calls, potentially unsuccessful.
Unfortunately, I'm not sure to understand how to best replicate this. The following snippet led me to face this error in 50% of cases, so it can be used for that. However, I'd need some help to create a more robust code snippet to replicate this error.
I believe we could solve this from ensuring content is not empty in https://github.com/pydantic/pydantic-ai/blob/240b0120bf542e8dbeeb52e7b0f4836f964ededa/pydantic_ai_slim/pydantic_ai/models/bedrock.py#L367C15-L367C28
Example Code
from pydantic_ai import Agent, ModelRetry
from random import randint
import asyncio
def get_random_number():
"""
Return a random number using the randint
"""
res = randint(0, 100)
if res > 50:
raise ModelRetry("Error: Random number is greater than 50")
return res
agent = Agent(
"bedrock:us.anthropic.claude-3-7-sonnet-20250219-v1:0",
system_prompt="You are a helpful assistant that generates random numbers.",
retries=3,
tools=[get_random_number],
)
async def main():
async with agent.iter(user_prompt="Generate 10 different random numbers and then create a summary of these numbers.") as result:
async for node in result:
if Agent.is_model_request_node(node):
async with node.stream(result.ctx) as request_stream:
async for event in request_stream:
print(event)
if __name__ == "__main__":
asyncio.run(main())
Python, Pydantic AI & LLM client version
pydantic-ai-slim==0.1.10
Python 3.10.13