Quick start

Give a PDF and a schema; get back JSON that fits it.

1. Get an API key

Create a key in the PageIndex Console and keep it in an environment variable. The same key works for PageIndex Agent and Page Search.

export PAGEINDEX_API_KEY="your-pageindex-key"

Install the Python SDK (source):

pip install -U pageindex

2. Extract

Describe each field you want, then extract. The SDK sends the file, starts the extraction and waits for it; most documents take a minute or less. The file is read for this extraction only and is not kept as a document. To extract from a document you have uploaded, pass its id in place of the path.

from pageindex import PageIndexClient

client = PageIndexClient(index="cloud")  # reads PAGEINDEX_API_KEY

schema = {
    "type": "object",
    "properties": {
        "company": {"type": "string", "description": "The company's name, as printed on the cover"},
        "segments": {
            "type": "array",
            "description": "Each business segment in the segment results table",
            "items": {
                "type": "object",
                "properties": {
                    "name": {"type": "string"},
                    "revenue_usd_millions": {"type": ["number", "null"], "description": "Revenue, in millions of dollars"},
                },
            },
        },
    },
}

job = client.extract("report.pdf", schema)  # sends the file, waits for the result
print(job["extract_result"])

Result:

{
  "company": "Acme Industries plc",
  "segments": [
    {"name": "Industrial", "revenue_usd_millions": 4120.5},
    {"name": "Consumer", "revenue_usd_millions": 2876.0},
    {"name": "Services", "revenue_usd_millions": null}
  ]
}

The result fits the schema. A value the document does not give is null; every row of a list comes back, in the order printed, however many there are.

3. Use a Pydantic model

The schema can be a Pydantic model, and the result parsed back into it.

from pydantic import BaseModel, Field

class Segment(BaseModel):
    name: str
    revenue_usd_millions: float | None = Field(description="Revenue, in millions of dollars")

class Report(BaseModel):
    company: str = Field(description="The company's name, as printed on the cover")
    segments: list[Segment] = Field(description="Each business segment in the segment results table")

job = client.extract("report.pdf", Report)
report = Report.model_validate(job["extract_result"])   # a Report
print(report.segments[0].name, job["usage"]["pages"], job["usage"]["credits"])

Next

  • Writing a schema: descriptions are the instructions.
  • Options and jobs: the pro tier, some pages only, the page each row came from, and coming back for a result later.