Skip to main content

Extract structured data from documents, images, audio, and video using LLMs

Project description

openextract

Extract structured data from documents, images, audio, and video using LLMs.

PyPI version Python versions License: MIT CI Coverage Ruff Downloads

Documentation · PyPI · Changelog · Issues


openextract turns any document, image, audio, or video file into a typed Pydantic model in a single function call. Point it at a local path or a URL, pass a schema, and get back a validated object you can use directly in your code.

Features

  • Type-safe output. Define your shape with Pydantic; get back a validated instance.
  • One function, many modalities. Documents (PDF, DOCX), images, audio, and video.
  • Local files or URLs. Pass a path or an https:// URL — openextract handles fetching.
  • Bring your own model. OpenAI, Google, and Ollama supported out of the box via pydantic-ai.
  • Explicit error handling. Distinct exceptions for URL fetch, schema validation, and model errors.
  • 100% test coverage, enforced in CI.

Installation

uv add openextract

Or with pip:

pip install openextract

Requires Python 3.12+.

Quick start

from pydantic import BaseModel
from openextract import extract


class PdfInfo(BaseModel):
    summary: str
    language: str


result = extract(
    schema=PdfInfo,
    model="openai:gpt-5",
    input_file="https://example.com/document.pdf",
    instructions="Return a two-sentence summary and the document's primary language.",
)

print(result.summary)
print(result.language)

result is a fully-validated PdfInfo instance — not a dict, not a string.

Usage

Local files

result = extract(
    schema=PdfInfo,
    model="openai:gpt-5",
    input_file="./reports/q4.pdf",
)

Bytes or file-like objects

result = extract(schema=PdfInfo, model="openai:gpt-5", input_file=pdf_bytes, media_type="application/pdf")
# A file-like object with .read() works too; pass media_type explicitly:
result = extract(schema=PdfInfo, model="openai:gpt-5", input_file=open("q4.pdf", "rb"), media_type="application/pdf")

Retry on transient model errors

result = extract(
    schema=PdfInfo,
    model="openai:gpt-5",
    input_file="./reports/q4.pdf",
    max_retries=3,
)

max_retries defaults to 0 (single attempt). When set, extract retries only on ModelError and sleeps retry_backoff * (2 ** attempt) seconds (with up to 25% jitter) between attempts. retry_backoff defaults to 1.0 second.

Inspecting token usage

Use extract_with_usage when you want token counts alongside the extracted output (for cost tracking, logging, etc.).

from openextract import extract_with_usage

result, usage = extract_with_usage(
    schema=PdfInfo,
    model="openai:gpt-5",
    input_file="./reports/q4.pdf",
)

print(result.summary)
print(f"tokens: {usage.input_tokens} in / {usage.output_tokens} out / {usage.total_tokens} total")

usage is a frozen Usage dataclass with input_tokens, output_tokens, and total_tokens fields.

Choosing a model

model follows the pydantic-ai provider prefix convention:

Provider Example identifier
OpenAI openai:gpt-5
Google google-gla:gemini-2.5-pro
Ollama ollama:llama3

Set the corresponding provider credentials in your environment (e.g. OPENAI_API_KEY). openextract loads .env automatically.

Command line

openextract ships with a CLI for one-shot extractions from the shell.

openextract ./reports/q4.pdf \
  --schema mypkg.schemas:Invoice \
  --model openai:gpt-5 \
  --instructions "Pull totals and line items." \
  --output json
  • <input_file> is a positional argument; a local path or https:// URL.
  • --schema is a Python import path of the form module:ClassName resolving to a Pydantic model.
  • --model is a pydantic-ai model identifier.
  • --instructions is optional natural-language guidance.
  • --output is json (default; prints model_dump_json(indent=2)) or repr.

Exit codes: 0 success, 2 URL fetch error, 3 schema validation error, 4 model error, 5 other extraction error, 1 any other failure (including bad --schema paths).

Examples

Runnable scripts live in the examples/ directory. Each one takes the input path as the first argument and prints a JSON dump of the validated result:

Script What it does
invoice_extraction.py PDF invoice -> structured line items
receipt_extraction.py receipt image -> merchant, items, totals
meeting_notes.py audio -> summary, decisions, action items

Run any example with uv once your provider credentials (e.g. OPENAI_API_KEY) are set:

uv run python examples/invoice_extraction.py ./invoices/q4.pdf

See the examples/ directory for the full source.

Error handling

from openextract import (
    extract,
    UrlFetchError,
    SchemaValidationError,
    ModelError,
    ExtractionError,
)

try:
    result = extract(schema=PdfInfo, model="openai:gpt-5", input_file=url)
except UrlFetchError:
    ...  # The URL could not be fetched
except SchemaValidationError:
    ...  # The model's output did not match your schema
except ModelError:
    ...  # The model provider returned an error
except ExtractionError:
    ...  # Any other extraction failure (base class)

All openextract exceptions inherit from ExtractionError, so you can catch it as a single fallback if you prefer.

API reference

extract(schema, model, input_file, instructions=None, *, media_type=None, max_retries=0, retry_backoff=1.0)

Argument Type Description
schema type[BaseModel] A Pydantic model class describing the desired output shape.
model str A pydantic-ai model identifier (e.g. "openai:gpt-5").
input_file str | bytes | BinaryIO A local file path, an https:// URL, raw bytes, or a binary file-like object with a .read() method.
instructions str | None Optional natural-language guidance for the model.
media_type str | None (keyword-only) MIME type. Required for bytes and file-like inputs; overrides the guessed type for str inputs when provided.
max_retries int (keyword-only) Extra attempts after a ModelError. Defaults to 0 (no retry).
retry_backoff float (keyword-only) Base seconds for exponential backoff with jitter between retries.

Returns an instance of schema.

Development

git clone https://github.com/Mellow-Artificial-Intelligence/openextract.git
cd openextract
uv sync --dev

uv run pytest --cov=openextract            # tests + coverage
uv run ruff check .                        # lint
uv run ruff format --check .               # format check

CI runs the test suite on every PR and fails if total coverage drops below 100%.

See CONTRIBUTING.md for the full contributor guide.

License

MIT © Cole McIntosh

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

openextract-0.5.0.tar.gz (98.4 kB view details)

Uploaded Source

Built Distribution

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

openextract-0.5.0-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

Details for the file openextract-0.5.0.tar.gz.

File metadata

  • Download URL: openextract-0.5.0.tar.gz
  • Upload date:
  • Size: 98.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for openextract-0.5.0.tar.gz
Algorithm Hash digest
SHA256 3e1be1a2d024d43be57ab386d2e7e1032ae3168aaf93114b30444c4364e49dff
MD5 78b6c14e6122472a9c14104f0842f47b
BLAKE2b-256 c2207281d1ebd340450abda85dca980647ae6b63973da9241f97c22ceb89d6d7

See more details on using hashes here.

Provenance

The following attestation bundles were made for openextract-0.5.0.tar.gz:

Publisher: release.yml on Mellow-Artificial-Intelligence/openextract

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

File details

Details for the file openextract-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: openextract-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 11.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for openextract-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e6f417a1d2d464b87f027c252905760476d575b84946c4a4bc919b7367132bd8
MD5 2a5280458ad1e89e0a93f3f07521dd11
BLAKE2b-256 8d47d7c8d6865caa3f481a35d057e18e1163c3e3e429eaaf66eb1e6ba691e36f

See more details on using hashes here.

Provenance

The following attestation bundles were made for openextract-0.5.0-py3-none-any.whl:

Publisher: release.yml on Mellow-Artificial-Intelligence/openextract

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