DEV Community

Cover image for Extract and Validate Insurance Claim Packets With Python
Unsiloed AI
Unsiloed AI

Posted on Originally published at unsiloed.ai

Extract and Validate Insurance Claim Packets With Python

By Aman Mishra. Originally published on Unsiloed AI.

An insurance claim rarely arrives as one document. The insurer gets a packet of forms, reports, estimates, and invoices from different organizations, often scanned together into one PDF. An auto claim might hold a body shop's repair estimate, a police report, and a rental car invoice, and a property claim might hold a contractor's estimate, an inspection report, and hotel receipts. Whatever the line of insurance, the documents repeat the same few facts, such as the policy number, the insured car or property, and the date of the loss. When those facts disagree, someone has to notice before the claim is paid, and finding a transposed digit across six pages by eye is slow and easy to miss.

We use the Unsiloed API to label each page, extract the identifiers each document carries, and record the exact spot on the page each value came from. Then we use Python to compare the documents and hand the claims adjuster only the facts that don't agree. The guide below takes one six-page auto claim packet through that process. Every page looks fine on its own, and the packet still has three problems.

Four facts compared across the claim packet, one at a time, using the cited line from each page. The VIN is the same on all four documents. The repair estimate prints policy number PAP-7781-2290 where the others print PAP-7781-2209. The claim form gives the date of loss as 09/14/2026 and the police report gives 09/13/2026. The rental invoice leaves the claim number blank.

What Is in an Insurance Claim Packet

A claim packet is the set of documents an insurer collects to handle one claim. What goes into it depends on the line of insurance. A homeowner's claim usually brings a contractor's repair estimate and an adjuster's inspection report, and a commercial claim might add invoices and repair records. This guide follows an auto claim, whose packet usually includes the following.

Document Who Writes It What It Shows
First notice of loss The insurer, from the policyholder's report The claim number, the policy number, the car, and the date of the accident
Policy declarations The insurer The policy number, the insured car, and what the policy covers
Repair estimate The body shop The parts and labor needed to fix the car
Police report The police department When and where the collision happened, and who was involved
Rental invoice The rental company The replacement car the policyholder used while theirs was repaired

The first notice of loss (the claim form) is the form that opens the claim, usually filled in from the policyholder's first call or online report. The policy declarations page is the summary page of the policy. A VIN, or vehicle identification number, is the 17-character code that identifies one car.

If the body shop and the insurer print different policy numbers, one of them is wrong, and an adjuster needs to know which before paying for repairs.

Insurance policy extraction covers reading one policy in depth, and loss-run extraction covers a policyholder's claim history. This guide is about checking the documents inside one claim against each other.

How Claim Packet Validation Works

The work happens in four steps.

  1. Label each page with its document type.
  2. Group consecutive pages with the same label into one document.
  3. Extract the identifiers each document carries, with a schema written for that document type.
  4. Compare each identifier across the documents that carry it, and flag every disagreement or blank with the page it came from.

One Packet, Six Pages

Marisol Teague's 2023 Kestrel Arden was hit from behind at a red light in Ashford. Her packet holds six pages from four organizations.

Pages Document Issued By
1 First notice of loss Harrowgate Mutual Insurance Company
2 Policy declarations Harrowgate Mutual Insurance Company
3–4 Repair estimate Copperline Collision Center
5 Traffic collision report Ashford Police Department
6 Rental invoice Northway Car Rental

Download the packet PDF and the extracted JSON to follow along.

Page by page, the packet looks clean. Every page that names the policyholder names the same person, every page that lists a car has the same VIN, and the totals on the repair estimate add up. The problems only appear when we see values from different pages next to each other.

Label Each Page and Group It Into Documents

Before we can extract anything, we need to know which pages belong to which document, because a repair estimate and a police report need different schemas. The classification endpoint takes a PDF and a list of category names and returns the best-matching category with a confidence score.

We send one page at a time. The endpoint classifies a multi-page file from its first four pages, so a page-by-page loop is a simple way to label every page in a longer packet. Unsiloed's splitting endpoint can cut a bundle into one PDF per category instead, but we label pages ourselves to keep the packet page numbers that every flag needs to point at. The document classification guide covers classification and splitting in more depth.

The code needs requests and pypdf (pip install requests pypdf) and an Unsiloed API key in the UNSILOED_API_KEY environment variable. The first snippet sets up two helpers. run() submits a PDF to an endpoint and polls until the job finishes or fails. pages() cuts a range of pages out of the packet with pypdf.

import json, os, time, requests
from io import BytesIO
from pypdf import PdfReader, PdfWriter

BASE = "https://prod.visionapi.unsiloed.ai"
HEADERS = {"api-key": os.environ["UNSILOED_API_KEY"]}

