Module 3 Lesson 2: The Converse API
Unified AI. How to use AWS Bedrock's standard interface to write model-agnostic code.
The Converse API: The New Standard
The Converse API is a Unified Interface. It doesn't care if you are talking to Claude, Llama, or Mistral. You send the same "Message" structure, and AWS translates it for the provider automatically.
1. Why use Converse?
- Portability: Swap
modelIdfrom "Claude" to "Llama" and the code still works. - Native Chat: It handles multi-turn conversations (History) natively.
- Automatic Parsing: You don't have to
.read()the stream and guess the JSON keys.
2. The Python Implementation
import boto3
client = boto3.client("bedrock-runtime", region_name="us-east-1")
model_id = "anthropic.claude-3-haiku-20240307-v1:0"
messages = [
{
"role": "user",
"content": [{"text": "Who won the World Series in 2023?"}]
}
]
response = client.converse(
modelId=model_id,
messages=messages,
inferenceConfig={
"maxTokens": 512,
"temperature": 0.5
}
)
# Accessing the response is standard across models!
print(response["output"]["message"]["content"][0]["text"])
3. Visualizing the Unified Layer
graph TD
Code[Your Single Python Code] --> C[Converse API]
C -->|Translates to| C1[Claude Prompt]
C -->|Translates to| C2[Llama Prompt]
C -->|Translates to| C3[Mistral Prompt]
C1 --> Output[Standardized JSON Result]
C2 --> Output
C3 --> Output
4. Error Handling in Bedrock
When a call fails (Service quota exceeded, invalid prompt), Bedrock throws specific exceptions:
- ValidationException: Your prompt is too long or JSON is broken.
- ThrottlingException: You are sending too many requests per second.
- AccessDeniedException: You forgot your IAM permission.
💡 Guidance for Learners
Use the Converse API for everything text-related. It will save you thousands of lines of maintenance work as you test new models in the future.
Summary
- Converse is the high-level, cross-model API.
- It uses a standardized Messages list.
- It eliminates the need for model-specific payload parsing.
- It is the recommended way to build production applications on Bedrock.