Skip to content

classify_document

classify_document(
    endpoint_arn: DocumentClassifierEndpointArn,
    *,
    config_overrides: Optional[ComprehendClientConfig] = None,
    text: Optional[CustomerInputString] = None,
    bytes: Optional[SemiStructuredDocumentBlob] = None,
    document_reader_config: Optional[DocumentReaderConfig] = None,
) -> ClassifyDocumentResponse

Creates a classification request to analyze a single document in real-time. ClassifyDocument supports the following model types:

  • Custom classifier - a custom model that you have created and trained. For input, you can provide plain text, a single-page document (PDF, Word, or image), or Amazon Textract API output. For more information, see Custom classification in the Amazon Comprehend Developer Guide.

  • Prompt safety classifier - Amazon Comprehend provides a pre-trained model for classifying input prompts for generative AI applications. For input, you provide English plain text input. For prompt safety classification, the response includes only the Classes field. For more information about prompt safety classifiers, see Prompt safety classification in the Amazon Comprehend Developer Guide.

If the system detects errors while processing a page in the input document, the API response includes an Errors field that describes the errors.

If the system detects a document-level error in your input document, the API returns an InvalidRequestException error response. For details about this exception, see Errors in semi-structured documents in the Comprehend Developer Guide.

Parameters:

  • text (Optional[CustomerInputString], default: None ) –

    The document text to be analyzed. If you enter text using this parameter, do not use the Bytes parameter.

  • endpoint_arn (DocumentClassifierEndpointArn) –

    The Amazon Resource Number (ARN) of the endpoint.

    For prompt safety classification, Amazon Comprehend provides the endpoint ARN. For more information about prompt safety classifiers, see Prompt safety classification in the Amazon Comprehend Developer Guide

    For custom classification, you create an endpoint for your custom model. For more information, see Using Amazon Comprehend endpoints.

  • bytes (Optional[SemiStructuredDocumentBlob], default: None ) –

    Use the Bytes parameter to input a text, PDF, Word or image file.

    When you classify a document using a custom model, you can also use the Bytes parameter to input an Amazon Textract DetectDocumentText or AnalyzeDocument output file.

    To classify a document using the prompt safety classifier, use the Text parameter for input.

    Provide the input document as a sequence of base64-encoded bytes. If your code uses an Amazon Web Services SDK to classify documents, the SDK may encode the document file bytes for you.

    The maximum length of this field depends on the input document type. For details, see Inputs for real-time custom analysis in the Comprehend Developer Guide.

    If you use the Bytes parameter, do not use the Text parameter.

  • document_reader_config (Optional[DocumentReaderConfig], default: None ) –

    Provides configuration parameters to override the default actions for extracting text from PDF documents and image files.

Raises:

