InvokeModel
Invokes the specified Amazon Bedrock model to run inference using the prompt and inference parameters provided in the request body. You use model inference to generate text, images, and embeddings.
For example code, see Invoke model code examples.
This operation requires permission for the bedrock:InvokeModel
action.
Request Syntax
POST /model/modelId
/invoke HTTP/1.1
Accept: accept
Content-Type: contentType
X-Amzn-Bedrock-GuardrailIdentifier: guardrailIdentifier
X-Amzn-Bedrock-GuardrailVersion: guardrailVersion
X-Amzn-Bedrock-Trace: trace
body
URI Request Parameters
The request uses the following URI parameters.
- accept
-
The desired MIME type of the inference body in the response. The default value is
application/json
. - contentType
-
The MIME type of the input data in the request. You must specify
application/json
. - guardrailIdentifier
-
The unique identifier of the guardrail that you want to use. If you don't provide a value, no guardrail is applied to the invocation.
An error will be thrown in the following situations.
-
You don't provide a guardrail identifier but you specify the
amazon-bedrock-guardrailConfig
field in the request body. -
You enable the guardrail but the
contentType
isn'tapplication/json
. -
You provide a guardrail identifier, but
guardrailVersion
isn't specified.
Length Constraints: Minimum length of 0. Maximum length of 2048.
Pattern:
^(([a-z0-9]+)|(arn:aws(-[^:]+)?:bedrock:[a-z0-9-]{1,20}:[0-9]{12}:guardrail/[a-z0-9]+))$
-
- guardrailVersion
-
The version number for the guardrail. The value can also be
DRAFT
.Pattern:
^(([1-9][0-9]{0,7})|(DRAFT))$
- modelId
-
The unique identifier of the model to invoke to run inference.
The
modelId
to provide depends on the type of model or throughput that you use:-
If you use a base model, specify the model ID or its ARN. For a list of model IDs for base models, see Amazon Bedrock base model IDs (on-demand throughput) in the Amazon Bedrock User Guide.
-
If you use an inference profile, specify the inference profile ID or its ARN. For a list of inference profile IDs, see Supported Regions and models for cross-region inference in the Amazon Bedrock User Guide.
-
If you use a provisioned model, specify the ARN of the Provisioned Throughput. For more information, see Run inference using a Provisioned Throughput in the Amazon Bedrock User Guide.
-
If you use a custom model, first purchase Provisioned Throughput for it. Then specify the ARN of the resulting provisioned model. For more information, see Use a custom model in Amazon Bedrock in the Amazon Bedrock User Guide.
-
If you use an imported model, specify the ARN of the imported model. You can get the model ARN from a successful call to CreateModelImportJob or from the Imported models page in the Amazon Bedrock console.
Length Constraints: Minimum length of 1. Maximum length of 2048.
Pattern:
^(arn:aws(-[^:]+)?:bedrock:[a-z0-9-]{1,20}:(([0-9]{12}:custom-model/[a-z0-9-]{1,63}[.]{1}[a-z0-9-]{1,63}/[a-z0-9]{12})|(:foundation-model/[a-z0-9-]{1,63}[.]{1}[a-z0-9-]{1,63}([.:]?[a-z0-9-]{1,63}))|([0-9]{12}:imported-model/[a-z0-9]{12})|([0-9]{12}:provisioned-model/[a-z0-9]{12})|([0-9]{12}:inference-profile/[a-zA-Z0-9-:.]+)))|([a-z0-9-]{1,63}[.]{1}[a-z0-9-]{1,63}([.:]?[a-z0-9-]{1,63}))|(([0-9a-zA-Z][_-]?)+)|([a-zA-Z0-9-:.]+)$
Required: Yes
-
- trace
-
Specifies whether to enable or disable the Bedrock trace. If enabled, you can see the full Bedrock trace.
Valid Values:
ENABLED | DISABLED
Request Body
The request accepts the following binary data.
- body
-
The prompt and inference parameters in the format specified in the
contentType
in the header. You must provide the body in JSON format. To see the format and content of the request and response bodies for different models, refer to Inference parameters. For more information, see Run inference in the Bedrock User Guide.Length Constraints: Minimum length of 0. Maximum length of 25000000.
Required: Yes
Response Syntax
HTTP/1.1 200
Content-Type: contentType
body
Response Elements
If the action is successful, the service sends back an HTTP 200 response.
The response returns the following HTTP headers.
- contentType
-
The MIME type of the inference result.
The response returns the following as the HTTP body.
- body
-
Inference response from the model in the format specified in the
contentType
header. To see the format and content of the request and response bodies for different models, refer to Inference parameters.Length Constraints: Minimum length of 0. Maximum length of 25000000.
Errors
For information about the errors that are common to all actions, see Common Errors.
- AccessDeniedException
-
The request is denied because of missing access permissions.
HTTP Status Code: 403
- InternalServerException
-
An internal server error occurred. Retry your request.
HTTP Status Code: 500
- ModelErrorException
-
The request failed due to an error while processing the model.
HTTP Status Code: 424
- ModelNotReadyException
-
The model specified in the request is not ready to serve inference requests. The AWS SDK will automatically retry the operation up to 5 times. For information about configuring automatic retries, see Retry behavior in the AWS SDKs and Tools reference guide.
HTTP Status Code: 429
- ModelTimeoutException
-
The request took too long to process. Processing time exceeded the model timeout length.
HTTP Status Code: 408
- ResourceNotFoundException
-
The specified resource ARN was not found. Check the ARN and try your request again.
HTTP Status Code: 404
- ServiceQuotaExceededException
-
Your request exceeds the service quota for your account. You can view your quotas at Viewing service quotas. You can resubmit your request later.
HTTP Status Code: 400
- ServiceUnavailableException
-
The service isn't currently available. Try again later.
HTTP Status Code: 503
- ThrottlingException
-
Your request was throttled because of service-wide limitations. Resubmit your request later or in a different region. You can also purchase Provisioned Throughput to increase the rate or number of tokens you can process.
HTTP Status Code: 429
- ValidationException
-
Input validation failed. Check your request parameters and retry the request.
HTTP Status Code: 400
Examples
Run inference on a text model
Send an invoke request to run inference on a Titan Text G1 - Express model. We set the
accept
parameter to accept any content type in the response.
POST https://bedrock-runtime.us-east-1.amazonaws.com/model/amazon.titan-text-express-v1/invoke -H accept: */* -H content-type: application/json Payload {"inputText": "Hello world"}
Example response
Response for the above request.
-H content-type: application/json Payload <the model response>
Run inference on an image model
In the following example, the request sets the accept
parameter to image/png
.
POST https://bedrock-runtime.us-east-1.amazonaws.com/model/stability.stable-diffusion-xl-v1/invoke -H accept: image/png -H content-type: application/json Payload {"inputText": "Picture of a bird"}
Example response
Response for the above example.
-H content-type: image/png Payload <image bytes>
Use a guardrail
This example shows how to use a guardrail with InvokeModel
.
POST /model/modelId/invoke HTTP/1.1 Accept: accept Content-Type: contentType X-Amzn-Bedrock-GuardrailIdentifier: guardrailIdentifier X-Amzn-Bedrock-GuardrailVersion: guardrailVersion X-Amzn-Bedrock-GuardrailTrace: guardrailTrace X-Amzn-Bedrock-Trace: trace body // body { "amazon-bedrock-guardrailConfig": { "tagSuffix": "string" } }
Example response
This is an example response from InvokeModel
when using a
guardrail.
HTTP/1.1 200 Content-Type: contentType body // body { "amazon-bedrock-guardrailAction": "INTERVENED | NONE" "amazon-bedrock-trace": { "guardrails": { // Detailed guardrail trace } } }
See Also
For more information about using this API in one of the language-specific AWS SDKs, see the following: