Skip to content

Allow blank text field in bedrock-runtime message content #3525

@itogaston

Description

@itogaston

Describe the bug

With the new citations feature the content of an ai message can be blank.

Regression Issue

  • Select this option if this issue appears to be a regression.

Expected Behavior

The second call to the llm with the previous user and ai messages to work

Current Behavior

The second call breaks because is receiving an ai message with just a text string like this " "

Reproduction Steps

import boto3

client = boto3.client("bedrock-runtime", region_name="eu-west-3")

messages = [
    {
        "role": "user",
        "content": [
            {
                "document": {
                    "format": "txt",
                    "name": "My Document",
                    "source": {
                        "text": "The grass is green. The sky is blue."
                    },
                    "citations": {"enabled": True}
                }
            },
            {"text": "What color is the grass and sky?"}
        ]
    }
]

response = client.converse(
    modelId="eu.anthropic.claude-3-7-sonnet-20250219-v1:0",
    messages=messages,
    inferenceConfig={
        "maxTokens": 1000,
        "temperature": 0.1
    }
)

print(response['output']['message'])

# Append the assistant's response to messages
messages.append(response['output']['message'])

# Add another user message and continue the conversation
messages.append({
    "role": "user", 
    "content": [{"text": "What about the sun?"}]
})

response2 = client.converse(
    modelId="eu.anthropic.claude-3-7-sonnet-20250219-v1:0",
    messages=messages,
    inferenceConfig={
        "maxTokens": 1000,
        "temperature": 0.1
    }
)

print(response2)

Possible Solution

Remove blank validation

Additional Information/Context

previous example gives the error:

ValidationException: An error occurred (ValidationException) when calling the Converse operation: The text field in the ContentBlock object at messages.1.content.1 is blank. Add text to the text field, and try again.

because

print(response['output']['message'])
>>>>
{
    "role": "assistant",
    "content": [
        {
            "citationsContent": {
                "content": [{"text": "The grass is green."}],
                "citations": [
                    {
                        "title": "My Document",
                        "sourceContent": [{"text": "The grass is green. "}],
                        "location": {
                            "documentChar": {"documentIndex": 0, "start": 0, "end": 20}
                        },
                    }
                ],
            }
        },
        {"text": " "},
        {
            "citationsContent": {
                "content": [{"text": "The sky is blue."}],
                "citations": [
                    {
                        "title": "My Document",
                        "sourceContent": [{"text": "The sky is blue."}],
                        "location": {
                            "documentChar": {"documentIndex": 0, "start": 20, "end": 36}
                        },
                    }
                ],
            }
        },
    ],
}

the second message {"text": " "} is causing the problem

SDK version used

1.39.7

Environment details (OS name and version, etc.)

mac OS 15.4.1

Metadata

Metadata

Assignees

Labels

bedrockp3This is a minor priority issueservice-apiThis issue is caused by the service API, not the SDK implementation.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions