Skip to content

clairic 0.3.0: typed and generated answers for the new Clairic schema - #24

Open
VianneyMI wants to merge 10 commits into
schema-drift/updatefrom
feat/v0.3
Open

VianneyMI wants to merge 10 commits into
schema-drift/updatefrom
feat/v0.3

Conversation

@VianneyMI

@VianneyMI VianneyMI commented Dec 5, 2026 •

Copy link
Copy Markdown
Owner

clairic 0.3.0: typed and generated answers for the new Clairic schema

Stacked on #21 (0.2.0); retarget to main once #21 is merged. Models everything Cloudflare's November 2026 schema offers. Design record: .cursor/brainstorms/clairic-schema-change.md (local).

The model

Clairic answers in two ways, and the answer classes now follow the response shape:

  • Generated (GeneratedAnswer): Clairic writes JSON and checks it. Covers TextAnswer, NumberAnswer, BooleanAnswer, EnumAnswer[Literal[...]], ObjectAnswer[Model], ListAnswer[T], SourceOnlyAnswer[T] and SourceOnlyListAnswer[T]. Each answer carries valid, complete, raw, decoder, tokens, failure_reason, …
  • Typed (TypedAnswer): Clairic scores candidates and keeps a guess. Covers DateAnswer, TimeAnswer, DatetimeAnswer, DurationAnswer and the new ComputeAnswer.
  • The annotation decides the field type and the answer shape: enum values come from the Literal, object properties from a plain model, and source_only from SourceOnlyAnswer. Specs carry only the options each type accepts.

Commits

  • S0 Re-record the fixtures against the current API, and record every new shape and error.
  • S1 Split answers into typed and generated families.
  • S2 Unify lists in ListAnswer[V] (TextListAnswer removed), and check generic arguments at declaration.
  • S3 Add the text, number and list options (nullable, knowledge, enum, pattern, length and bound limits, max_items).
  • S4 Add compute fields.
  • S5 Add boolean and enum fields.
  • S6 Add objects and lists of objects. Also fixes values_as for aliased nested models.
  • S7 Add source-only fields and lists.
  • S8 Update the README, example and docstrings.
  • Prepare the 0.3.0 release.

Breaking changes (from 0.2)

  • TextListAnswer is removed: use ListAnswer[str].
  • guess is removed from text and number answers.
  • Results stored from launch-era responses no longer load.

Testing

  • ruff, pyright strict and pytest pass (180 tests). The typing file has positive and negative assertions for every new class.
  • The live suite passes (11 tests, including the new types). examples/invoice.py runs live.
  • A test checks that every key the specs can send exists in the vendored input schema.

🤖 Generated with Claude Code

The launch-era recordings (invoice, types_present, types_absent,
model_full_name, options_state_text) no longer match what Clairic
returns: text and number answers are now generated, with valid,
complete, decoder and raw metadata and no guess. They are re-recorded
with identical requests; options_state_text drops its text
null_threshold fields, which Clairic now rejects.

New recordings cover the 0.3 answer model: boolean, enum, object and
compute fields, source-only values and lists, object lists (including
one read from a table), the new text, number and list options, a
pattern no value matches, and the errors for forbidden option pairs.

The 0.2 models parse every recording; only assertions on recorded values
change (A-55120 now has no span because it appears twice, text and
number answers carry no guess, usage and estimates moved).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
VianneyMI and others added 9 commits December 5, 2026 14:14
Clairic now answers fields in two ways. Date, time, datetime and
duration fields are scored: Clairic ranks candidates and reports the
best as guess, with p_null deciding against null_threshold. Text, number
and list fields are generated: Clairic writes JSON, checks it against the
field's options, and reports valid, complete, decoder, raw and tokens.

Answer classes now follow the response shape:

- TypedAnswer (p_null, confidence) is the base of DateAnswer,
  TimeAnswer, DatetimeAnswer and DurationAnswer, which keep guess.
- GeneratedAnswer (valid, complete, p_null, confidence, plus the
  optional log_probability, failure_reason, retry_input_tokens,
  null_recovered and method labels) is the base of the generated answers.
  Single values add decoder, raw, tokens, span and their method labels.
- TextAnswer and NumberAnswer become generated and lose guess; both list
  classes gain p_nulls and optional spans.

Required keys are those present in every recorded answer of the family,
invalid answers included (S0). A launch-era text answer no longer
validates.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
0.1 split lists into TextListAnswer (with spans) and ListAnswer[V] (with
per-item confidences) because the two had different response shapes.
Clairic now returns one list shape for every item type: valid, complete,
confidences, p_nulls, p_null and confidence, plus spans for text and
number items.

- TextListAnswer is removed; ListAnswer[str] replaces it. V is bound to
  str | int | float | date, so pyright keeps the exact item type.
- A null list value, which the schema allows for invalid lists, reads as
  an empty list; valid and complete say why it is empty.
- Pydantic does not check a generic's type argument, and pyright maps
  bool to int, so the compiler now rejects NumberAnswer[bool],
  ListAnswer[bool], ListAnswer[datetime] and other unsupported
  arguments before any request.
- List(decimals=...) stays limited to number items.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Clairic now accepts options per field type, and the specs expose exactly
the documented ones a probe of every option on every type confirmed
(December 2026):

- Text: nullable, knowledge, enum, pattern, min_length, max_length.
- Number: nullable, knowledge, enum, minimum, maximum,
  exclusive_minimum, exclusive_maximum, besides unit and decimals.
- List: max_items, knowledge, enum, besides unit and decimals.

Spec.to_payload sends the schema's camelCase keys (minLength,
exclusiveMinimum, ...) and tuples as lists. The compiler rejects
knowledge on lists that are not text or number lists, as it already did
decimals on lists that are not number lists, and returns a deep copy now
that payloads nest lists.

Docstrings record what probes saw: nullable=False makes Clairic invent a
missing value and still mark it valid, knowledge had no visible effect,
and a pattern nothing matches yields null.

A new test checks that every key the specs send exists in the vendored
input schema, so the drift check and the specs fail together.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Clairic's new compute type derives a number from the input, such as a
total or a difference. It is scored like dates and durations, not
written, so ComputeAnswer[N] is a typed answer with value and guess.
ComputeAnswer[int] sends integer: true.

The Compute spec carries unit, decimals and null_threshold. It has no
nullable option: Clairic requires compute answers to stay nullable.
A Number spec on a compute field is refused at declaration.

The docstring keeps the probe result: a simple invoice total came back
wrong with a confidence of 0.45, so read confidence or compute in code
when it matters.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Clairic's new boolean and enum types are generated answers, like text.

- BooleanAnswer answers a yes or no question; its span is always None.
- EnumAnswer[E] takes its allowed values from the annotation:
  EnumAnswer[Literal["card", "transfer", "cash"]] sends
  enum: ["card", "transfer", "cash"], and a value outside the Literal
  fails validation. E is bound to str, so pyright keeps the Literal.
- The Boolean and Enum specs carry nullable only.

The compiler rejects EnumAnswer[str], a union of Literals and an
unparametrized EnumAnswer, and each "parametrize" error now shows an
example for its own class.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Clairic's object type reads several related values together, and lists
can now hold objects. Both take their properties from a plain Pydantic
model:

- ObjectAnswer[M] holds an instance of M, or None.
- ListAnswer[M] holds a list of M; V's bound gains BaseModel, so pyright
  types res.data.lines.value as list[Line]. property_spans carries the
  per-row spans Clairic documents for tables.
- Model fields map to properties: str to text, int to number with
  integer, float to number, bool to boolean, a Literal of strings to
  enum. "| None" is unwrapped, an alias names the property and a
  description becomes its instructions. Nested models, lists, dates and
  specs on properties are refused, matching the API
  (err_object_property_date).
- Clairic returns null for properties the input lacks (objects
  recording); the docstring says to declare such fields "| None".
- Lists of objects take only max_items, so unit and enum are refused.

values_as now dumps nested values by alias, which fixes exporting
objects whose properties use aliases.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Clairic's source_only option keeps only values printed in the input and
normalizes them from the printed text. It turns dates, times and
datetimes into generated answers, so it changes the answer's shape and
lives in the annotation:

- SourceOnlyAnswer[S], for int, float, date, time or datetime, sends the
  matching type with source_only: true and takes the Number, Date, Time
  or Datetime spec. Its answer adds printed_value and
  normalization_method; when the printed text cannot be normalized
  ("1,200 EUR", "Friday") it is invalid, with value None and a
  failure_reason.
- SourceOnlyListAnswer[L], for int, float or date, sends a source-only
  list. Recordings show the plain list shape, so both list classes now
  share a private base.

The compiler refuses knowledge (err_knowledge_source_only) and
null_threshold (err_source_only_null_threshold) on source-only fields,
and unsupported type arguments.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- README: the field table gains the new types and a generated/typed
  column, the answer table is split by family, a new example covers
  choices, objects, computed totals and printed values (outputs from a
  live call, December 2026), and an "Upgrading from 0.2" section lists
  the breaking changes. It also records what probes saw: nullable=False
  invents values, knowledge had no effect, compute can be wrong.
- examples/invoice.py extracts an order with the new types and exports a
  list of objects with values_as.
- The answers and specs module docstrings describe the two families and
  the validation policy: specs carry only the options each type accepts,
  and value ranges stay with Clairic while its API is in preview.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@VianneyMI VianneyMI added this to the v0.3 milestone Dec 5, 2026
@VianneyMI
VianneyMI marked this pull request as ready for review December 5, 2026 13:59
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