AWS Builder Center

From Messy PDFs to Clean JSON: Structured Extraction with Amazon Bedrock

Turning PDF reports into reliable structured data is harder than it looks: every template is different, and "just return JSON" breaks in production. This article shows a practical pattern with the Amazon Bedrock Converse API: define your target record as a JSON Schema, expose it as a tool, force the model to call it, validate the output, and allow one repair round. Includes full Python code with boto3, plus lessons learned from running document extraction at scale.

Etudiant

The problem: every report looks different

I work as an AI engineer on generative AI agents used by thousands of people every day. One recurring job is turning PDF reports into structured data that a downstream system can actually use.
The catch: the reports come from many authors and many templates. The same field can sit in a table on page 2 of one report and in a paragraph on page 7 of another. Rule-based parsers break every time a template changes.
Large language models handle this variety well. But getting reliable, schema-valid output from them at scale takes more than a good prompt. This article shows the pattern I use with Amazon Bedrock and what I learned running it.

Why "just return JSON" is not enough

The first version most of us write is a prompt ending in "Respond only with JSON." It works in a demo. In production it fails in small, annoying ways:
  • The model wraps the JSON in markdown fences or adds a friendly sentence before it.
  • Field names drift:  inspection_date  one day,  date_of_inspection  the next.
  • Missing values get invented instead of left empty.
  • Long documents produce truncated, unparseable output.
Each failure is rare on its own. Across thousands of documents, they add up to a pipeline you cannot trust.

The approach: use tool use as a schema

The trick is to stop asking the model for JSON and instead give it a tool to call. With the Bedrock Converse API, you define a tool whose input schema is exactly the record you want. Then you force the model to call it.
The model never "writes" JSON as free text. It fills in the arguments of a function, and Bedrock returns them as a parsed object. The pipeline looks like this:
  1. Extract the text (and optionally page images) from the PDF.
  1. Send it to a model on Bedrock with one tool,  save_report , whose input schema is your target record.
  1. Force that tool with  toolChoice , so the model must answer through it.
  1. Validate the returned object against the same schema in your code.
  1. On a validation error, send the error back to the model once and ask it to fix the record.
Because the Converse API is the same across model providers on Bedrock, you can swap models without rewriting this logic.

Code walkthrough

The example below extracts a simplified inspection report. You need  boto3 ,  pypdf  and  jsonschema , plus Bedrock model access enabled in your account and region.
First, define the record you want as a JSON Schema. This one schema drives both the model and your validation.
REPORT_SCHEMA = {
"type": "object",
"properties": {
"report_id": {"type": ["string", "null"]},
"inspection_date": {"type": ["string", "null"], "description": "ISO date, YYYY-MM-DD"},
"site_address": {"type": ["string", "null"]},
"inspector_company": {"type": ["string", "null"]},
"findings": {
"type": "array",
"items": {
"type": "object",
"properties": {
"description": {"type": "string"},
"severity": {"type": "string", "enum": ["low", "medium", "high"]},
"page": {"type": "integer"}
},
"required": ["description", "severity", "page"]
}
}
},
"required": ["report_id", "inspection_date", "site_address", "inspector_company", "findings"]
}
Note the  ["string", "null"]  types. They give the model an honest way to say "not in the document" instead of guessing.
Next, read the PDF and call Bedrock with the forced tool:
import boto3
from pypdf import PdfReader
from jsonschema import validate, ValidationError

MODEL_ID = "<a model ID enabled in your Bedrock console>"
bedrock = boto3.client("bedrock-runtime")

TOOL_CONFIG = {
"tools": [{
"toolSpec": {
"name": "save_report",
"description": "Save the structured data extracted from an inspection report.",
"inputSchema": {"json": REPORT_SCHEMA},
}
}],
"toolChoice": {"tool": {"name": "save_report"}},
}

SYSTEM = [{"text": (
"You extract data from inspection reports. "
"Use only information present in the document. "
"If a field is missing, set it to null. Never guess."
)}]

def pdf_to_text(path):
reader = PdfReader(path)
return "\n\n".join(
f"--- Page {i + 1} ---\n{page.extract_text() or ''}"
for i, page in enumerate(reader.pages)
)

def get_tool_input(response):
for block in response["output"]["message"]["content"]:
if "toolUse" in block:
return block["toolUse"]
raise RuntimeError("Model did not call the tool")
The page markers matter. They let the model fill the  page  field, which makes every finding traceable back to the source.
Finally, validate and allow one repair round:
def extract_report(path):
messages = [{
"role": "user",
"content": [{"text": f"Extract the report data.\n\n{pdf_to_text(path)}"}],
}]

for attempt in range(2):
response = bedrock.converse(
modelId=MODEL_ID,
system=SYSTEM,
messages=messages,
toolConfig=TOOL_CONFIG,
inferenceConfig={"temperature": 0},
)
tool_use = get_tool_input(response)
try:
validate(instance=tool_use["input"], schema=REPORT_SCHEMA)
return tool_use["input"]
except ValidationError as err:
# Send the error back so the model can repair its own output
messages.append(response["output"]["message"])
messages.append({
"role": "user",
"content": [{
"toolResult": {
"toolUseId": tool_use["toolUseId"],
"content": [{"text": f"Validation failed: {err.message}. Call save_report again with a corrected record."}],
"status": "error",
}
}],
})

raise RuntimeError(f"Could not extract a valid record from {path}")

if __name__ == "__main__":
import json
print(json.dumps(extract_report("sample_report.pdf"), indent=2))
The repair round is cheap and catches most of the remaining errors, such as a severity of  "critical"  when the enum only allows three values.

Lessons from running this in production

  • Nullable beats required. Forcing every field to have a value is the fastest way to get hallucinated data. Let the model say "not found" and track null rates per field instead.
  • Ask for evidence. A  page  number (or a short source quote) on each extracted item makes reviews much faster. Reviewers check the source instead of rereading the whole report.
  • Build a small gold set early. Twenty to fifty hand-labelled reports, covering each template you see, are enough to compare prompts and models objectively. Measure per-field accuracy, not one global score.
  • Hierarchy is the hard part. Flat fields are easy. Nested structures, like findings grouped under sections and sub-sections, are where models slip. Describe the nesting explicitly in the schema descriptions.
  • Scanned PDFs need a different path. When  extract_text()  returns almost nothing, route the pages as images to a multimodal model, or run Amazon Textract first.
  • Watch long documents. Very long reports can exceed comfortable context sizes. Split by section, extract per chunk, then merge, rather than sending 200 pages at once.
  • Log the inputs. Keep the model ID, prompt version and schema version with every extracted record. When accuracy changes, you will want to know why.

Wrapping up

Forcing a tool call turns an LLM from a text generator into a structured extractor. Add schema validation and one repair round, and you get output a downstream system can trust.
The same pattern works beyond inspection reports: invoices, contracts, CVs, technical datasheets. Anywhere the layout varies but the target record does not.
How do you handle structured extraction in your projects? Do you use tool use, native structured outputs, or a different approach entirely? Let me know in the comments.
Tags: Amazon Bedrock, Generative AI, Python, LLM, Document Processing
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article