def run(endpoint, pdf_bytes, data):
    response = requests.post(f"{BASE}/{endpoint}", headers=HEADERS, data=data,
        files={"pdf_file": ("pages.pdf", pdf_bytes, "application/pdf")})
    response.raise_for_status()
    job = response.json()
    poll = "extract" if endpoint == "v2/extract" else endpoint
    while True:
        result = requests.get(f"{BASE}/{poll}/{job['job_id']}", headers=HEADERS).json()
        if result["status"] == "completed":
            return result["result"]
        if result["status"] == "failed":
            raise RuntimeError(result.get("error"))
        time.sleep(3)

def pages(reader, first, last):
    writer = PdfWriter()
    for n in range(first, last + 1):
        writer.add_page(reader.pages[n - 1])
    out = BytesIO()
    writer.write(out)
    return out.getvalue()
Enter fullscreen mode Exit fullscreen mode

Append the classification loop. Each page gets a label, and a page with the same label as the page before it joins that document, which is how the two estimate pages become one repair estimate.

CATEGORIES = ["first_notice_of_loss", "policy_declarations", "repair_estimate",
              "police_report", "rental_invoice", "other"]

reader = PdfReader("claim-packet.pdf")
docs = []
for n in range(1, len(reader.pages) + 1):
    result = run("classify", pages(reader, n, n),
                 {"categories": json.dumps([{"name": c} for c in CATEGORIES])})
    label = result["classification"]
    print(f"page {n}: {label} ({result['confidence']:.2f})")
    if docs and docs[-1]["type"] == label:
        docs[-1]["last"] = n
    else:
        docs.append({"type": label, "first": n, "last": n})
Enter fullscreen mode Exit fullscreen mode
page 1: first_notice_of_loss (1.00)
page 2: policy_declarations (1.00)
page 3: repair_estimate (1.00)
page 4: repair_estimate (1.00)
page 5: police_report (1.00)
page 6: rental_invoice (1.00)
Enter fullscreen mode Exit fullscreen mode

The other category gives a page with no good match, such as a photo or a cover letter, somewhere to go instead of being forced into the wrong type. Grouping by adjacent label is enough for this packet. A packet with two repair estimates back to back would need a second rule, such as starting a new document whenever the estimate number changes.

Extract the Identifiers Each Document Carries

A schema tells the extraction API which fields to return and what each one means. Each document type gets its own small schema, holding only the identifiers we want to compare. The descriptions ask for identifiers exactly as printed, so a transposed digit stays transposed instead of being tidied into the expected value.

def fields(**descriptions):
    return {"type": "object", "required": list(descriptions), "properties": {
        name: {"type": ["string", "null"], "description": text}
        for name, text in descriptions.items()}}

AS_PRINTED = "exactly as printed, including dashes. Null if blank."

SCHEMAS = {
    "first_notice_of_loss": fields(
        claim_number=f"Claim number {AS_PRINTED}",
        policy_number=f"Policy number {AS_PRINTED}",
        vin=f"Vehicle identification number {AS_PRINTED}",
        loss_date="Date of loss as YYYY-MM-DD"),
    "policy_declarations": fields(
        policy_number=f"Policy number {AS_PRINTED}",
        vin=f"VIN of the insured vehicle {AS_PRINTED}"),
    "repair_estimate": fields(
        claim_number=f"Claim number {AS_PRINTED}",
        policy_number=f"Policy number {AS_PRINTED}",
        vin=f"VIN {AS_PRINTED}"),
    "police_report": fields(
        vin=f"VIN of the insured party's vehicle {AS_PRINTED}",
        loss_date="Date of collision as YYYY-MM-DD"),
    "rental_invoice": fields(
        claim_number=f"Claim number {AS_PRINTED}",
        policy_number=f"Insurance policy reference {AS_PRINTED}"),
}
Enter fullscreen mode Exit fullscreen mode

Each field shares a name across schemas even where the document's label differs. The police report prints "Date of collision" and the rental invoice prints "Insurance policy reference", but both map to the same field as the claim form, which lets the comparison line them up later.

Append the extraction loop. Each document's pages go to the extraction endpoint with its schema. model names the extraction model, and enable_citations asks for the location of every value. A document with no schema, such as an other page, is skipped.

for doc in docs:
    if doc["type"] not in SCHEMAS:
        doc["fields"] = {}
        continue
    doc["fields"] = run("v2/extract", pages(reader, doc["first"], doc["last"]), {
        "schema_data": json.dumps(SCHEMAS[doc["type"]]),
        "model": "gamma",
        "enable_citations": "true",
    })
Enter fullscreen mode Exit fullscreen mode

