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

For schemas made of bool, Literal, and Enum fields, use a decision model instead of generating free-form text. Both extract and extract_async support text and images with these providers:

Model string API key Endpoint
openai-decisions:gpt-6-luna OPENAI_API_KEY OpenAI Decisions
openrouter:typesafe/jev-1.14 OPENROUTER_API_KEY OpenRouter Decisions

No provider extra is needed for either decision endpoint. The explicit openai-decisions: prefix selects the Decisions API; openai: continues to use Responses for general extraction. OpenAI Decisions accepts 1–400 schema fields. PDFs, audio, video, and free-form fields are not supported by decision models.

For example, classify a receipt and check whether it is paid. Jev is also available as openrouter:typesafe/jev-1.14 or openrouter:~typesafe/jev-latest.

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, "openai-decisions:gpt-6-luna", source="./receipt.png")
# Or: 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. Other field types raise a TypeError. Missing, invalid, or refused OpenAI answers raise SchemaValidationError.

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.2.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.2.0
File Size Uploaded Provenance Actions
openextract-1.2.0.tar.gz 210.4 kB With provenance Details for openextract-1.2.0.tar.gz

Built distribution (wheel)

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

Total release size: 221.7 kB

Release files / openextract-1.2.0.tar.gz

Download URL openextract-1.2.0.tar.gz
Size 210.4 kB
Tags Source
SHA-256 checksum
How to use checksums
6c46f0ca88e4b7b88f204f631e26c9fb8e33e1504eb4adea89857ffa389223b3
BLAKE2b-256 checksum
How to use checksums
9e4e3a2031b774833d8287b4b5c7757dafa74f428abcc2bb9a0ed0ae64884f48
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 25, 2026.

Transparency log

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

Download URL openextract-1.2.0-py3-none-any.whl
Size 11.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1890c989b956f40dbab23b786603513af162cd4af29bfd8591ce74f85da9c826
BLAKE2b-256 checksum
How to use checksums
487f8258a60ecac8f59a0b892e6ee99164cffb3a82e32282112c10b25184278b
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 25, 2026.

Transparency log

Security for openextract 1.2.0

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for openextract-1.2.0.tar.gz

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 25, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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