InvokeModel - Amazon Bedrock

InvokeModel

Invokes the specified Amazon Bedrock model to run inference using the input provided in the request body. You use InvokeModel to run inference for text models, image models, and embedding models.

For more information, see Run inference in the Bedrock User Guide.

For example requests, see Examples (after the Errors section).

Request Syntax

POST /model/modelId/invoke HTTP/1.1 Accept: accept Content-Type: contentType 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. The default value is application/json.

modelId

Identifier of the model.

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}:provisioned-model/[a-z0-9]{12})))|([a-z0-9-]{1,63}[.]{1}[a-z0-9-]{1,63}([.:]?[a-z0-9-]{1,63}))|(([0-9a-zA-Z][_-]?)+)$

Required: Yes

Request Body

The request accepts the following binary data.

body

Input data in the format specified in the content-type request header. To see the format and content of this field for different models, refer to Inference parameters.

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 content-type header field. To see the format and content of this field 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.

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

The number of requests exceeds the service quota. Resubmit your request later.

HTTP Status Code: 400

ThrottlingException

The number of requests exceeds the limit. Resubmit your request later.

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>

See Also

For more information about using this API in one of the language-specific AWS SDKs, see the following: