Skip to main content

Haystack integration for AWS Textract document text extraction and analysis

Project description

amazon-textract-haystack

PyPI - Version PyPI - Python Version


Overview

A Haystack integration for AWS Textract that extracts text and structured data from documents using OCR.

The AmazonTextractConverter component converts images and single-page PDFs into Haystack Document objects using the AWS Textract synchronous API.

Supported file formats: JPEG, PNG, TIFF, BMP, and single-page PDF (up to 10 MB).

Installation

pip install amazon-textract-haystack

Usage

Basic text extraction

Extract plain text from a document using DetectDocumentText:

from haystack_integrations.components.converters.amazon_textract import AmazonTextractConverter

converter = AmazonTextractConverter()
results = converter.run(sources=["document.png"])
documents = results["documents"]

print(documents[0].content)

Table and form analysis

Use AnalyzeDocument to detect tables and forms by setting feature_types:

converter = AmazonTextractConverter(feature_types=["TABLES", "FORMS"])
results = converter.run(sources=["invoice.png"])

documents = results["documents"]
raw_responses = results["raw_textract_response"]

Valid feature_types values: "TABLES", "FORMS", "SIGNATURES", "LAYOUT".

Natural-language queries

Ask questions about a document and get extracted answers. The QUERIES feature type is enabled automatically when you pass the queries parameter at runtime:

converter = AmazonTextractConverter()
results = converter.run(
    sources=["medical_form.png"],
    queries=["What is the patient name?", "What is the date of birth?"],
)

documents = results["documents"]
raw_responses = results["raw_textract_response"]

Queries can be combined with feature_types for both structural and question-based extraction:

converter = AmazonTextractConverter(feature_types=["TABLES", "FORMS"])
results = converter.run(
    sources=["invoice.png"],
    queries=["What is the total amount due?"],
)

In a Haystack pipeline

from haystack import Pipeline
from haystack.components.preprocessors import DocumentCleaner
from haystack_integrations.components.converters.amazon_textract import AmazonTextractConverter

pipeline = Pipeline()
pipeline.add_component("converter", AmazonTextractConverter())
pipeline.add_component("cleaner", DocumentCleaner())
pipeline.connect("converter.documents", "cleaner.documents")

result = pipeline.run({"converter": {"sources": ["scan.png"]}})

AWS Credentials

The component uses the standard boto3 credential chain. You can configure credentials in any of these ways:

  1. Environment variables (default): Set AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_DEFAULT_REGION.
  2. AWS credentials file: Configure via ~/.aws/credentials and ~/.aws/config.
  3. IAM role: When running on AWS infrastructure (EC2, Lambda, ECS).
  4. Explicit parameters:
from haystack.utils import Secret

converter = AmazonTextractConverter(
    aws_access_key_id=Secret.from_env_var("MY_AWS_KEY"),
    aws_secret_access_key=Secret.from_env_var("MY_AWS_SECRET"),
    aws_region_name=Secret.from_token("us-east-1"),
)

Running Tests

Unit tests (no AWS credentials needed):

cd integrations/amazon_textract
hatch run test:unit

Integration tests (require AWS credentials and a test image at tests/test_files/sample_text.png):

export AWS_ACCESS_KEY_ID=...
export AWS_SECRET_ACCESS_KEY=...
export AWS_DEFAULT_REGION=us-east-1
hatch run test:integration

Contributing

Refer to the general Contribution Guidelines.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

amazon_textract_haystack-1.0.0.tar.gz (37.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

amazon_textract_haystack-1.0.0-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file amazon_textract_haystack-1.0.0.tar.gz.

File metadata

  • Download URL: amazon_textract_haystack-1.0.0.tar.gz
  • Upload date:
  • Size: 37.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for amazon_textract_haystack-1.0.0.tar.gz
Algorithm Hash digest
SHA256 8fea83f38c71b7d391c5a1a806041adbfb1958d3d57fbcc75f9099ff29842b0a
MD5 35f6c274363e46f7d22cf079e496625f
BLAKE2b-256 2478ba6dcf2e79cbb1392f5a2922593864009f3b223f5dfcc141c160e312408d

See more details on using hashes here.

Provenance

The following attestation bundles were made for amazon_textract_haystack-1.0.0.tar.gz:

Publisher: CI_pypi_release.yml on deepset-ai/haystack-core-integrations

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file amazon_textract_haystack-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for amazon_textract_haystack-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4e05b20d2021ad5ad1801f7ff7f4048b086bc6e39d55eb5318737ad2ef9dfb40
MD5 b41b3167e2b395e77b6fbd74f7202ce7
BLAKE2b-256 e119becb95335694fdd4fee40ed73210bcc90f44385c6862edcba91ea99ebb56

See more details on using hashes here.

Provenance

The following attestation bundles were made for amazon_textract_haystack-1.0.0-py3-none-any.whl:

Publisher: CI_pypi_release.yml on deepset-ai/haystack-core-integrations

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page