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 · Roadmap

One function: give it a Pydantic schema, a model, and a source. You get back a validated instance. Supported input formats depend on the chosen model.

First extraction

Requires Python 3.12+. All model requests use HTTPX directly. Pydantic is used for schemas and validation; Pydantic AI and provider SDKs are not dependencies. No provider extras are needed:

pip install openextract

Set OPENAI_API_KEY to your OpenAI API key in the same terminal. Your account needs access to gpt-5; running the example makes a paid provider request.

export OPENAI_API_KEY='your-api-key'  # macOS / Linux

On PowerShell, use $env:OPENAI_API_KEY = 'your-api-key' instead.

Save the following as first_extraction.py. The fictional invoice is included as plain text, so there is no document to download or missing file to create.

from decimal import Decimal

from pydantic import BaseModel, Field

from openextract import extract


class Invoice(BaseModel):
    vendor: str = Field(description="Seller, not buyer.")
    total: Decimal = Field(description="Final amount due, including tax.")
    currency: str | None = Field(description="Three-letter currency code, or null if unclear.")


source = """INVOICE DEMO-001 (fictional sample)
Seller: Acme Paper Co.
Bill to: Northwind Studio
Paper supplies: 100.00 USD
Subtotal: 100.00 USD
Tax: 20.00 USD
Total due: 120.00 USD
"""

invoice = extract(Invoice, "openai:gpt-5", source)
print(invoice.vendor)  # str: Acme Paper Co.
print(invoice.total)  # Decimal: 120.00
print(invoice.currency)  # str | None: USD
python first_extraction.py

Expected fields for this fixture:

Field Python type Expected value
vendor str "Acme Paper Co." (seller, not Northwind Studio)
total Decimal Decimal("120.00") (final amount including tax, not the subtotal)
currency str | None "USD" (or None when the source is unclear)

Schema validation checks structure and types; it does not guarantee factual accuracy. Check extracted values against your source before relying on them.

The same non-sensitive invoice is supplied in examples/fixtures/invoice.txt. In a repository checkout, after setting the key, run uv run python examples/basic.py; it finds the fixture automatically. An optional path or URL argument lets you use your own document. See example checks for offline validation and the live-check record.

Continue with fine-detail controls: field descriptions, Decimal totals, nullable currency, validators, interpretation instructions, and media-type overrides, using the same Invoice result type.

Usage

extract(schema, model, source, instructions=None, *, media_type=None, client=None)
await extract_async(schema, model, source, instructions=None, *, media_type=None, client=None)
  • source accepts plain text, a pathlib.Path (or another os.PathLike) for a local file, an http(s):// URL, or raw bytes. Strings are text unless they start with http:// or https://. Text is encoded as UTF-8 automatically; use Path("invoice.pdf") to read a local file.
  • model is a supported "provider:model" string ("anthropic:claude-sonnet-5", "google-gla:gemini-2.5-pro", "ollama:llama3", ...) or an openextract.Model configuration.
  • instructions are optional extra guidance for the model.
  • media_type defaults to text/plain for plain strings. For files, URLs, and bytes, openextract checks the file extension, then the URL's Content-Type, then the leading bytes (PDF, PNG, JPEG, GIF, WebP, MP3, WAV, FLAC, OGG, MP4, UTF-8 text). Pass it to override the result.
  • client is an optional httpx.Client for extract, or httpx.AsyncClient for extract_async. It is used only for model requests and remains open. Without it, openextract creates and closes a client for each extraction. Source URLs are fetched separately without model credentials.

Media detection does not mean every model accepts every format. Choose a model that supports your input and schema.

from pathlib import Path

extract(Invoice, "openai:gpt-5", source="Total due: 120.00")  # plain text
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

For provider generation settings, see configured models for a complete example, sync/async checks, and provider compatibility limits.

Providers

Adapters encode requests for these APIs. The selected model must support the input type and structured output or tool calling used by its adapter.

Prefix HTTP API API key environment variable Inputs encoded by the adapter
openai, openai-responses Responses OPENAI_API_KEY Text, images, PDF, Word, RTF, ODT, PowerPoint, Excel
openai-chat Chat Completions, tool or JSON Schema output OPENAI_API_KEY Text, images, PDF, MP3, WAV
openrouter Chat Completions, tool or JSON Schema output OPENROUTER_API_KEY Chat content supported by the endpoint and model
groq, mistral, xai, cerebras, huggingface Chat Completions, tool or JSON Schema output GROQ_API_KEY, MISTRAL_API_KEY, XAI_API_KEY, CEREBRAS_API_KEY, HF_TOKEN, respectively Chat content supported by the endpoint and model
anthropic Messages with a forced extraction tool ANTHROPIC_API_KEY Text, PNG, JPEG, GIF, WebP, PDF
google, google-gla, gemini Gemini generateContent with JSON Schema GOOGLE_API_KEY or GEMINI_API_KEY Text, images, PDF, audio, video
ollama Local /api/chat with JSON Schema None Text, images
cohere V2 Chat with JSON Schema CO_API_KEY Text, images
bedrock Converse with a forced extraction tool AWS_BEARER_TOKEN_BEDROCK Text, PNG, JPEG, GIF, WebP, PDF