Each value comes back with two scores and, when the API can locate it, a citation. Here is the policy number from the repair estimate.

{
  "value": "PAP-7781-2290",
  "score": {"grounding_score": 0.98, "extraction_score": 0.99},
  "citation": {"page": 1, "bbox": [221, 154, 304, 168],
               "page_width": 612.0, "page_height": 792.0}
}
Enter fullscreen mode Exit fullscreen mode

The extraction_score is the API's confidence in the value itself, and the grounding_score is its confidence that it found the value at the cited spot. Both run from zero to one. The citation gives the page within the submitted document and a bounding box, the rectangle on the page where the value sits, as [left, top, right, bottom] in PDF points measured from the top-left corner. A blank field comes back with a null value and no citation.

Both scores here are high, and they should be, because the estimate does print PAP-7781-2290. A score measures whether the value was read correctly from the page and says nothing about whether the page itself is right, so the documents still need comparing.

Compare the Documents Field by Field

For each field, we collect the values from every document that carries it. One distinct value is a pass, and two or more values, or a blank, go to review.

Citation pages count from the start of the submitted document, so page 2 of the repair estimate is page 4 of the packet. We add the document's first packet page to every citation, so each finding points at a page the adjuster can open.

from collections import defaultdict

def compare(name):
    values, missing = defaultdict(list), []
    for doc in docs:
        field = doc["fields"].get(name)
        if field is None:
            continue
        if field["value"] is None:
            missing.append(f"{doc['type']} p.{doc['first']}")
            continue
        cite = field["citation"]
        page = doc["first"] + cite["page"] - 1 if cite else doc["first"]
        values[field["value"].replace(" ", "").upper()].append(f"{doc['type']} p.{page}")
    status = "PASS" if len(values) == 1 and not missing else "REVIEW"
    print(f"{name}: {status}")
    for value, where in values.items():
        print(f"  {value:<19} {', '.join(where)}")
    for where in missing:
        print(f"  {'(blank)':<19} {where}")

for name in ["claim_number", "policy_number", "vin", "loss_date"]:
    compare(name)
Enter fullscreen mode Exit fullscreen mode

We remove spaces and uppercase each value before comparing, so pap-7781-2209 and PAP-7781-2209 count as one value. Dashes and digits are left alone, because in an identifier they're the part that matters.

Run against the packet, the comparison prints this.

claim_number: REVIEW
  CLM-26-004417       first_notice_of_loss p.1, repair_estimate p.3
  (blank)             rental_invoice p.6
policy_number: REVIEW
  PAP-7781-2209       first_notice_of_loss p.1, policy_declarations p.2, rental_invoice p.6
  PAP-7781-2290       repair_estimate p.3
vin: PASS
  3KA7B2E44PR417263   first_notice_of_loss p.1, policy_declarations p.2, repair_estimate p.3, police_report p.5
loss_date: REVIEW
  2026-09-14          first_notice_of_loss p.1
  2026-09-13          police_report p.5
Enter fullscreen mode Exit fullscreen mode

The VIN matches on all four documents, so the car is the same car throughout. The other three fields each have a problem.

  • The policy number. The body shop typed 2290 where the policy says 2209. Two swapped digits are the kind of mistake that survives a read-through, because the number looks right at a glance. Three documents agree and one doesn't, but the check doesn't take a vote and lists both values with their pages, because the adjuster needs to confirm which policy the estimate was written against.
  • The date of the accident. The claim form says September 14, and the police report says September 13 at 18:40. A one-day gap can have an ordinary explanation, such as a late-evening collision reported the next day, but the two documents now give different accounts of when the loss happened, so the adjuster has to settle which date is right.
  • The claim number. The rental invoice leaves it blank. Without it, the invoice can't be matched to this claim by claim number, so someone has to confirm it belongs here before it's paid.

Send the Disagreements to an Adjuster

The output above is the adjuster's whole review list. Instead of reading six pages, they open page 3 for the policy number, pages 1 and 5 for the date, and page 6 for the claim number. Each value carries a bounding box, so a review screen can draw a highlight on the exact figure in question. Working out why the documents disagree, and whether it matters for the claim, stays with the adjuster.

Values with low scores belong on the same list, and any value below your cutoff should go to a person before it's compared, because a misread value would show up as a false disagreement or hide a real one. The confidence score thresholds guide covers choosing a cutoff for each field.

The same pattern works for other lines of insurance and other checks. A property claim can compare the policy number, the property address, and the date of loss across the declarations page, the contractor's estimate, and the inspection report. An auto claim can check the estimate's labor rates against your shop agreements or the rental days against the policy's rental limit. Label the pages, extract the few fields the check needs, compare them across documents, and keep the citations so every flag points back to the page that raised it.

Top comments (0)