Styxis
Company
Knowledge

Pydantic Decimal JSON Schema: aligning scientific notation with the consumer contract

Separate numeric-accuracy requirements from permitted string notation. Consumers then get a compatibility choice instead of an unnecessary change to the value.

Pydantic Decimal JSON Schema: aligning scientific notation with the consumer contract

An exact Decimal value can still fail exchange when producer and consumer use different string grammars. Recalculating the value does not resolve a serializer that emits scientific notation alongside a schema that rejects it. Both sides must agree whether to expand the grammar or constrain the output representation. [1, 2, 3]

What the primary source establishes

A Pydantic report describes Decimal serialization producing 1E-7 while its serialization JSON Schema pattern lacks an exponent component and rejects that output. It proposes either extending the schema or using fixed-point serialization. [1]

The handover decision

Producer and consumer should agree whether scientific notation is permitted, then test small values, large values, and signed values through that policy. Retain decimal_text, notation_policy, and schema_acceptance so a formatting disagreement is not mistaken for arithmetic failure. [1, 3]

A reference defined before the output

The illustrative contract permits an exponent, so 1E-7 should be accepted. A rejected result fails this chosen grammar without demonstrating that the underlying decimal value is wrong. The normal state is accepted, and the counterexample is rejected; the reference is the agreed notation policy. [1, 3]

The scoped Truthound check

Truthound Core 3.3.2 can check original Decimal strings against the approved exponent grammar with RegexValidator. Confirm that the pattern implements the business grammar and validate arithmetic separately. Update the producer and consumer contract together instead of silently rewriting output text. [1, 2, 3]

Limits and alternative choices

A regular-expression match does not prove Decimal arithmetic or rounding accuracy. If consumers require fixed-point notation, serialize explicitly to that representation. If the consumer intentionally forbids exponents, changing producer formatting may be appropriate instead of widening the schema. [1, 3]

Application conditions

Define the accepted exponent grammar, apply it to the original Decimal strings, and record the acceptance state separately.

Alternatives

If consumers require fixed-point notation, serialize explicitly to that representation.

Limitations

  • A regular-expression match does not prove Decimal arithmetic or rounding accuracy.

Evidence

Event checked
Subject checked
Modified