Lesson 2 of 5 Preparing Data for Generative AI

Parsing Documents and Extracting Metadata for Retrieval

This lesson explains how document content, including tables, images and layout, is extracted correctly before chunking, and how metadata lets retrieval filter results and enforce boundaries.

Learning outcomes

After completing this lesson, you will be able to:

  • Choose a parsing approach for complex documents.
  • Attach metadata for filtering.
  • Extract metadata automatically during ingestion.

Parsing options

Bedrock Knowledge Bases offers three parsing approaches. The default parser extracts text from supported formats such as PDF, Word, HTML, Markdown and CSV, and is cheapest. Amazon Bedrock Data Automation parses multimodal content, including tables, figures and images, into structured output. Foundation model parsing uses a multimodal FM with a parsing prompt to interpret complex layouts, nested tables and charts, at extra cost per page. Outside knowledge bases, Amazon Textract extracts text, forms and tables from scanned documents with its AnalyzeDocument features (TABLES, FORMS, LAYOUT, QUERIES).

Scenario questions

Choose a parser

Exam skills 1.2.7 · 3.2.7

Choose how a knowledge base should parse each kind of document.

Log in to try this activity Interactive activities are free with an account.

Metadata for filtering

Metadata describes each document: department, product, document type, date, language, access level. In a knowledge base with an S3 data source, metadata is supplied in a companion file named <document>.metadata.json in the same prefix. At query time, metadata filters restrict retrieval, for example product = 'X200' and year >= 2025, and implicit filtering can let a model derive filters from the question.

// s3://kb-docs/manuals/x200-service-guide.pdf.metadata.json
{
  "metadataAttributes": {
    "product": "X200",
    "doc_type": "service_guide",
    "year": 2026,
    "audience": "technician"
  }
}

The following diagram shows how a metadata filter stops a support assistant from answering from the manual of the wrong product.

Fixing answers from the wrong product. Each manual gets a .metadata.json file with product, version and document type, and FM parsing extracts specification tables as structured text. The support portal, which knows the customer's device, passes product = 'X200' as a metadata filter, so look-alike passages from the X100 manual are excluded before ranking.
The scenario is illustrative.
Spot the bug

Metadata that is ignored

Exam skills 1.2.7 · 3.2.7

Find the two lines that stop a knowledge base from using a document's metadata.

Log in to try this activity Interactive activities are free with an account.

Extracting metadata automatically

Documents often arrive without the metadata that retrieval filters need, and it can be extracted automatically during ingestion. Amazon Comprehend detects entities, key phrases, language and custom classes, such as document type. Amazon Textract queries pull specific fields, such as a policy number, from forms. A foundation model can classify each document and summarise it. S3 object tags and folder paths provide the source and owner.

For finer control, a custom chunking Lambda function in a Bedrock knowledge base can attach metadata to each chunk, for example the section heading each chunk came from. The following table summarises the options.

NeedTool
Text from simple digital filesDefault parser
Tables, figures, scanned pages in a knowledge baseBedrock Data Automation or FM parsing
Fields and tables from scanned formsAmazon Textract
Entities, language, document classAmazon Comprehend (built-in or custom classifier)
Filter retrieval by attributes.metadata.json files and metadata filters
Match the pairs

Where metadata comes from

Exam skills 1.2.7

Match each metadata need with an automatic way to extract it.

Log in to try this activity Interactive activities are free with an account.
Worked example

Answers from the wrong product

A support knowledge base covers 40 router models with very similar manuals. Questions about the X200 often return passages from the X100 manual, and specification tables come back as jumbled text.

  • Generate a .metadata.json file for each manual with product, version and document type, using the S3 path and a Comprehend custom classifier.
  • Switch the data source to FM parsing so specification tables are extracted as structured text.
  • Pass product = 'X200' as a metadata filter from the support portal, which already knows the customer's device.

Filtering removes look-alike passages from other products, and better parsing makes table values retrievable. A larger embedding model alone would not have separated near-identical manuals.

Check yourself

Where does a knowledge base read metadata for documents in S3?

Show answer

From a .metadata.json file next to each document.

Exam Tips

Remember for the exam

  • Complex tables and images: FM parsing or Bedrock Data Automation.
  • Scanned forms outside knowledge bases: Textract.
  • Metadata comes from <document>.metadata.json files in S3 data sources.
  • Metadata filters restrict retrieval by product, date or access level.
  • Comprehend can classify documents and extract entities as metadata.