The Chat Completions adapter encodes text, images, PDF, MP3, and WAV; compatible services do not necessarily accept every format. Unsupported adapter input types raise ValueError before a model request. Model-specific restrictions are reported by the provider as ModelError. Recognizing a file type does not imply every provider can process it. For example, Responses does not accept audio or video; use Gemini for those inputs, or an audio-capable Chat Completions model for MP3/WAV.

Responses and Chat Completions in JSON Schema mode use strict JSON Schema: all declared fields, including fields with defaults, are requested, and fixed objects disallow additional properties. Open-ended dictionaries are rejected; select Model("openai:gpt-5", api="chat") for tool-based output instead. Other schema restrictions depend on the provider. The original Pydantic model always performs the final validation, including custom validators and constraints. Tagged unions are adapted to the provider's strict schema format. General adapters use JSON validation consistently, so strict date and Decimal fields also work with decoded tool inputs.

Bedrock requires AWS_REGION (or AWS_DEFAULT_REGION) unless base_url is provided. Authentication uses a Bedrock bearer token. AWS credential discovery, profiles, role assumption, and automatic SigV4 signing are not implemented. You can supply your own authenticated HTTPX client with Model(..., api_key="") to disable environment-key lookup and the key requirement. Local Ollama supports structured output; Ollama Cloud currently does not.

Custom endpoints and settings

Use Model to configure an existing provider or any endpoint implementing a supported API. Custom provider names require both api and base_url.

import os

import httpx
from openextract import Model, extract

model = Model(
    "custom:my-model",
    api="chat",
    base_url="https://models.example.com/v1",
    api_key=os.environ["CUSTOM_API_KEY"],
    headers={"X-Tenant": "accounts"},
    settings={"temperature": 0},
    timeout=60,
    max_retries=2,
    validation_retries=1,
)

with httpx.Client() as client:
    invoice = extract(Invoice, model, "./invoice.pdf", client=client)

api selects responses, chat, anthropic, gemini, ollama, cohere, bedrock, openai-decisions, or openrouter-decisions. base_url includes the API version, but not the operation path. For Decisions, use a base ending in /v1 for OpenAI or /api/alpha for OpenRouter. Credentials go in api_key or headers, not the URL. Custom headers take precedence over generated headers. For keyless custom services, omit api_key.

Explicit configuration takes precedence over environment variables. Built-in providers also read their base URL override: OPENAI_BASE_URL, ANTHROPIC_BASE_URL, GOOGLE_BASE_URL, OLLAMA_BASE_URL, OPENROUTER_BASE_URL, GROQ_BASE_URL, MISTRAL_BASE_URL, XAI_BASE_URL, CEREBRAS_BASE_URL, HF_BASE_URL, COHERE_BASE_URL, or BEDROCK_BASE_URL. For automatic OpenRouter Decisions routing, OPENROUTER_BASE_URL is the regular base ending in /api/v1; openextract replaces /v1 with /alpha.

Chat Completions defaults to tool calling. For compatible endpoints, select output_mode="json_schema" to request JSON text using response_format, with the schema generated from your Pydantic model. For example, use Gemini through OpenRouter structured outputs:

model = Model(
    "openrouter:google/gemini-2.5-flash",
    output_mode="json_schema",
    settings={"provider": {"require_parameters": True}},
)
invoice = extract(Invoice, model, "./invoice.pdf")

Set OPENROUTER_API_KEY in the environment. require_parameters restricts OpenRouter routing to endpoints supporting the requested parameters. In this mode, openextract sends the JSON Schema instead of extraction tools, reads JSON from the response's message content, and validates it with the original Pydantic model. output_mode="tool" explicitly selects tool calling. This option is only configurable for Chat Completions; other adapters retain their own output formats. The model and endpoint must support the selected mode.

settings uses the provider's native parameter names. Gemini settings go inside generationConfig; Ollama sampling options go inside settings["options"]; Bedrock inference options go inside settings["inferenceConfig"]. Anthropic's default max_tokens is 4096 and can be overridden. Request structure and output schema fields cannot be replaced. Decisions adapters do not accept settings. Extraction does not stream responses.

