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.1

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.1
File Size Uploaded
openextract-1.1.1.tar.gz 211.4 kB Details

Built distribution (wheel)

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

Total release size: 220.6 kB

Release files / openextract-1.1.1.tar.gz

Download URL openextract-1.1.1.tar.gz
Size 211.4 kB
Tags Source
SHA-256 checksum
How to use checksums
efd769d5428184e2fb50a9280a583f06eeda126e1c0654be2fa459a14787e668
BLAKE2b-256 checksum
How to use checksums
95fb1bc66188bcc21590e5e01a1af9f70454a94d7f3125d60b8d76e268660ae2
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 16, 2026.

Transparency log

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

Download URL openextract-1.1.1-py3-none-any.whl
Size 9.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
685c7bc1e56cf47e2a45ffb1e7849c9f044ec0af37684e597bfe791954de2e8f
BLAKE2b-256 checksum
How to use checksums
d7db2b256650a1d36c35ebcc858198ba3ad061a68000332a4feebfc1a0269429
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 16, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

1.1.1 This release

2 release files

1.1.0

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