Skip to content

clairic 0.1.0: typed SDK for Cloudflare Clairic - #1

Merged
VianneyMI merged 9 commits into
v0.1from
feat/v0.1
Oct 25, 2026
Merged

VianneyMI merged 9 commits into
v0.1from
feat/v0.1

Conversation

@VianneyMI

Copy link
Copy Markdown
Owner

What it does

First release of clairic, an unofficial typed Python SDK for Cloudflare Clairic. A user declares a Pydantic model whose fields are Clairic answer types, calls Clairic().extract(text, Model), and gets a Result holding each value with its metadata (confidence, probability of absence, spans, percentiles).

  • All 9 Clairic field types: text, number, date, time, datetime, duration, lists, estimates (number and date bounds), forecasts.
  • Options through Annotated specs; specs reject the option values the live API rejects, before any request.
  • Sync Clairic and async AsyncClairic clients; Cloudflare errors map to typed exceptions with status, code and request id.
  • Result.values_as(PlainModel) exports plain values; results round-trip through JSON.
  • Docstrings adapted from the Cloudflare documentation (CC BY 4.0, credited in NOTICE).

Answer shapes, accepted options and error codes come from 54 requests recorded against the live API on 2026-10-25, stored under tests/fixtures/responses.

One commit per step: packaging, answers and errors, specs, compiler, result, clients, live tests and schema drift check, README and example, release metadata.

How to test

uv sync
uv run ruff format --check && uv run ruff check && uv run pyright && uv run pytest
uv run pytest -m live                     # needs CLOUDFLARE_ACCOUNT_ID and CLOUDFLARE_API_TOKEN
uv run python examples/invoice.py

Locally: 98 offline tests and 5 live tests pass on Python 3.14 and 3.10.

🤖 Generated with Claude Code

VianneyMI and others added 9 commits October 25, 2026 18:01
Turn the proof-of-concept project into the `clairic` package: hatchling
build with a src layout, Python 3.10+, httpx and pydantic dependencies,
ruff, pyright in strict mode, pytest, and a CI workflow running all
checks on Python 3.10 to 3.14. Add the MIT license, a NOTICE crediting
the Cloudflare documentation (CC BY 4.0) and the unofficial disclaimer.

main.py is removed here because this commit drops the `requests`
dependency it relies on; examples/invoice.py replaces it in S8.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Answer models cover every Clairic response shape: text, number, date,
time, datetime, duration, text lists, number and date lists, numeric and
date estimates, and forecasts. Shapes and semantics come from responses
recorded against the live API on 2026-10-25, now stored under
tests/fixtures/responses (account id redacted). response.json moves
there as invoice.json.

errors.py defines the exception hierarchy and ClairicTruncationWarning.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Specs carry the options of each Clairic field type and attach to answer
types through Annotated. Constructors reject every option value the live
API rejected during probing (bounds, decimals, horizon, null threshold,
log scale minimum, steps, duration units), so a mistake surfaces where
the model is declared. pytest now also runs the doctests in src.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The compiler turns a Pydantic model whose fields are answer types into
the Clairic `fields` object and reads the answers back into the model.
The answer type picks the Clairic field type and implies `integer` and
list `items`; an Annotated spec adds options; Field(description=...)
becomes the instructions; an alias becomes the Clairic field name.
Invalid models raise ModelDefinitionError naming the field, before any
request. Compiled payloads are cached per model class.

tests/typing holds assert_type checks that pyright enforces, including
attribute accesses that must stay type errors.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Result[M] holds the extraction model, token usage, the truncation flag
and the request id. It round-trips through JSON, including models with
aliased fields, which are dumped under their Clairic names.
values_as(Target) copies each answer value into the same-named field of
a plain-values model and lets Pydantic validate it.

Usage documents that input tokens count the state once per pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Clairic and AsyncClairic post an extraction model to the Workers AI REST
endpoint and return a Result. Credentials fall back to
CLOUDFLARE_ACCOUNT_ID and CLOUDFLARE_API_TOKEN. Cloudflare error
responses map to typed exceptions carrying the status, error code and
request id; transport failures map to APIConnectionError and
APITimeoutError; a truncated result emits ClairicTruncationWarning at
the caller. Invalid models and empty states fail before any request.

The package now exports its public API, and the version lives in
_version.py, read by hatch and sent in the User-Agent header.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
tests/live extracts every field type, estimates, a forecast and an
authentication error against the real API; they run with
`pytest -m live` when CLOUDFLARE_ACCOUNT_ID and CLOUDFLARE_API_TOKEN are
set (a local .env is loaded) and are deselected otherwise.

scripts/check_schema_drift.py diffs Cloudflare's published input and
output schemas against the copies vendored in tests/fixtures/schemas; a
weekly workflow runs it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The README covers installation, credentials, declaring fields with
answer types and specs, reading answers and their metadata, the values
export, storing results, the async client, errors, cost per pass, and
development commands. examples/invoice.py runs the reference invoice
extraction end to end and replaces the former main.py.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add the repository, issues and Clairic reference links shown on the
PyPI project page, and run CI on pushes to version branches (v*), where
each release line lives.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@VianneyMI
VianneyMI merged commit d4b0f33 into v0.1 Oct 25, 2026
5 checks passed
@VianneyMI VianneyMI self-assigned this Oct 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant