Skip to content

converse async

converse(
    model_id: ConversationalModelId,
    *,
    config_overrides: Optional[AsyncBedrockRuntimeClientConfig] = None,
    messages: Optional[Messages] = None,
    system: Optional[SystemContentBlocks] = None,
    inference_config: Optional[InferenceConfiguration] = None,
    tool_config: Optional[ToolConfiguration] = None,
    guardrail_config: Optional[GuardrailConfiguration] = None,
    additional_model_request_fields: Optional[object] = None,
    prompt_variables: Optional[PromptVariableMap] = None,
    additional_model_response_field_paths: Optional[
        AdditionalModelResponseFieldPaths
    ] = None,
    request_metadata: Optional[RequestMetadata] = None,
    performance_config: Optional[PerformanceConfiguration] = None,
    service_tier: Optional[ServiceTier] = None,
    output_config: Optional[OutputConfig] = None,
) -> ConverseResponse

Sends messages to the specified Amazon Bedrock model. Converse provides a consistent interface that works with all models that support messages. This allows you to write code once and use it with different models. If a model has unique inference parameters, you can also pass those unique parameters to the model.

Amazon Bedrock doesn't store any text, images, or documents that you provide as content. The data is only used to generate the response.

You can submit a prompt by including it in the messages field, specifying the modelId of a foundation model or inference profile to run inference on it, and including any other fields that are relevant to your use case.

You can also submit a prompt from Prompt management by specifying the ARN of the prompt version and including a map of variables to values in the promptVariables field. You can append more messages to the prompt by using the messages field. If you use a prompt from Prompt management, you can't include the following fields in the request: additionalModelRequestFields, inferenceConfig, system, or toolConfig. Instead, these fields must be defined through Prompt management. For more information, see Use a prompt from Prompt management.

For information about the Converse API, see Use the Converse API in the Amazon Bedrock User Guide. To use a guardrail, see Use a guardrail with the Converse API in the Amazon Bedrock User Guide. To use a tool with a model, see Tool use (Function calling) in the Amazon Bedrock User Guide

For example code, see Converse API examples in the Amazon Bedrock User Guide.

This operation requires permission for the bedrock:InvokeModel action.

To deny all inference access to resources that you specify in the modelId field, you need to deny access to the bedrock:InvokeModel and bedrock:InvokeModelWithResponseStream actions. Doing this also denies access to the resource through the base inference actions (InvokeModel and InvokeModelWithResponseStream). For more information see Deny access for inference on specific models.

For troubleshooting some of the common errors you might encounter when using the Converse API, see Troubleshooting Amazon Bedrock API Error Codes in the Amazon Bedrock User Guide

Parameters:

  • model_id (ConversationalModelId) –

    Specifies the model or throughput with which to run inference, or the prompt resource to use in inference. The value depends on the resource that you use:

    The Converse API doesn't support imported models.

  • messages (Optional[Messages], default: None ) –

    The messages that you want to send to the model.

  • system (Optional[SystemContentBlocks], default: None ) –

    A prompt that provides instructions or context to the model about the task it should perform, or the persona it should adopt during the conversation.

  • inference_config (Optional[InferenceConfiguration], default: None ) –

    Inference parameters to pass to the model. Converse and ConverseStream support a base set of inference parameters. If you need to pass additional parameters that the model supports, use the additionalModelRequestFields request field.

  • tool_config (Optional[ToolConfiguration], default: None ) –

    Configuration information for the tools that the model can use when generating a response.

    For information about models that support tool use, see Supported models and model features.

  • guardrail_config (Optional[GuardrailConfiguration], default: None ) –

    Configuration information for a guardrail that you want to use in the request. If you include guardContent blocks in the content field in the messages field, the guardrail operates only on those messages. If you include no guardContent blocks, the guardrail operates on all messages in the request body and in any included prompt resource.

  • additional_model_request_fields (Optional[object], default: None ) –

    Additional inference parameters that the model supports, beyond the base set of inference parameters that Converse and ConverseStream support in the inferenceConfig field. For more information, see Model parameters.

  • prompt_variables (Optional[PromptVariableMap], default: None ) –

    Contains a map of variables in a prompt from Prompt management to objects containing the values to fill in for them when running model invocation. This field is ignored if you don't specify a prompt resource in the modelId field.

  • additional_model_response_field_paths (Optional[AdditionalModelResponseFieldPaths], default: None ) –

    Additional model parameters field paths to return in the response. Converse and ConverseStream return the requested fields as a JSON Pointer object in the additionalModelResponseFields field. The following is example JSON for additionalModelResponseFieldPaths.

    [ "/stop_sequence" ]

    For information about the JSON Pointer syntax, see the Internet Engineering Task Force (IETF) documentation.

    Converse and ConverseStream reject an empty JSON Pointer or incorrectly structured JSON Pointer with a 400 error code. if the JSON Pointer is valid, but the requested field is not in the model response, it is ignored by Converse.

  • request_metadata (Optional[RequestMetadata], default: None ) –

    Key-value pairs that you can use to filter invocation logs.

  • performance_config (Optional[PerformanceConfiguration], default: None ) –

    Model performance settings for the request.

  • service_tier (Optional[ServiceTier], default: None ) –

    Specifies the processing tier configuration used for serving the request.

  • output_config (Optional[OutputConfig], default: None ) –

    Output configuration for a model response.

Raises:

