ConverseStream
Sends messages to the specified Amazon Bedrock model and returns
the response in a stream. ConverseStream
provides a consistent API
that works with all Amazon Bedrock models that support messages.
This allows you to write code once and use it with different models. Should a
model have unique inference parameters, you can also pass those unique parameters to the
model.
To find out if a model supports streaming, call GetFoundationModel
and check the responseStreamingSupported
field in the response.
Note
The AWS CLI doesn't support streaming operations in Amazon Bedrock, including ConverseStream
.
Amazon Bedrock doesn't store any text, images, or documents that you provide as content. The data is only used to generate the response.
For information about the Converse API, see Use the Converse API. To use a guardrail, see Use a guardrail with the Converse API. To use a tool with a model, see Tool use (Function calling).
For example code, see Conversation streaming example.
This operation requires permission for the bedrock:InvokeModelWithResponseStream
action.
Request Syntax
POST /model/modelId
/converse-stream HTTP/1.1
Content-type: application/json
{
"additionalModelRequestFields": JSON value
,
"additionalModelResponseFieldPaths": [ "string
" ],
"guardrailConfig": {
"guardrailIdentifier": "string
",
"guardrailVersion": "string
",
"streamProcessingMode": "string
",
"trace": "string
"
},
"inferenceConfig": {
"maxTokens": number
,
"stopSequences": [ "string
" ],
"temperature": number
,
"topP": number
},
"messages": [
{
"content": [
{ ... }
],
"role": "string
"
}
],
"system": [
{ ... }
],
"toolConfig": {
"toolChoice": { ... },
"tools": [
{ ... }
]
}
}
URI Request Parameters
The request uses the following URI parameters.
- modelId
-
The ID for the model.
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.
The Converse API doesn't support imported models.
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})|([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
-
Request Body
The request accepts the following data in JSON format.
- additionalModelRequestFields
-
Additional inference parameters that the model supports, beyond the base set of inference parameters that
ConverseStream
supports in theinferenceConfig
field.Type: JSON value
Required: No
- additionalModelResponseFieldPaths
-
Additional model parameters field paths to return in the response.
ConverseStream
returns the requested fields as a JSON Pointer object in theadditionalModelResponseFields
field. The following is example JSON foradditionalModelResponseFieldPaths
.[ "/stop_sequence" ]
For information about the JSON Pointer syntax, see the Internet Engineering Task Force (IETF)
documentation. ConverseStream
rejects an empty JSON Pointer or incorrectly structured JSON Pointer with a400
error code. if the JSON Pointer is valid, but the requested field is not in the model response, it is ignored byConverseStream
.Type: Array of strings
Array Members: Minimum number of 0 items. Maximum number of 10 items.
Length Constraints: Minimum length of 1. Maximum length of 256.
Required: No
- guardrailConfig
-
Configuration information for a guardrail that you want to use in the request.
Type: GuardrailStreamConfiguration object
Required: No
- inferenceConfig
-
Inference parameters to pass to the model.
ConverseStream
supports a base set of inference parameters. If you need to pass additional parameters that the model supports, use theadditionalModelRequestFields
request field.Type: InferenceConfiguration object
Required: No
- messages
-
The messages that you want to send to the model.
Type: Array of Message objects
Required: Yes
- system
-
A system prompt to send to the model.
Type: Array of SystemContentBlock objects
Required: No
- toolConfig
-
Configuration information for the tools that the model can use when generating a response.
For information about models that support streaming tool use, see Supported models and model features.
Type: ToolConfiguration object
Required: No
Response Syntax
HTTP/1.1 200
Content-type: application/json
{
"contentBlockDelta": {
"contentBlockIndex": number,
"delta": { ... }
},
"contentBlockStart": {
"contentBlockIndex": number,
"start": { ... }
},
"contentBlockStop": {
"contentBlockIndex": number
},
"internalServerException": {
},
"messageStart": {
"role": "string"
},
"messageStop": {
"additionalModelResponseFields": JSON value,
"stopReason": "string"
},
"metadata": {
"metrics": {
"latencyMs": number
},
"trace": {
"guardrail": {
"inputAssessment": {
"string" : {
"contentPolicy": {
"filters": [
{
"action": "string",
"confidence": "string",
"type": "string"
}
]
},
"contextualGroundingPolicy": {
"filters": [
{
"action": "string",
"score": number,
"threshold": number,
"type": "string"
}
]
},
"sensitiveInformationPolicy": {
"piiEntities": [
{
"action": "string",
"match": "string",
"type": "string"
}
],
"regexes": [
{
"action": "string",
"match": "string",
"name": "string",
"regex": "string"
}
]
},
"topicPolicy": {
"topics": [
{
"action": "string",
"name": "string",
"type": "string"
}
]
},
"wordPolicy": {
"customWords": [
{
"action": "string",
"match": "string"
}
],
"managedWordLists": [
{
"action": "string",
"match": "string",
"type": "string"
}
]
}
}
},
"modelOutput": [ "string" ],
"outputAssessments": {
"string" : [
{
"contentPolicy": {
"filters": [
{
"action": "string",
"confidence": "string",
"type": "string"
}
]
},
"contextualGroundingPolicy": {
"filters": [
{
"action": "string",
"score": number,
"threshold": number,
"type": "string"
}
]
},
"sensitiveInformationPolicy": {
"piiEntities": [
{
"action": "string",
"match": "string",
"type": "string"
}
],
"regexes": [
{
"action": "string",
"match": "string",
"name": "string",
"regex": "string"
}
]
},
"topicPolicy": {
"topics": [
{
"action": "string",
"name": "string",
"type": "string"
}
]
},
"wordPolicy": {
"customWords": [
{
"action": "string",
"match": "string"
}
],
"managedWordLists": [
{
"action": "string",
"match": "string",
"type": "string"
}
]
}
}
]
}
}
},
"usage": {
"inputTokens": number,
"outputTokens": number,
"totalTokens": number
}
},
"modelStreamErrorException": {
},
"serviceUnavailableException": {
},
"throttlingException": {
},
"validationException": {
}
}
Response Elements
If the action is successful, the service sends back an HTTP 200 response.
The following data is returned in JSON format by the service.
- contentBlockDelta
-
The messages output content block delta.
Type: ContentBlockDeltaEvent object
- contentBlockStart
-
Start information for a content block.
Type: ContentBlockStartEvent object
- contentBlockStop
-
Stop information for a content block.
Type: ContentBlockStopEvent object
- internalServerException
-
An internal server error occurred. Retry your request.
Type: Exception
HTTP Status Code: 500 - messageStart
-
Message start information.
Type: MessageStartEvent object
- messageStop
-
Message stop information.
Type: MessageStopEvent object
- metadata
-
Metadata for the converse output stream.
Type: ConverseStreamMetadataEvent object
- modelStreamErrorException
-
A streaming error occurred. Retry your request.
Type: Exception
HTTP Status Code: 424 -
The service isn't currently available. Try again later.
Type: Exception
HTTP Status Code: 503 - throttlingException
-
The number of requests exceeds the limit. Resubmit your request later.
Type: Exception
HTTP Status Code: 429 - validationException
-
Input validation failed. Check your request parameters and retry the request.
Type: Exception
HTTP Status Code: 400
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
- 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
Send a message to a model and stream the response.
Send a message to Anthropic Claude Sonnet with ConverseStream
and stream the response.
Sample Request
POST /model/anthropic.claude-3-sonnet-20240229-v1:0/converse-stream HTTP/1.1
{
"messages": [
{
"role": "user",
"content": [
{
"text": "Write an article about impact of high inflation to GDP of a country"
}
]
}
],
"system": [{"text" : "You are an economist with access to lots of data"}],
"inferenceConfig": {
"maxTokens": 1000,
"temperature": 0.5
}
}
See Also
For more information about using this API in one of the language-specific AWS SDKs, see the following: