Skip to content

post_content

post_content(
    bot_name: BotName,
    bot_alias: BotAlias,
    user_id: UserId,
    content_type: HttpContentType,
    input_stream: Body[Iterator[bytes]] | Iterator[bytes] | bytes,
    *,
    config_overrides: Optional[LexRuntimeServiceClientConfig] = None,
    session_attributes: Optional[SynthesizedJsonAttributesString] = None,
    request_attributes: Optional[SynthesizedJsonAttributesString] = None,
    accept: Optional[Accept] = None,
    active_contexts: Optional[SynthesizedJsonActiveContextsString] = None,
) -> Generator[PostContentResponse]

Sends user input (text or speech) to Amazon Lex. Clients use this API to send text and audio requests to Amazon Lex at runtime. Amazon Lex interprets the user input using the machine learning model that it built for the bot.

The PostContent operation supports audio input at 8kHz and 16kHz. You can use 8kHz audio to achieve higher speech recognition accuracy in telephone audio applications.

In response, Amazon Lex returns the next message to convey to the user. Consider the following example messages:

  • For a user input "I would like a pizza," Amazon Lex might return a response with a message eliciting slot data (for example, PizzaSize): "What size pizza would you like?".

  • After the user provides all of the pizza order information, Amazon Lex might return a response with a message to get user confirmation: "Order the pizza?".

  • After the user replies "Yes" to the confirmation prompt, Amazon Lex might return a conclusion statement: "Thank you, your cheese pizza has been ordered.".

Not all Amazon Lex messages require a response from the user. For example, conclusion statements do not require a response. Some messages require only a yes or no response. In addition to the message, Amazon Lex provides additional context about the message in the response that you can use to enhance client behavior, such as displaying the appropriate client user interface. Consider the following examples:

  • If the message is to elicit slot data, Amazon Lex returns the following context information:

    • x-amz-lex-dialog-state header set to ElicitSlot

    • x-amz-lex-intent-name header set to the intent name in the current context

    • x-amz-lex-slot-to-elicit header set to the slot name for which the message is eliciting information

    • x-amz-lex-slots header set to a map of slots configured for the intent with their current values

  • If the message is a confirmation prompt, the x-amz-lex-dialog-state header is set to Confirmation and the x-amz-lex-slot-to-elicit header is omitted.

  • If the message is a clarification prompt configured for the intent, indicating that the user intent is not understood, the x-amz-dialog-state header is set to ElicitIntent and the x-amz-slot-to-elicit header is omitted.

In addition, Amazon Lex also returns your application-specific sessionAttributes. For more information, see Managing Conversation Context.

Parameters:

  • bot_name (BotName) –

    Name of the Amazon Lex bot.

  • bot_alias (BotAlias) –

    Alias of the Amazon Lex bot.

  • user_id (UserId) –

    The ID of the client application user. Amazon Lex uses this to identify a user's conversation with your bot. At runtime, each request must contain the userID field.

    To decide the user ID to use for your application, consider the following factors.

    • The userID field must not contain any personally identifiable information of the user, for example, name, personal identification numbers, or other end user personal information.

    • If you want a user to start a conversation on one device and continue on another device, use a user-specific identifier.

    • If you want the same user to be able to have two independent conversations on two different devices, choose a device-specific identifier.

    • A user can't have two independent conversations with two different versions of the same bot. For example, a user can't have a conversation with the PROD and BETA versions of the same bot. If you anticipate that a user will need to have conversation with two different versions, for example, while testing, include the bot alias in the user ID to separate the two conversations.

  • session_attributes (Optional[SynthesizedJsonAttributesString], default: None ) –

    You pass this value as the x-amz-lex-session-attributes HTTP header.

    Application-specific information passed between Amazon Lex and a client application. The value must be a JSON serialized and base64 encoded map with string keys and values. The total size of the sessionAttributes and requestAttributes headers is limited to 12 KB.

    For more information, see Setting Session Attributes.

  • request_attributes (Optional[SynthesizedJsonAttributesString], default: None ) –

    You pass this value as the x-amz-lex-request-attributes HTTP header.

    Request-specific information passed between Amazon Lex and a client application. The value must be a JSON serialized and base64 encoded map with string keys and values. The total size of the requestAttributes and sessionAttributes headers is limited to 12 KB.

    The namespace x-amz-lex: is reserved for special attributes. Don't create any request attributes with the prefix x-amz-lex:.

    For more information, see Setting Request Attributes.

  • content_type (HttpContentType) –

    You pass this value as the Content-Type HTTP header.

    Indicates the audio format or text. The header value must start with one of the following prefixes:

    • PCM format, audio data must be in little-endian byte order.

      • audio/l16; rate=16000; channels=1

      • audio/x-l16; sample-rate=16000; channel-count=1

      • audio/lpcm; sample-rate=8000; sample-size-bits=16; channel-count=1; is-big-endian=false

    • Opus format

      • audio/x-cbr-opus-with-preamble; preamble-size=0; bit-rate=256000; frame-size-milliseconds=4

    • Text format

      • text/plain; charset=utf-8

  • accept (Optional[Accept], default: None ) –

    You pass this value as the Accept HTTP header.

    The message Amazon Lex returns in the response can be either text or speech based on the Accept HTTP header value in the request.

    • If the value is text/plain; charset=utf-8, Amazon Lex returns text in the response.

    • If the value begins with audio/, Amazon Lex returns speech in the response. Amazon Lex uses Amazon Polly to generate the speech (using the configuration you specified in the Accept header). For example, if you specify audio/mpeg as the value, Amazon Lex returns speech in the MPEG format.

    • If the value is audio/pcm, the speech returned is audio/pcm in 16-bit, little endian format.

    • The following are the accepted values:

      • audio/mpeg

      • audio/ogg

      • audio/pcm

      • text/plain; charset=utf-8

      • audio/* (defaults to mpeg)

  • input_stream (Body[Iterator[bytes]] | Iterator[bytes] | bytes) –

    User input in PCM or Opus audio format or text format as described in the Content-Type HTTP header.

    You can stream audio data to Amazon Lex or you can create a local buffer that captures all of the audio data before sending. In general, you get better performance if you stream audio data rather than buffering the data locally.

  • active_contexts (Optional[SynthesizedJsonActiveContextsString], default: None ) –

    A list of contexts active for the request. A context can be activated when a previous intent is fulfilled, or by including the context in the request,

    If you don't specify a list of contexts, Amazon Lex will use the current list of contexts for the session. If you specify an empty list, all contexts for the session are cleared.

Raises:

  • BadGatewayException –

    Either the Amazon Lex bot is still building, or one of the dependent services (Amazon Polly, AWS Lambda) failed with an internal service error.

  • BadRequestException –

    Request validation failed, there is no usable message in the context, or the bot build failed, is still in progress, or contains unbuilt changes.

  • ConflictException –

    Two clients are using the same AWS account, Amazon Lex bot, and user ID.

  • DependencyFailedException –

    One of the dependencies, such as AWS Lambda or Amazon Polly, threw an exception. For example,

    • If Amazon Lex does not have sufficient permissions to call a Lambda function.

    • If a Lambda function takes longer than 30 seconds to execute.

    • If a fulfillment Lambda function returns a Delegate dialog action without removing any slot values.

  • InternalFailureException –

    Internal service error. Retry the call.

  • LimitExceededException –

    Exceeded a limit.

  • LoopDetectedException –

    This exception is not used.

  • NotAcceptableException –

    The accept header in the request does not have a valid value.

  • NotFoundException –

    The resource (such as the Amazon Lex bot or an alias) that is referred to is not found.

  • RequestTimeoutException –

    The input speech is too long.

  • UnsupportedMediaTypeException –

    The Content-Type header (PostContent API) has an invalid value.

  • UnknownServiceError –

    The service returned an error code this client does not model.

Source code in docs_src/capo_lex_runtime_service/_services/lex_runtime_service.py
@contextmanager
def post_content(
    self,
    bot_name: "capo_lex_runtime_service.types.bot_name.BotName",
    bot_alias: "capo_lex_runtime_service.types.bot_alias.BotAlias",
    user_id: "capo_lex_runtime_service.types.user_id.UserId",
    content_type: "capo_lex_runtime_service.types.http_content_type.HttpContentType",
    input_stream: Body[Iterator[bytes]] | Iterator[bytes] | bytes,
    *,
    config_overrides: Optional[LexRuntimeServiceClientConfig] = None,
    session_attributes: Optional[
        "capo_lex_runtime_service.types.synthesized_json_attributes_string.SynthesizedJsonAttributesString"
    ] = None,
    request_attributes: Optional[
        "capo_lex_runtime_service.types.synthesized_json_attributes_string.SynthesizedJsonAttributesString"
    ] = None,
    accept: Optional["capo_lex_runtime_service.types.accept.Accept"] = None,
    active_contexts: Optional[
        "capo_lex_runtime_service.types.synthesized_json_active_contexts_string.SynthesizedJsonActiveContextsString"
    ] = None,
) -> "Generator[capo_lex_runtime_service.types.post_content_response.PostContentResponse]":
    """<p> Sends user input (text or speech) to Amazon Lex. Clients use this API to send text and audio requests to Amazon Lex at runtime. Amazon Lex interprets the user input using the machine learning model that it built for the bot. </p> <p>The <code>PostContent</code> operation supports audio input at 8kHz and 16kHz. You can use 8kHz audio to achieve higher speech recognition accuracy in telephone audio applications. </p> <p> In response, Amazon Lex returns the next message to convey to the user. Consider the following example messages: </p> <ul> <li> <p> For a user input "I would like a pizza," Amazon Lex might return a response with a message eliciting slot data (for example, <code>PizzaSize</code>): "What size pizza would you like?". </p> </li> <li> <p> After the user provides all of the pizza order information, Amazon Lex might return a response with a message to get user confirmation: "Order the pizza?". </p> </li> <li> <p> After the user replies "Yes" to the confirmation prompt, Amazon Lex might return a conclusion statement: "Thank you, your cheese pizza has been ordered.". </p> </li> </ul> <p> Not all Amazon Lex messages require a response from the user. For example, conclusion statements do not require a response. Some messages require only a yes or no response. In addition to the <code>message</code>, Amazon Lex provides additional context about the message in the response that you can use to enhance client behavior, such as displaying the appropriate client user interface. Consider the following examples: </p> <ul> <li> <p> If the message is to elicit slot data, Amazon Lex returns the following context information: </p> <ul> <li> <p> <code>x-amz-lex-dialog-state</code> header set to <code>ElicitSlot</code> </p> </li> <li> <p> <code>x-amz-lex-intent-name</code> header set to the intent name in the current context </p> </li> <li> <p> <code>x-amz-lex-slot-to-elicit</code> header set to the slot name for which the <code>message</code> is eliciting information </p> </li> <li> <p> <code>x-amz-lex-slots</code> header set to a map of slots configured for the intent with their current values </p> </li> </ul> </li> <li> <p> If the message is a confirmation prompt, the <code>x-amz-lex-dialog-state</code> header is set to <code>Confirmation</code> and the <code>x-amz-lex-slot-to-elicit</code> header is omitted. </p> </li> <li> <p> If the message is a clarification prompt configured for the intent, indicating that the user intent is not understood, the <code>x-amz-dialog-state</code> header is set to <code>ElicitIntent</code> and the <code>x-amz-slot-to-elicit</code> header is omitted. </p> </li> </ul> <p> In addition, Amazon Lex also returns your application-specific <code>sessionAttributes</code>. For more information, see <a href="https://docs.aws.amazon.com/lex/latest/dg/context-mgmt.html">Managing Conversation Context</a>. </p>

    Args:
        bot_name: <p>Name of the Amazon Lex bot.</p>
        bot_alias: <p>Alias of the Amazon Lex bot.</p>
        user_id: <p>The ID of the client application user. Amazon Lex uses this to identify a user's conversation with your bot. At runtime, each request must contain the <code>userID</code> field.</p> <p>To decide the user ID to use for your application, consider the following factors.</p> <ul> <li> <p>The <code>userID</code> field must not contain any personally identifiable information of the user, for example, name, personal identification numbers, or other end user personal information.</p> </li> <li> <p>If you want a user to start a conversation on one device and continue on another device, use a user-specific identifier.</p> </li> <li> <p>If you want the same user to be able to have two independent conversations on two different devices, choose a device-specific identifier.</p> </li> <li> <p>A user can't have two independent conversations with two different versions of the same bot. For example, a user can't have a conversation with the PROD and BETA versions of the same bot. If you anticipate that a user will need to have conversation with two different versions, for example, while testing, include the bot alias in the user ID to separate the two conversations.</p> </li> </ul>
        session_attributes: <p>You pass this value as the <code>x-amz-lex-session-attributes</code> HTTP header.</p> <p>Application-specific information passed between Amazon Lex and a client application. The value must be a JSON serialized and base64 encoded map with string keys and values. The total size of the <code>sessionAttributes</code> and <code>requestAttributes</code> headers is limited to 12 KB.</p> <p>For more information, see <a href="https://docs.aws.amazon.com/lex/latest/dg/context-mgmt.html#context-mgmt-session-attribs">Setting Session Attributes</a>.</p>
        request_attributes: <p>You pass this value as the <code>x-amz-lex-request-attributes</code> HTTP header.</p> <p>Request-specific information passed between Amazon Lex and a client application. The value must be a JSON serialized and base64 encoded map with string keys and values. The total size of the <code>requestAttributes</code> and <code>sessionAttributes</code> headers is limited to 12 KB.</p> <p>The namespace <code>x-amz-lex:</code> is reserved for special attributes. Don't create any request attributes with the prefix <code>x-amz-lex:</code>.</p> <p>For more information, see <a href="https://docs.aws.amazon.com/lex/latest/dg/context-mgmt.html#context-mgmt-request-attribs">Setting Request Attributes</a>.</p>
        content_type: <p> You pass this value as the <code>Content-Type</code> HTTP header. </p> <p> Indicates the audio format or text. The header value must start with one of the following prefixes: </p> <ul> <li> <p>PCM format, audio data must be in little-endian byte order.</p> <ul> <li> <p>audio/l16; rate=16000; channels=1</p> </li> <li> <p>audio/x-l16; sample-rate=16000; channel-count=1</p> </li> <li> <p>audio/lpcm; sample-rate=8000; sample-size-bits=16; channel-count=1; is-big-endian=false </p> </li> </ul> </li> <li> <p>Opus format</p> <ul> <li> <p>audio/x-cbr-opus-with-preamble; preamble-size=0; bit-rate=256000; frame-size-milliseconds=4</p> </li> </ul> </li> <li> <p>Text format</p> <ul> <li> <p>text/plain; charset=utf-8</p> </li> </ul> </li> </ul>
        accept: <p> You pass this value as the <code>Accept</code> HTTP header. </p> <p> The message Amazon Lex returns in the response can be either text or speech based on the <code>Accept</code> HTTP header value in the request. </p> <ul> <li> <p> If the value is <code>text/plain; charset=utf-8</code>, Amazon Lex returns text in the response. </p> </li> <li> <p> If the value begins with <code>audio/</code>, Amazon Lex returns speech in the response. Amazon Lex uses Amazon Polly to generate the speech (using the configuration you specified in the <code>Accept</code> header). For example, if you specify <code>audio/mpeg</code> as the value, Amazon Lex returns speech in the MPEG format.</p> </li> <li> <p>If the value is <code>audio/pcm</code>, the speech returned is <code>audio/pcm</code> in 16-bit, little endian format. </p> </li> <li> <p>The following are the accepted values:</p> <ul> <li> <p>audio/mpeg</p> </li> <li> <p>audio/ogg</p> </li> <li> <p>audio/pcm</p> </li> <li> <p>text/plain; charset=utf-8</p> </li> <li> <p>audio/* (defaults to mpeg)</p> </li> </ul> </li> </ul>
        input_stream: <p> User input in PCM or Opus audio format or text format as described in the <code>Content-Type</code> HTTP header. </p> <p>You can stream audio data to Amazon Lex or you can create a local buffer that captures all of the audio data before sending. In general, you get better performance if you stream audio data rather than buffering the data locally.</p>
        active_contexts: <p>A list of contexts active for the request. A context can be activated when a previous intent is fulfilled, or by including the context in the request,</p> <p>If you don't specify a list of contexts, Amazon Lex will use the current list of contexts for the session. If you specify an empty list, all contexts for the session are cleared.</p>

    Raises:
        capo_lex_runtime_service.errors.bad_gateway_exception.BadGatewayException: <p>Either the Amazon Lex bot is still building, or one of the dependent services (Amazon Polly, AWS Lambda) failed with an internal service error.</p>
        capo_lex_runtime_service.errors.bad_request_exception.BadRequestException: <p> Request validation failed, there is no usable message in the context, or the bot build failed, is still in progress, or contains unbuilt changes. </p>
        capo_lex_runtime_service.errors.conflict_exception.ConflictException: <p> Two clients are using the same AWS account, Amazon Lex bot, and user ID. </p>
        capo_lex_runtime_service.errors.dependency_failed_exception.DependencyFailedException: <p> One of the dependencies, such as AWS Lambda or Amazon Polly, threw an exception. For example, </p> <ul> <li> <p>If Amazon Lex does not have sufficient permissions to call a Lambda function.</p> </li> <li> <p>If a Lambda function takes longer than 30 seconds to execute.</p> </li> <li> <p>If a fulfillment Lambda function returns a <code>Delegate</code> dialog action without removing any slot values.</p> </li> </ul>
        capo_lex_runtime_service.errors.internal_failure_exception.InternalFailureException: <p>Internal service error. Retry the call.</p>
        capo_lex_runtime_service.errors.limit_exceeded_exception.LimitExceededException: <p>Exceeded a limit.</p>
        capo_lex_runtime_service.errors.loop_detected_exception.LoopDetectedException: <p>This exception is not used.</p>
        capo_lex_runtime_service.errors.not_acceptable_exception.NotAcceptableException: <p>The accept header in the request does not have a valid value.</p>
        capo_lex_runtime_service.errors.not_found_exception.NotFoundException: <p>The resource (such as the Amazon Lex bot or an alias) that is referred to is not found.</p>
        capo_lex_runtime_service.errors.request_timeout_exception.RequestTimeoutException: <p>The input speech is too long.</p>
        capo_lex_runtime_service.errors.unsupported_media_type_exception.UnsupportedMediaTypeException: <p>The Content-Type header (<code>PostContent</code> API) has an invalid value. </p>
        capo_lex_runtime_service.errors.UnknownServiceError: The service returned an error code this client does not model.
    """

    def _handler(
        req: "OperationRequest[capo_lex_runtime_service.types.post_content_request.PostContentRequest]",
    ) -> OperationResponse[
        "capo_lex_runtime_service.types.post_content_response.PostContentResponse"
    ]:
        import capo_lex_runtime_service._operations.aws_deep_sense_run_time_service.post_content

        output, http_response = (
            capo_lex_runtime_service._operations.aws_deep_sense_run_time_service.post_content.post_content(
                req.options, req.input
            )
        )
        return OperationResponse(output=output, response=http_response)

    def _redact_input(
        input_: "capo_lex_runtime_service.types.post_content_request.PostContentRequest",
    ) -> dict[str, Any]:
        import capo_lex_runtime_service.types.post_content_request

        return capo_lex_runtime_service.types.post_content_request.redact_sensitive(
            input_
        )

    def _redact_output(
        output: "capo_lex_runtime_service.types.post_content_response.PostContentResponse",
    ) -> dict[str, Any]:
        import capo_lex_runtime_service.types.post_content_response

        return (
            capo_lex_runtime_service.types.post_content_response.redact_sensitive(
                output
            )
        )

    interceptors_, options_ = self.operation_options(
        config_overrides,
        OperationLogging(
            "PostContent", redact_input=_redact_input, redact_output=_redact_output
        ),
    )
    input_: capo_lex_runtime_service.types.post_content_request.PostContentRequest = {
        "bot_name": bot_name,
        "bot_alias": bot_alias,
        "user_id": user_id,
        "content_type": content_type,
        "input_stream": ensure_sync_iterator(input_stream),
    }
    if session_attributes is not None:
        input_["session_attributes"] = session_attributes
    if request_attributes is not None:
        input_["request_attributes"] = request_attributes
    if accept is not None:
        input_["accept"] = accept
    if active_contexts is not None:
        input_["active_contexts"] = active_contexts

    response = execute_pipeline(
        OperationRequest(input=input_, options=options_),
        handler=_handler,
        interceptors=list(interceptors_),
    )
    try:
        yield response.output
    finally:
        response.response.close()