Skip to main content

clairic

Unofficial typed Python SDK for Cloudflare Clairic, the Workers AI extraction model. This project is not affiliated with or endorsed by Cloudflare.

Clairic reads a text and returns typed values for the fields you name: extracted, computed, estimated or forecast, each with a confidence score, a probability of absence, or a percentile range. clairic lets you declare those fields as a Pydantic model and get the answers back as typed Pydantic objects.

from typing import Annotated

from pydantic import BaseModel, Field

from clairic import Clairic, DateAnswer, Estimate, EstimateAnswer, Number, NumberAnswer, TextAnswer


class InvoiceExtraction(BaseModel):
    invoice_number: TextAnswer
    due_date: DateAnswer = Field(description="Issue date plus the net payment days")
    total: Annotated[NumberAnswer[float], Number(decimals=2, unit="USD")] = Field(
        description="Amount due including shipping"
    )
    po_number: TextAnswer = Field(description="Purchase order number")
    days_to_pay: Annotated[EstimateAnswer[int], Estimate(min=0, max=120, step=1)] = Field(
        description="Days after issue until this invoice is paid"
    )


with Clairic() as clairic:
    result = clairic.extract(
        "Invoice INV-2291 from Northwind Supply, issued 2026-09-02, net 30. "
        "4 pallets at $250.00 each, shipping $80.00.",
        InvoiceExtraction,
    )

result.data.total.value  # 1080.0
result.data.total.confidence  # 0.99995
result.data.po_number.value  # None: the invoice has no PO number
result.data.days_to_pay.p90  # 37

Install

pip install clairic

Python 3.10 or later. Dependencies: httpx and pydantic 2.

Credentials

The client reads CLOUDFLARE_ACCOUNT_ID and CLOUDFLARE_API_TOKEN, or takes them as arguments:

clairic = Clairic(account_id="...", api_token="...")

Create the token in the Cloudflare dashboard with access to Workers AI.

Declaring fields

Each field of the extraction model is an answer type. The answer type sets the Clairic field type; an Annotated spec sets its options; Field(description=...) gives Clairic the instructions; a Pydantic alias sets the field name sent to Clairic.

Answer type Clairic type Spec (options)
TextAnswer text: a span of the input Text(unit, null_threshold)
NumberAnswer[float], NumberAnswer[int] number (int requests whole numbers) Number(unit, null_threshold, decimals)
DateAnswer, TimeAnswer, DatetimeAnswer date, time, datetime Date(null_threshold), Time(...), Datetime(...)
DurationAnswer[float], DurationAnswer[int] duration Duration(unit, null_threshold, decimals), unit defaults to seconds
TextListAnswer list of text spans List(unit, decimals)
ListAnswer[int], ListAnswer[float], ListAnswer[date] list of numbers or dates List(unit, decimals)
EstimateAnswer[int], EstimateAnswer[float] estimate with number bounds Estimate(min, max, step, scale, unit), required
DateEstimateAnswer estimate with date bounds Estimate(min=date, max=date, step="month"), required
ForecastAnswer forecast of a series Forecast(min, max, horizon, interval, step, scale, unit), required

Invalid declarations fail before any request: a non-answer annotation, a missing or mismatched spec, or an option Clairic rejects (such as decimals=7 or scale="log" with min=0) raises ModelDefinitionError or ValueError naming the problem.

Set reference_date whenever a field depends on today's date or on dates written without a year:

clairic.extract(text, InvoiceExtraction, reference_date=date(2026, 9, 2))

Reading answers

Every answer holds a value and the metadata Clairic attaches to it. Your editor completes the attributes, and a type checker knows their types.

  • Single values (text, number, date, time, datetime, duration): value, guess, confidence, p_null. value is None when p_null exceeds the field's null_threshold (0.5 by default); guess still shows the best candidate.
  • Text: span, end-exclusive offsets into the input: text[span.start:span.end]. Text lists carry spans.
  • Number and date lists: confidences, one per item.
  • Estimates: value (the mean rounded to the step grid), mean, median, p10, p90, and for number bounds p_below and p_above, the probability mass outside [min, max].
  • Forecasts: per-step lists value, median, p10, p90, and steps with the full estimate of each step.

The p10 to p90 range is an 80% interval. Cloudflare measures its calibration but does not guarantee it, and intervals can be too narrow on unfamiliar data. Use confidence, p_null and the interval to decide which answers to act on and which to review.

Plain values

values_as copies each answer value into the same-named field of a plain Pydantic model, ready for the rest of your program:

class Invoice(BaseModel):
    invoice_number: str
    total: float
    po_number: str | None
    days_to_pay: int


invoice = result.values_as(Invoice)

Pydantic validates the values, so a None landing in a field that does not accept None raises ValidationError.

Storing results

A Result serializes to JSON and back with its answers, token usage and request id:

stored = result.model_dump_json()
result = Result[InvoiceExtraction].model_validate_json(stored)

Async

from clairic import AsyncClairic

async with AsyncClairic() as clairic:
    result = await clairic.extract(text, InvoiceExtraction)

Errors

Exception When
ModelDefinitionError The extraction model cannot be sent to Clairic. Raised before any request.
ClairicConfigurationError The account id or token is missing.
AuthenticationError HTTP 401: invalid token or unknown account.
InvalidRequestError HTTP 400 or 422: Cloudflare rejected the request content.
ContextWindowExceededError HTTP 413: the input exceeds the 65,536-token context window.
APIError Any other error response, or answers that do not fit the model. Base class of the API errors.
APIConnectionError, APITimeoutError Cloudflare could not be reached, or did not answer within the timeout (30 seconds by default).

API errors carry status_code, the Cloudflare error code, message and request_id. When Clairic reports a truncated input, the client emits ClairicTruncationWarning and sets result.truncated.

Cost

Clairic bills input tokens only ($0.10 per million at the time of writing). Fields that do not fit next to the input in one 8,192-token pass are split across parallel passes, and each pass reads the whole input, so result.usage.input_tokens counts the input once per pass.

Development

uv sync
uv run ruff format --check && uv run ruff check && uv run pyright && uv run pytest
uv run pytest -m live                         # real API, needs credentials
uv run python scripts/check_schema_drift.py   # compare Cloudflare's schemas with the vendored ones

examples/invoice.py runs the invoice extraction end to end.

License and attribution

MIT. Docstrings adapt text from the Cloudflare documentation, published under CC BY 4.0; see NOTICE.

Metadata

Release files for clairic 0.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 clairic 0.1.0
File Size Uploaded Provenance Actions
clairic-0.1.0.tar.gz 17.4 kB Without provenance Details for clairic-0.1.0.tar.gz

Built distribution (wheel)

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

Total release size: 39.5 kB

Release files / clairic-0.1.0.tar.gz

Download URL clairic-0.1.0.tar.gz
Size 17.4 kB
Tags Source
SHA-256 checksum
How to use checksums
bb408d59184712d28fc3f07dd789cdff0320616f2af0a837811350ab5096c492
BLAKE2b-256 checksum
How to use checksums
cf7f51b5d102d18e361c98387db550fb270a634028f9d974370b4a343a60c3f6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.10.0 {"installer":{"name":"uv","version":"0.10.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / clairic-0.1.0-py3-none-any.whl

Download URL clairic-0.1.0-py3-none-any.whl
Size 22.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
90601517cb1b854122430898a4caa998d06219e6444d6f6c6e86ba3abcff6ee2
BLAKE2b-256 checksum
How to use checksums
bf1daaf0e4f83de68453a3c6582aaadf8c6e5a34ba54714c9f6b951b0c506bc0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.10.0 {"installer":{"name":"uv","version":"0.10.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for clairic 0.1.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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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