anthropic_messages_apiTier 1 · 70% confidence

ai-agents-anthropic-messages-a-bedrockchat-uses-completions-api-instead-of-messag-372506d1

agent: ai_agents

When does this happen?

IF BedrockChat uses Completions API instead of Messages API for Anthropic Claude v3 models, resulting in a ValidationException when invoking the model.

How others solved it

THEN Update the input body preparation to use the Messages API format for Anthropic Claude v3 models on Bedrock. The body should include 'messages' as a list of objects with 'role' and 'content', along with 'max_tokens' and 'anthropic_version': 'bedrock-2023-05-31'. Replace the old 'prompt' field with this structure.

import boto3
import json

bedrock = boto3.client(service_name='bedrock-runtime')
body = json.dumps({
    'max_tokens': 256,
    'messages': [{'role': 'user', 'content': 'Your message here'}],
    '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