Module 3 Lesson 1: The InvokeModel API
Going Raw. How to use InvokeModel to send model-specific JSON payloads to Bedrock.
InvokeModel: The Foundation
InvokeModel is the original, "Low-level" API for Bedrock. It requires you to know exactly which JSON structure a specific model (like Llama or Titan) expects. While it's more work, it gives you 100% control over the specialized features of each model.
1. Setting up Boto3
You need the boto3 library.
pip install boto3
2. The Python Implementation
Every model has a different body structure. Below is an example for Amazon Titan.
import boto3
import json
# Use 'bedrock-runtime' to call models
client = boto3.client("bedrock-runtime", region_name="us-east-1")
model_id = "amazon.titan-text-express-v1"
# Model-specific payload
body = {
"inputText": "Explain quantum physics in 2 sentences.",
"textGenerationConfig": {
"maxTokenCount": 512,
"temperature": 0.5
}
}
response = client.invoke_model(
modelId=model_id,
contentType="application/json",
accept="application/json",
body=json.dumps(body)
)
# Parse the response body stream
response_body = json.loads(response["body"].read())
print(response_body["results"][0]["outputText"])
3. Why InvokeModel is becoming "Legacy"
- If you want to switch from Titan to Claude, you have to rewrite the
bodyand theresponse_bodyparsing logic because they use different keys (e.g.,inputTextvsmessages).
4. Visualizing the Payload Cycle
graph LR
Input[Text Prompt] --> B[Wrap in Model JSON: {inputText: ...}]
B --> Call[invoke_model API]
Call --> Res[Raw JSON: {results: [...]}]
Res --> P[Parse specific keys]
P --> Final[Answer string]
💡 Guidance for Learners
Use InvokeModel only if you need a specialized parameter that the standard API doesn't support (like specific image-generation settings for Stable Diffusion). For 99% of text tasks, use the Converse API (Lesson 2).
Summary
- InvokeModel is the low-level API for Bedrock.
- The Body structure changes for every model provider.
- You must read and parse the binary response stream manually.
- It provides maximum control but minimum portability.