By default, transport failures and HTTP 408, 409, 429, 500, 502, 503, and 504 are retried twice, using exponential backoff or Retry-After capped at 30 seconds. Other HTTP errors, redirects, and provider-reported generation failures fail immediately. General extraction retries a Pydantic validation failure once with feedback that omits raw input values. Refusals, incomplete generations, malformed response envelopes, and invalid Decisions answers fail immediately. The HTTP retry budget applies to each validation attempt. Set both retry counts to zero for a single model request.

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 pathlib import Path
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=Path("receipt.png"))
# Or: extract(Triage, "openrouter:typesafe/jev-1.14", source=Path("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

Operational errors subclass ExtractionError. The original exception is chained as __cause__ when available. ModelError includes selected provider error messages and upstream details instead of dumping the entire response body. Known credentials are redacted from those details; provider messages may still contain input data. The chained httpx.HTTPStatusError retains the response for HTTP failures.

ModelError exposes status_code, code, provider, finish_reason, and native_finish_reason when available. An HTTP 200 response with an error envelope or a Chat Completions finish_reason="error" raises ModelError, including the native reason such as MALFORMED_FUNCTION_CALL. Invalid generated data, refusals, and truncated output remain SchemaValidationError.

Messages explain what failed and end with a Next step: suggestion. For example:

Model provider returned HTTP 401.
Code: authentication_error
Details: Invalid API key.
Next step: Check the API key or authentication headers for the selected provider.

Schema errors list field paths and validation reasons, with at most five details. Raw invalid values are omitted; custom ValueError/assertion messages and invalid union tags are summarized because they can embed input data. Request and validation failures include attempt counts. Chained exceptions retain full diagnostics and can contain sensitive data. For programmatic handling, use exception classes and ModelError attributes rather than parsing message text.

Exception Raised when
ModelError A model request fails, or the provider reports an HTTP or generation error.
SchemaValidationError The model's output doesn't match the schema.
ProviderNotInstalledError Deprecated compatibility export; never raised by HTTP adapters.
ExtractionError A URL can't be fetched.

Invalid configuration or unsupported media raises ValueError; unsupported model objects or Decisions field types raise TypeError. Local file errors retain their standard Python exceptions. If openextract can't infer the media type, it raises a ValueError and asks you to pass media_type.

Migrating from Pydantic AI

Install plain openextract; provider extras have been removed. Replace configured Pydantic AI Model objects with openextract.Model. Supported model strings and the Pydantic schema/validated-result interface remain available. Provider support is defined by the adapters above rather than by Pydantic AI's model registry. Vertex AI credential handling, SDK-specific configuration, and Logfire integration are not bundled. HTTPX clients provide the transport customization boundary.

Plain string sources are now document text, with HTTP(S) URLs still fetched. Wrap local file names in Path(...): replace source="invoice.pdf" with source=Path("invoice.pdf"). Missing Path inputs still raise FileNotFoundError. Raw bytes remain supported.

Tests

Default tests run offline and skip all live API calls. Saved synthetic PDF/image fixtures cover single-page, multi-page, and scanned invoices. Run the provider matrix explicitly with:

uv run --env-file .env pytest --live -m live --no-cov -v

See Contributing for credentials, provider filters, and the offline CI command.

License

MIT

Metadata

Release files for openextract 2.0.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 2.0.0
File Size Uploaded Provenance Actions
openextract-2.0.0.tar.gz 671.8 kB With provenance Details for openextract-2.0.0.tar.gz

Built distribution (wheel)

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

Total release size: 699.2 kB

Release files / openextract-2.0.0.tar.gz

Download URL openextract-2.0.0.tar.gz
Size 671.8 kB
Tags Source
SHA-256 checksum
How to use checksums
daef5ec46c03942f94d4f862a45d9b0a0c4973874dc0d872c50522f4a1590dc1
BLAKE2b-256 checksum
How to use checksums
16c0706c5c07a8288840825bbd9cc70e6f37650c39695f514d95699e086a731b
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 Nov 10, 2026.

Transparency log

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

Download URL openextract-2.0.0-py3-none-any.whl
Size 27.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
db69c631fd2a11387e5072d4bc63c12c68e647c48080c6db8c259e9859a61bf6
BLAKE2b-256 checksum
How to use checksums
cf47634164a6be9c6280351fb4521531cd9b627192688cd77d21acfef14e3827
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 Nov 10, 2026.

Transparency log

Security for openextract 2.0.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-2.0.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 Nov 10, 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