Source code in docs_src/capo_comprehend/_services/comprehend.py
def classify_document(
    self,
    endpoint_arn: "capo_comprehend.types.document_classifier_endpoint_arn.DocumentClassifierEndpointArn",
    *,
    config_overrides: Optional[ComprehendClientConfig] = None,
    text: Optional[
        "capo_comprehend.types.customer_input_string.CustomerInputString"
    ] = None,
    bytes: Optional[
        "capo_comprehend.types.semi_structured_document_blob.SemiStructuredDocumentBlob"
    ] = None,
    document_reader_config: Optional[
        "capo_comprehend.types.document_reader_config.DocumentReaderConfig"
    ] = None,
) -> "capo_comprehend.types.classify_document_response.ClassifyDocumentResponse":
    """<p>Creates a classification request to analyze a single document in real-time. <code>ClassifyDocument</code> supports the following model types:</p> <ul> <li> <p>Custom classifier - a custom model that you have created and trained. For input, you can provide plain text, a single-page document (PDF, Word, or image), or Amazon Textract API output. For more information, see <a href="https://docs.aws.amazon.com/comprehend/latest/dg/how-document-classification.html">Custom classification</a> in the <i>Amazon Comprehend Developer Guide</i>.</p> </li> <li> <p>Prompt safety classifier - Amazon Comprehend provides a pre-trained model for classifying input prompts for generative AI applications. For input, you provide English plain text input. For prompt safety classification, the response includes only the <code>Classes</code> field. For more information about prompt safety classifiers, see <a href="https://docs.aws.amazon.com/comprehend/latest/dg/trust-safety.html#prompt-classification">Prompt safety classification</a> in the <i>Amazon Comprehend Developer Guide</i>.</p> </li> </ul> <p>If the system detects errors while processing a page in the input document, the API response includes an <code>Errors</code> field that describes the errors.</p> <p>If the system detects a document-level error in your input document, the API returns an <code>InvalidRequestException</code> error response. For details about this exception, see <a href="https://docs.aws.amazon.com/comprehend/latest/dg/idp-inputs-sync-err.html"> Errors in semi-structured documents</a> in the Comprehend Developer Guide. </p>

    Args:
        text: <p>The document text to be analyzed. If you enter text using this parameter, do not use the <code>Bytes</code> parameter.</p>
        endpoint_arn: <p>The Amazon Resource Number (ARN) of the endpoint. </p> <p>For prompt safety classification, Amazon Comprehend provides the endpoint ARN. For more information about prompt safety classifiers, see <a href="https://docs.aws.amazon.com/comprehend/latest/dg/trust-safety.html#prompt-classification">Prompt safety classification</a> in the <i>Amazon Comprehend Developer Guide</i> </p> <p>For custom classification, you create an endpoint for your custom model. For more information, see <a href="https://docs.aws.amazon.com/comprehend/latest/dg/using-endpoints.html">Using Amazon Comprehend endpoints</a>.</p>
        bytes: <p>Use the <code>Bytes</code> parameter to input a text, PDF, Word or image file.</p> <p>When you classify a document using a custom model, you can also use the <code>Bytes</code> parameter to input an Amazon Textract <code>DetectDocumentText</code> or <code>AnalyzeDocument</code> output file.</p> <p>To classify a document using the prompt safety classifier, use the <code>Text</code> parameter for input.</p> <p>Provide the input document as a sequence of base64-encoded bytes. If your code uses an Amazon Web Services SDK to classify documents, the SDK may encode the document file bytes for you. </p> <p>The maximum length of this field depends on the input document type. For details, see <a href="https://docs.aws.amazon.com/comprehend/latest/dg/idp-inputs-sync.html"> Inputs for real-time custom analysis</a> in the Comprehend Developer Guide. </p> <p>If you use the <code>Bytes</code> parameter, do not use the <code>Text</code> parameter.</p>
        document_reader_config: <p>Provides configuration parameters to override the default actions for extracting text from PDF documents and image files.</p>

    Raises:
        capo_comprehend.errors.internal_server_exception.InternalServerException: <p>An internal server error occurred. Retry your request.</p>
        capo_comprehend.errors.invalid_request_exception.InvalidRequestException: <p>The request is invalid.</p>
        capo_comprehend.errors.resource_unavailable_exception.ResourceUnavailableException: <p>The specified resource is not available. Check the resource and try your request again.</p>
        capo_comprehend.errors.text_size_limit_exceeded_exception.TextSizeLimitExceededException: <p>The size of the input text exceeds the limit. Use a smaller document.</p>
        capo_comprehend.errors.UnknownServiceError: The service returned an error code this client does not model.
    """

    def _handler(
        req: "OperationRequest[capo_comprehend.types.classify_document_request.ClassifyDocumentRequest]",
    ) -> OperationResponse[
        "capo_comprehend.types.classify_document_response.ClassifyDocumentResponse"
    ]:
        import capo_comprehend._operations.comprehend_20171127.classify_document

        output, http_response = (
            capo_comprehend._operations.comprehend_20171127.classify_document.classify_document(
                req.options, req.input
            )
        )
        return OperationResponse(output=output, response=http_response)

    def _redact_input(
        input_: "capo_comprehend.types.classify_document_request.ClassifyDocumentRequest",
    ) -> dict[str, Any]:
        import capo_comprehend.types.classify_document_request

        return capo_comprehend.types.classify_document_request.redact_sensitive(
            input_
        )

    interceptors_, options_ = self.operation_options(
        config_overrides,
        OperationLogging("ClassifyDocument", redact_input=_redact_input),
    )
    input_: capo_comprehend.types.classify_document_request.ClassifyDocumentRequest = {
        "endpoint_arn": endpoint_arn
    }
    if text is not None:
        input_["text"] = text
    if bytes is not None:
        input_["bytes"] = bytes
    if document_reader_config is not None:
        input_["document_reader_config"] = document_reader_config

    response = execute_pipeline(
        OperationRequest(input=input_, options=options_),
        handler=_handler,
        interceptors=list(interceptors_),
    )
    response.response.close()
    return response.output