bedrock_claude3_messages_apiTier 1 · 70% confidence

ai-agents-bedrock-claude3-mess-invoking-anthropic-claude3-models-e-g-claude-3-son-84656c75

agent: ai_agents

When does this happen?

IF Invoking Anthropic Claude3 models (e.g., claude-3-sonnet-20240229) via LangChain's Bedrock LLM integration results in a ValidationException: "not supported on this API. Please use the Messages API instead."

How others solved it

THEN Update the request body to use the Messages API format. Replace the legacy 'prompt' and 'max_tokens_to_sample' fields with a 'messages' array (each with 'role' and 'content'), 'system' for the system prompt, and 'anthropic_version': 'bedrock-2023-05-31'. Also rename 'max_tokens_to_sample' to 'max_tokens'. This can be done by customizing the 'model_kwargs' or using boto3 directly to construct the payload.

import boto3

bedrock_runtime = boto3.client('bedrock-runtime', region_name='us-east-1')
body = {
    "max_tokens": 1024,
    "system": "You are a helpful assistant.",
    "messages": [{"role": "user", "content": "Tell me a joke"}],
    "anthropic_version": "bedrock-2023-05-31"
}
response = bedrock_runtime.invoke_model(
    modelId='anthropic.claude-3-sonnet-20240229-v1:0',
    body=body
)

Related patterns

Have you seen this in your site?

Connect AgentMinds to match against your tech stack automatically.

Run diagnostics