From f26ebc892bff1f291e23d488e38e0255cce8ddff Mon Sep 17 00:00:00 2001 From: Vianney Mixtur Date: Wed, 11 Nov 2026 23:05:34 +0100 Subject: [PATCH] Rewrite the README around runnable examples Overview, features, installation, then three usage examples built on the recorded invoice call: basic extraction, an estimate field, and values_as into a plain model. Reference sections are tightened, with a reading-answers table, real token usage and a preview-status section. Co-Authored-By: Claude Opus 5.5 --- README.md | 198 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 142 insertions(+), 56 deletions(-) diff --git a/README.md b/README.md index d3ad31d..93d9c88 100644 --- a/README.md +++ b/README.md @@ -1,63 +1,137 @@ # clairic -**Unofficial** typed Python SDK for [Cloudflare Clairic](https://developers.cloudflare.com/workers-ai/models/clairic/), the Workers AI extraction model. This project is not affiliated with or endorsed by Cloudflare. +clairic is a typed Python SDK for [Clairic](https://developers.cloudflare.com/workers-ai/models/clairic/), +Cloudflare's Workers AI extraction model. Clairic reads a text and returns values for the fields you +name, whether extracted, computed, estimated or forecast. Each value comes with signals for +deciding what to automate: a confidence score, a probability of absence, a source span or a +percentile range. clairic turns your Pydantic model into the request and gives the answers back as +that model. + +## Features + +- Declare fields as a Pydantic model, with one answer type per field. +- Read each value with its confidence, probability of absence, span or percentiles one attribute + away. +- Use all nine Clairic field types, from text spans to forecasts. +- Catch invalid declarations before any request. +- Export plain values to your own domain model with `values_as`. +- Read Cloudflare's documentation in your editor, through the docstrings. +- Use a sync or an async client, fully typed for pyright. + +## Installation -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. +```sh +pip install clairic +``` + +Python 3.10 or later. The client reads `CLOUDFLARE_ACCOUNT_ID` and `CLOUDFLARE_API_TOKEN`, or takes +them as arguments with `Clairic(account_id="...", api_token="...")`. Create the token in the +Cloudflare dashboard with access to Workers AI. + +## Usage + +> The examples below extract the same invoice. Outputs come from a call to Clairic. + +### Basic usage ```python from typing import Annotated from pydantic import BaseModel, Field -from clairic import Clairic, DateAnswer, Estimate, EstimateAnswer, Number, NumberAnswer, TextAnswer +from clairic import Clairic, DateAnswer, Number, NumberAnswer, TextAnswer, TextListAnswer +INVOICE = ( + "Invoice INV-2291 from Northwind Supply, issued 2026-09-02, net 30. 4 pallets at $250.00 " + "each, shipping $80.00. Contact: Dana Ruiz, Priya Shah." +) + +# Declaring the fields to extract 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" ) + contacts: TextListAnswer po_number: TextAnswer = Field(description="Purchase order number") + + +# Extracting +with Clairic() as clairic: + result = clairic.extract(INVOICE, InvoiceExtraction) + +invoice = result.data + +invoice.invoice_number.value # 'INV-2291' +invoice.due_date.value # datetime.date(2026, 10, 2), computed from the issue date and net 30 +invoice.total.value # 1080.0, computed from the pallets and shipping +invoice.total.confidence # 0.99995 +invoice.contacts.value # ['Dana Ruiz', 'Priya Shah'] + +invoice.po_number.value # None +invoice.po_number.p_null # 0.9999994, the probability that the PO number is absent +invoice.po_number.guess # 'INV-2291', the best candidate +``` + +### Estimates + +An estimate field asks Clairic for a number the text only hints at. Continuing from the previous +example: + +```python +from clairic import Estimate, EstimateAnswer + + +# Adding an estimate to the extraction model +class InvoiceForecast(InvoiceExtraction): 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 = clairic.extract(INVOICE, InvoiceForecast) + +days = result.data.days_to_pay -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 +days.value # 31, the mean rounded to the step +days.p10 # 24 +days.p90 # 37 ``` -## Install +### Plain values -```sh -pip install clairic -``` +`values_as` copies each answer value into the same-named field of your own model, ready for the rest +of your program. Continuing from the previous example: -Python 3.10 or later. Dependencies: `httpx` and `pydantic` 2. +```python +import datetime as dt -## Credentials -The client reads `CLOUDFLARE_ACCOUNT_ID` and `CLOUDFLARE_API_TOKEN`, or takes them as arguments: +class Invoice(BaseModel): + invoice_number: str + due_date: dt.date + total: float + contacts: list[str] + po_number: str | None + days_to_pay: int -```python -clairic = Clairic(account_id="...", api_token="...") + +result.values_as(Invoice) +# Invoice(invoice_number='INV-2291', due_date=datetime.date(2026, 10, 2), total=1080.0, +# contacts=['Dana Ruiz', 'Priya Shah'], po_number=None, days_to_pay=31) ``` -Create the token in the Cloudflare dashboard with access to Workers AI. +Pydantic validates the values on the way. A `None` landing in a field typed `str` raises +`ValidationError`, and `str | None` accepts it. ## 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. +Each field of the extraction model has an answer type, which sets the Clairic field type. An +`Annotated` spec sets its options, `Field(description=...)` holds the instructions, and a Pydantic +`alias` sets the field name sent to Clairic. | Answer type | Clairic type | Spec (options) | |---|---|---| @@ -71,9 +145,11 @@ Each field of the extraction model is an answer type. The answer type sets the C | `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. +Declarations are checked before any request. A field with a plain type, a missing or mismatched +spec, or an option Clairic rejects (such as `decimals=7`, or `scale="log"` with `min=0`) raises +`ModelDefinitionError` or `ValueError` with the problem named. -Set `reference_date` whenever a field depends on today's date or on dates written without a year: +Fields that depend on today's date, or on dates written as day and month only, need a `reference_date`: ```python clairic.extract(text, InvoiceExtraction, reference_date=date(2026, 9, 2)) @@ -81,32 +157,23 @@ 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: - -```python -class Invoice(BaseModel): - invoice_number: str - total: float - po_number: str | None - days_to_pay: int +Every answer holds a `value` and the metadata Clairic attaches to it, all typed for your editor. +| Answers | Attributes | +|---|---| +| Single values (`text`, `number`, `date`, `time`, `datetime`, `duration`) | `value`, `guess`, `confidence`, `p_null` | +| 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`. With number bounds, `p_below` and `p_above` hold the probability mass below `min` and above `max` | +| Forecasts | Per-step lists `value`, `median`, `p10`, `p90`, and `steps` with the full estimate of each step | -invoice = result.values_as(Invoice) -``` +`value` turns to `None` when `p_null` exceeds the field's `null_threshold`, 0.5 by default, and +`guess` keeps the best candidate. On "can be reached at +33 6 12 34 56 78 or at +firstname.name@gmail.com", Clairic returned the phone number with a confidence of 1.0, and for the +email a `p_null` of 0.66 with the address in `guess`. -Pydantic validates the values, so a `None` landing in a field that does not accept `None` raises `ValidationError`. +The p10 to p90 range is an 80% interval. Cloudflare publishes calibration measurements, and +intervals can come out too narrow on new kinds of data. ## Storing results @@ -130,19 +197,39 @@ async with AsyncClairic() as clairic: | Exception | When | |---|---| -| `ModelDefinitionError` | The extraction model cannot be sent to Clairic. Raised before any request. | +| `ModelDefinitionError` | The extraction model is invalid for 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). | +| `APIError` | Any other error response, or answers outside the model's shape. Base class of the API errors. | +| `APIConnectionError`, `APITimeoutError` | Cloudflare was unreachable, or the timeout expired (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`. +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. +Clairic bills input tokens, at $0.10 per million at the time of writing. It packs fields next to +the input in 8,192-token passes and runs extra passes in parallel when they overflow. Each pass +reads the whole input, and `result.usage.input_tokens` counts it once per pass. The full invoice +extraction above took 655 input tokens over 3 passes. + +## Preview status + +Clairic is a preview model. Cloudflare provides it as is and may withdraw it at any time. Field +options can change too, and a weekly CI job compares Cloudflare's published schemas with the ones +vendored here. The cause of `truncated: true` is under investigation in +[#15](https://github.com/VianneyMI/clairic/issues/15). + +## Going further + +- [examples/invoice.py](examples/invoice.py) runs the invoice extraction end to end. +- [Cloudflare's Clairic documentation](https://developers.cloudflare.com/workers-ai/models/clairic/) + covers every field type and option. +- [python-jev](https://github.com/VianneyMI/python-jev) applies the same approach to TypeSafe's + Jev, a decision model. ## Development @@ -153,8 +240,7 @@ 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`. +MIT. clairic is a community project, independent from Cloudflare. Docstrings adapt text from +Cloudflare's documentation, published under CC BY 4.0 (see `NOTICE`).