Styxis
Company
Knowledge

Pydantic strict Literal: preserving the original JSON numeric kind

If a protocol distinguishes original numeric kinds, a normalized value alone cannot prove that the received representation met the contract.

Pydantic strict Literal: preserving the original JSON numeric kind

Obtaining an allowed numeric value does not establish that the original token kind met the contract. A protocol distinguishing integer and decimal notation cannot be checked from normalized values alone. Preserve the raw kind to inspect the actual admission policy instead of relying on an option named strict. [1, 2, 3]

What the primary source establishes

A report describes strict integer Literal validation accepting numerically equal floating-point inputs and converting them into integers. It compares StrictInt rejection and asks whether the behavior is intentional or needs clearer documentation. [1]

The handover decision

Protocol and model owners should retain the raw numeric token kind and check it separately from the permitted value set. Compare raw_numeric_kind, normalized_value, and protocol_kind so a value that looks correct after coercion does not erase evidence about the input. [1, 3]

A reference defined before the output

The illustrative protocol treats 2 as an integer token and distinguishes 2.0 and 2e0. The normal kind is integer; decimal is the counterexample. A normalized result of 2 does not overwrite the original kind. This is a declared protocol rule rather than a claim that every JSON consumer must make the distinction. [1, 3]

The scoped Truthound check

Truthound Core 3.3.2 can check raw_numeric_kind, preserved during preprocessing, against an allowed set with InSetValidator. A normalized numeric column cannot recover the original token kind. Keep raw parsing and allowed-value validation separate before approving them together. [1, 2, 3]

Limits and alternative choices

JSON itself does not require every consumer to distinguish these representations; the rule belongs to this business contract. Add explicit type checks at raw parsing and combine them with value validation. A protocol accepting all mathematically equivalent numbers does not need this strict token distinction. [1, 3]

Application conditions

Extract raw token kinds before coercion and preserve their link to normalized values and the explicit protocol policy.

Alternatives

Add explicit type checks at raw parsing and combine them with value validation.

Limitations

  • JSON itself does not require every consumer to distinguish these representations; the rule belongs to this business contract.

Evidence

Event checked
Subject checked
Modified