Source code in docs_src/capo_bedrock_runtime/_services/async_bedrock_runtime.py
async def converse(
    self,
    model_id: "capo_bedrock_runtime.types.conversational_model_id.ConversationalModelId",
    *,
    config_overrides: Optional[AsyncBedrockRuntimeClientConfig] = None,
    messages: Optional["capo_bedrock_runtime.types.messages.Messages"] = None,
    system: Optional[
        "capo_bedrock_runtime.types.system_content_blocks.SystemContentBlocks"
    ] = None,
    inference_config: Optional[
        "capo_bedrock_runtime.types.inference_configuration.InferenceConfiguration"
    ] = None,
    tool_config: Optional[
        "capo_bedrock_runtime.types.tool_configuration.ToolConfiguration"
    ] = None,
    guardrail_config: Optional[
        "capo_bedrock_runtime.types.guardrail_configuration.GuardrailConfiguration"
    ] = None,
    additional_model_request_fields: Optional[object] = None,
    prompt_variables: Optional[
        "capo_bedrock_runtime.types.prompt_variable_map.PromptVariableMap"
    ] = None,
    additional_model_response_field_paths: Optional[
        "capo_bedrock_runtime.types.additional_model_response_field_paths.AdditionalModelResponseFieldPaths"
    ] = None,
    request_metadata: Optional[
        "capo_bedrock_runtime.types.request_metadata.RequestMetadata"
    ] = None,
    performance_config: Optional[
        "capo_bedrock_runtime.types.performance_configuration.PerformanceConfiguration"
    ] = None,
    service_tier: Optional[
        "capo_bedrock_runtime.types.service_tier.ServiceTier"
    ] = None,
    output_config: Optional[
        "capo_bedrock_runtime.types.output_config.OutputConfig"
    ] = None,
) -> "capo_bedrock_runtime.types.converse_response.ConverseResponse":
    """<p>Sends messages to the specified Amazon Bedrock model. <code>Converse</code> provides a consistent interface that works with all models that support messages. This allows you to write code once and use it with different models. If a model has unique inference parameters, you can also pass those unique parameters to the model.</p> <p>Amazon Bedrock doesn't store any text, images, or documents that you provide as content. The data is only used to generate the response.</p> <p>You can submit a prompt by including it in the <code>messages</code> field, specifying the <code>modelId</code> of a foundation model or inference profile to run inference on it, and including any other fields that are relevant to your use case.</p> <p>You can also submit a prompt from Prompt management by specifying the ARN of the prompt version and including a map of variables to values in the <code>promptVariables</code> field. You can append more messages to the prompt by using the <code>messages</code> field. If you use a prompt from Prompt management, you can't include the following fields in the request: <code>additionalModelRequestFields</code>, <code>inferenceConfig</code>, <code>system</code>, or <code>toolConfig</code>. Instead, these fields must be defined through Prompt management. For more information, see <a href="https://docs.aws.amazon.com/bedrock/latest/userguide/prompt-management-use.html">Use a prompt from Prompt management</a>.</p> <p>For information about the Converse API, see <i>Use the Converse API</i> in the <i>Amazon Bedrock User Guide</i>. To use a guardrail, see <i>Use a guardrail with the Converse API</i> in the <i>Amazon Bedrock User Guide</i>. To use a tool with a model, see <i>Tool use (Function calling)</i> in the <i>Amazon Bedrock User Guide</i> </p> <p>For example code, see <i>Converse API examples</i> in the <i>Amazon Bedrock User Guide</i>. </p> <p>This operation requires permission for the <code>bedrock:InvokeModel</code> action. </p> <important> <p>To deny all inference access to resources that you specify in the modelId field, you need to deny access to the <code>bedrock:InvokeModel</code> and <code>bedrock:InvokeModelWithResponseStream</code> actions. Doing this also denies access to the resource through the base inference actions (<a href="https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModel.html">InvokeModel</a> and <a href="https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModelWithResponseStream.html">InvokeModelWithResponseStream</a>). For more information see <a href="https://docs.aws.amazon.com/bedrock/latest/userguide/security_iam_id-based-policy-examples.html#security_iam_id-based-policy-examples-deny-inference">Deny access for inference on specific models</a>. </p> </important> <p>For troubleshooting some of the common errors you might encounter when using the <code>Converse</code> API, see <a href="https://docs.aws.amazon.com/bedrock/latest/userguide/troubleshooting-api-error-codes.html">Troubleshooting Amazon Bedrock API Error Codes</a> in the Amazon Bedrock User Guide</p>

    Args:
        model_id: <p>Specifies the model or throughput with which to run inference, or the prompt resource to use in inference. The value depends on the resource that you use:</p> <ul> <li> <p>If you use a base model, specify the model ID or its ARN. For a list of model IDs for base models, see <a href="https://docs.aws.amazon.com/bedrock/latest/userguide/model-ids.html#model-ids-arns">Amazon Bedrock base model IDs (on-demand throughput)</a> in the Amazon Bedrock User Guide.</p> </li> <li> <p>If you use an inference profile, specify the inference profile ID or its ARN. For a list of inference profile IDs, see <a href="https://docs.aws.amazon.com/bedrock/latest/userguide/cross-region-inference-support.html">Supported Regions and models for cross-region inference</a> in the Amazon Bedrock User Guide.</p> </li> <li> <p>If you use a provisioned model, specify the ARN of the Provisioned Throughput. For more information, see <a href="https://docs.aws.amazon.com/bedrock/latest/userguide/prov-thru-use.html">Run inference using a Provisioned Throughput</a> in the Amazon Bedrock User Guide.</p> </li> <li> <p>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 <a href="https://docs.aws.amazon.com/bedrock/latest/userguide/model-customization-use.html">Use a custom model in Amazon Bedrock</a> in the Amazon Bedrock User Guide.</p> </li> <li> <p>To include a prompt that was defined in <a href="https://docs.aws.amazon.com/bedrock/latest/userguide/prompt-management.html">Prompt management</a>, specify the ARN of the prompt version to use.</p> </li> </ul> <p>The Converse API doesn't support <a href="https://docs.aws.amazon.com/bedrock/latest/userguide/model-customization-import-model.html">imported models</a>.</p>
        messages: <p>The messages that you want to send to the model.</p>
        system: <p>A prompt that provides instructions or context to the model about the task it should perform, or the persona it should adopt during the conversation.</p>
        inference_config: <p>Inference parameters to pass to the model. <code>Converse</code> and <code>ConverseStream</code> support a base set of inference parameters. If you need to pass additional parameters that the model supports, use the <code>additionalModelRequestFields</code> request field.</p>
        tool_config: <p>Configuration information for the tools that the model can use when generating a response. </p> <p>For information about models that support tool use, see <a href="https://docs.aws.amazon.com/bedrock/latest/userguide/conversation-inference.html#conversation-inference-supported-models-features">Supported models and model features</a>.</p>
        guardrail_config: <p>Configuration information for a guardrail that you want to use in the request. If you include <code>guardContent</code> blocks in the <code>content</code> field in the <code>messages</code> field, the guardrail operates only on those messages. If you include no <code>guardContent</code> blocks, the guardrail operates on all messages in the request body and in any included prompt resource.</p>
        additional_model_request_fields: <p>Additional inference parameters that the model supports, beyond the base set of inference parameters that <code>Converse</code> and <code>ConverseStream</code> support in the <code>inferenceConfig</code> field. For more information, see <a href="https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters.html">Model parameters</a>.</p>
        prompt_variables: <p>Contains a map of variables in a prompt from Prompt management to objects containing the values to fill in for them when running model invocation. This field is ignored if you don't specify a prompt resource in the <code>modelId</code> field.</p>
        additional_model_response_field_paths: <p>Additional model parameters field paths to return in the response. <code>Converse</code> and <code>ConverseStream</code> return the requested fields as a JSON Pointer object in the <code>additionalModelResponseFields</code> field. The following is example JSON for <code>additionalModelResponseFieldPaths</code>.</p> <p> <code>[ "/stop_sequence" ]</code> </p> <p>For information about the JSON Pointer syntax, see the <a href="https://datatracker.ietf.org/doc/html/rfc6901">Internet Engineering Task Force (IETF)</a> documentation.</p> <p> <code>Converse</code> and <code>ConverseStream</code> reject an empty JSON Pointer or incorrectly structured JSON Pointer with a <code>400</code> error code. if the JSON Pointer is valid, but the requested field is not in the model response, it is ignored by <code>Converse</code>.</p>
        request_metadata: <p>Key-value pairs that you can use to filter invocation logs.</p>
        performance_config: <p>Model performance settings for the request.</p>
        service_tier: <p>Specifies the processing tier configuration used for serving the request.</p>
        output_config: <p>Output configuration for a model response.</p>

    Raises:
        capo_bedrock_runtime.errors.access_denied_exception.AccessDeniedException: <p>The request is denied because you do not have sufficient permissions to perform the requested action. For troubleshooting this error, see <a href="https://docs.aws.amazon.com/bedrock/latest/userguide/troubleshooting-api-error-codes.html#ts-access-denied">AccessDeniedException</a> in the Amazon Bedrock User Guide</p>
        capo_bedrock_runtime.errors.internal_server_exception.InternalServerException: <p>An internal server error occurred. For troubleshooting this error, see <a href="https://docs.aws.amazon.com/bedrock/latest/userguide/troubleshooting-api-error-codes.html#ts-internal-failure">InternalFailure</a> in the Amazon Bedrock User Guide</p>
        capo_bedrock_runtime.errors.model_error_exception.ModelErrorException: <p>The request failed due to an error while processing the model.</p>
        capo_bedrock_runtime.errors.model_not_ready_exception.ModelNotReadyException: <p>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 <a href="https://docs.aws.amazon.com/sdkref/latest/guide/feature-retry-behavior.html">Retry behavior</a> in the <i>AWS SDKs and Tools</i> reference guide.</p>
        capo_bedrock_runtime.errors.model_timeout_exception.ModelTimeoutException: <p>The request took too long to process. Processing time exceeded the model timeout length.</p>
        capo_bedrock_runtime.errors.resource_not_found_exception.ResourceNotFoundException: <p>The specified resource ARN was not found. For troubleshooting this error, see <a href="https://docs.aws.amazon.com/bedrock/latest/userguide/troubleshooting-api-error-codes.html#ts-resource-not-found">ResourceNotFound</a> in the Amazon Bedrock User Guide</p>
        capo_bedrock_runtime.errors.service_unavailable_exception.ServiceUnavailableException: <p>The service isn't currently available. For troubleshooting this error, see <a href="https://docs.aws.amazon.com/bedrock/latest/userguide/troubleshooting-api-error-codes.html#ts-service-unavailable">ServiceUnavailable</a> in the Amazon Bedrock User Guide</p>
        capo_bedrock_runtime.errors.throttling_exception.ThrottlingException: <p>Your request was denied due to exceeding the account quotas for <i>Amazon Bedrock</i>. For troubleshooting this error, see <a href="https://docs.aws.amazon.com/bedrock/latest/userguide/troubleshooting-api-error-codes.html#ts-throttling-exception">ThrottlingException</a> in the Amazon Bedrock User Guide</p>
        capo_bedrock_runtime.errors.validation_exception.ValidationException: <p>The input fails to satisfy the constraints specified by <i>Amazon Bedrock</i>. For troubleshooting this error, see <a href="https://docs.aws.amazon.com/bedrock/latest/userguide/troubleshooting-api-error-codes.html#ts-validation-error">ValidationError</a> in the Amazon Bedrock User Guide</p>
        capo_bedrock_runtime.errors.UnknownServiceError: The service returned an error code this client does not model.
    """

    async def _handler(
        req: "AsyncOperationRequest[capo_bedrock_runtime.types.converse_request.ConverseRequest]",
    ) -> AsyncOperationResponse[
        "capo_bedrock_runtime.types.converse_response.ConverseResponse"
    ]:
        import capo_bedrock_runtime._operations.amazon_bedrock_frontend_service.converse

        (
            output,
            http_response,
        ) = await capo_bedrock_runtime._operations.amazon_bedrock_frontend_service.converse.async_converse(
            req.options, req.input
        )
        return AsyncOperationResponse(output=output, response=http_response)

    def _redact_input(
        input_: "capo_bedrock_runtime.types.converse_request.ConverseRequest",
    ) -> dict[str, Any]:
        import capo_bedrock_runtime.types.converse_request

        return capo_bedrock_runtime.types.converse_request.redact_sensitive(input_)

    def _redact_output(
        output: "capo_bedrock_runtime.types.converse_response.ConverseResponse",
    ) -> dict[str, Any]:
        import capo_bedrock_runtime.types.converse_response

        return capo_bedrock_runtime.types.converse_response.redact_sensitive(output)

    interceptors_, options_ = self.operation_options(
        config_overrides,
        OperationLogging(
            "Converse", redact_input=_redact_input, redact_output=_redact_output
        ),
    )
    input_: capo_bedrock_runtime.types.converse_request.ConverseRequest = {
        "model_id": model_id
    }
    if messages is not None:
        input_["messages"] = messages
    if system is not None:
        input_["system"] = system
    if inference_config is not None:
        input_["inference_config"] = inference_config
    if tool_config is not None:
        input_["tool_config"] = tool_config
    if guardrail_config is not None:
        input_["guardrail_config"] = guardrail_config
    if additional_model_request_fields is not None:
        input_["additional_model_request_fields"] = additional_model_request_fields
    if prompt_variables is not None:
        input_["prompt_variables"] = prompt_variables
    if additional_model_response_field_paths is not None:
        input_["additional_model_response_field_paths"] = (
            additional_model_response_field_paths
        )
    if request_metadata is not None:
        input_["request_metadata"] = request_metadata
    if performance_config is not None:
        input_["performance_config"] = performance_config
    if service_tier is not None:
        input_["service_tier"] = service_tier
    if output_config is not None:
        input_["output_config"] = output_config

    response = await aexecute_pipeline(
        AsyncOperationRequest(input=input_, options=options_),
        handler=_handler,
        interceptors=list(interceptors_),
    )
    await response.response.aclose()
    return response.output