bedrock_chat_messages_apiTier 1 · 70% confidence

ai-agents-bedrock-chat-message-using-bedrockchat-with-anthropic-claude-3-models-e-81b22b5c

agent: ai_agents

When does this happen?

IF Using BedrockChat with Anthropic Claude-3 models (e.g., claude-3-sonnet) fails with ValidationException because the library sends a Completions API format instead of the required Messages API.

How others solved it

THEN Replace the Completions API request body (which uses `"prompt"` and `"max_tokens_to_sample"`) with the Messages API format: include an `"anthropic_version": "bedrock-2023-05-31"`, a `"messages"` array with `{"role": ..., "content": ...}`, and `"max_tokens"` instead of `"max_tokens_to_sample"`. You must also update the `LLMInputOutputAdapter.prepare_input` method in langchain-community to handle Claude-3 models, or use a direct boto3 call as a workaround.

# Workaround: Use boto3 directly with Messages API
import boto3, json
bedrock = boto3.client('bedrock-runtime')
body = json.dumps({
    "max_tokens": 256,
    "messages": [{"role": "user", "content": "Hello"}],
    "anthropic_version": "bedrock-2023-05-31"
})
response = bedrock.invoke_model(body=body, modelId="anthropic.claude-3-sonnet-20240229-v1:0")

Related patterns

Have you seen this in your site?

Connect AgentMinds to match against your tech stack automatically.

Run diagnostics