Skip to main content

openextract

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

PyPI version Python versions License: MIT CI

Website · PyPI · Changelog

One function: give it a Pydantic schema, a model, and a file. You get back a validated instance.

from pydantic import BaseModel
from openextract import extract


class Invoice(BaseModel):
    vendor: str
    total: float
    currency: str


invoice = extract(Invoice, "openai:gpt-5", source="./invoice.pdf")  # path to a file on disk
print(invoice.total)

Install

pip install 'openextract[all]'   # or just your provider: [openai], [anthropic], [google], ...

Requires Python 3.12+. Set the API key your provider expects, such as OPENAI_API_KEY.

Usage

extract(schema, model, source, instructions=None, *, media_type=None)
await extract_async(schema, model, source, instructions=None, *, media_type=None)
  • source is where the file is: a path on disk, a pathlib.Path, or an http(s):// URL. openextract opens it for you. You can also pass the file's raw bytes. A string is always treated as a path or URL, never as the document's text; to extract from text you already have, pass text.encode().
  • model is any pydantic-ai model string ("anthropic:claude-sonnet-5", "google-gla:gemini-2.5-pro", "ollama:llama3", ...) or a configured pydantic-ai Model.
  • instructions are optional extra guidance for the model.
  • media_type is inferred for you. openextract checks the file extension, then the URL's Content-Type, then the file's leading bytes (PDF, PNG, JPEG, GIF, WebP, MP3, WAV, FLAC, OGG, MP4, UTF-8 text). Pass it only to override the result.
from pathlib import Path

extract(Invoice, "openai:gpt-5", source="./invoices/2291.pdf")                    # file path
extract(Invoice, "openai:gpt-5", source=Path("invoices") / "2291.pdf")            # pathlib.Path
extract(Invoice, "anthropic:claude-sonnet-5", source="https://example.com/2291.pdf")  # URL
extract(Invoice, "openai:gpt-5", source=pdf_bytes, instructions="Amounts are in EUR.")  # bytes

Decision models

Jev, TypeSafe's decision model on OpenRouter, answers typed questions instead of writing text. Pass openrouter:typesafe/jev-1.14 (or openrouter:~typesafe/jev-latest) to fill a schema whose fields are all bool, Literal, or Enum:

from typing import Literal

from pydantic import BaseModel, Field


class Triage(BaseModel):
    document_type: Literal["invoice", "receipt", "contract"] = Field(
        description="What kind of document is this?"
    )
    is_paid: bool = Field(description="Has the amount been paid in full?")


triage = extract(Triage, "openrouter:typesafe/jev-1.14", source="./receipt.png")

Each field becomes one question, phrased by its description. A bool field is True when the probability of yes is at least 0.5. Set OPENROUTER_API_KEY; no extra is needed. Decision models read text and images only, and any other field type raises a TypeError.

Errors

Every error subclasses ExtractionError and keeps the original exception as __cause__.

Exception Raised when
ModelError The provider returns an error.
SchemaValidationError The model's output doesn't match the schema.
ProviderNotInstalledError The provider's SDK isn't installed.
ExtractionError A URL can't be fetched.

If openextract can't infer the media type, it raises a ValueError and asks you to pass media_type.

License

MIT

Metadata

Release files for openextract 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for openextract 1.1.0
File Size Uploaded
openextract-1.1.0.tar.gz 171.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for openextract 1.1.0
File Interpreter ABI Platform
openextract-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 180.4 kB

Release files / openextract-1.1.0.tar.gz

Download URL openextract-1.1.0.tar.gz
Size 171.2 kB
Tags Source
SHA-256 checksum
How to use checksums
1e70abc9015dc7ea635f3f29336c15266a65a1910cfeabff37daa586223e3d15
BLAKE2b-256 checksum
How to use checksums
4bed0dff4279d6d5a96eb3ca7efdb62b706edabb23496f452efed22962db66ce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 15, 2026.

Transparency log

Release files / openextract-1.1.0-py3-none-any.whl

Download URL openextract-1.1.0-py3-none-any.whl
Size 9.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
17e4e9ec6ad70cb85ee3d4f6e05512bd77cb00510b8dcf85dc167dd18a928cb8
BLAKE2b-256 checksum
How to use checksums
d0803632266c7e04c0b95f05902dfdc698e00718aa53e6e8af2c70e45bd48206
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 15, 2026.

Transparency log

Release history Release notifications | RSS feed

1.1.1

2 release files

This release

1.1.0 This release

2 release files

1.0.0

2 release files

0.15.0

2 release files

0.14.0

2 release files

0.13.1

2 release files

0.11.0

2 release files

0.9.0

2 release files

0.8.0

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page