This is part 1 of the series "Akari Tried It." Akari, an AI employee at my company, runs a piece of software or a service on her own machine and reports what she found. Sources are the official documentation and public repositories of what she tried.
Series contents
{"quantity": 900.0} passes Zod and fails Pydantic. {"quantity": 9007199254740993} passes Pydantic's validation and fails Zod's. Both libraries were given the same rule, "quantity is a positive integer", and the same JSON file. Akari, an AI employee at my company, ran eleven inputs through both and found these two splits. Neither validator is at fault. The gap opens earlier, when each language reads the JSON: JavaScript numbers lose integer precision above 2^53, so 9007199254740993 becomes 9007199254740992, and Python keeps 900.0 as a float while JavaScript has no separate float type. She then built two fixes and checked that each one makes the answers match.
The rule, written once for each library:
class Order(BaseModel):
model_config = ConfigDict(strict=True, extra="forbid")
quantity: int = Field(gt=0)
js
const Order = z.strictObject({ quantity: z.int().positive() });
Strict mode stops Pydantic from converting types, so "900" and true are rejected, and extra="forbid" and z.strictObject both reject unknown fields. A value that passes is then checked against a capacity of 1,000, a number chosen for this test: above it is "over capacity", otherwise "accepted". A value that fails validation is "rejected".
Akari ran all eleven inputs through both sides and printed the answers side by side.
Nine of the eleven matched. Wrong types, a missing field and an extra field were rejected on both sides. Only the two boundary inputs split:
| Input | Python (Pydantic) | TypeScript (Zod) |
|---|---|---|
900.0 |
rejected | accepted |
9007199254740993 |
over capacity | rejected |
These inputs were picked to sit on the edges, so "2 out of 11" says nothing about how often this happens with real traffic.
Neither library is wrong. The values were already different by the time the validators saw them. Akari printed what each language's standard JSON reader returns for the two inputs.
json module returns a float. In strict mode Pydantic accepts only an actual int for an 900.0 and 900 are the same value, and Zod sees a plain integer.Number.MAX_SAFE_INTEGER), so JSON.parse rounds it to 9007199254740992. The Zod docs say z.int() "restricts to safe integer range", and the rounded value is outside it.
Both libraries behave as documented. The rule "positive integer" left two questions open: is a number written with a decimal point allowed, and how large can it be?
Answering those two questions should make the sides agree. There are two reasonable answers to the first, so Akari built both. Each fix also adds the same upper bound, 1,000,000, on both sides, so large integers are stopped before the languages can disagree about them. Each fix was run against the original eleven inputs plus three more aimed at the fixes: 9e2, 1000000 and 1000001.
Treat 900.0 as 900. Only the Python side changes: a Pydantic BeforeValidator, which runs before Pydantic's own validation, turns a float with no fractional part into an int, and strict mode checks the result.
def float_to_int_if_whole(v):
if isinstance(v, float) and v.is_integer():
return int(v)
return v
quantity: Annotated[int, BeforeValidator(float_to_int_if_whole), Field(gt=0, le=1_000_000)]
All fourteen answers match. 900.0 and 9e2 are accepted on both sides, and 9007199254740993 is rejected on both sides: it is above the 1,000,000 bound, and on the TypeScript side the rounded value is also outside the safe integer range.
Reject 900.0 on both sides. Only the TypeScript side changes. The reviver passed to JSON.parse receives the raw source text of each primitive in context.source. If the text for quantity has a decimal point or an exponent, the reviver replaces it with a string, which Zod then rejects.
JSON.parse(text, function (key, value, context?: { source?: string }) {
if (key === 'quantity' && typeof value === 'number' && !/^-?\d+$/.test(context?.source ?? '')) {
return `not-digits:${context?.source}`;
}
return value;
});
All fourteen match again. The only difference from fix A is that 900.0 and 9e2 are now rejected on both sides.
The test shows that either fix works. Which one to use is a business rule, not a library setting:
When you ask an AI to move code from one language to another, ask it for this kind of table too: the same JSON run through the old side and the new side. The rows marked DIFF are what a person needs to look at.
In this series, Akari, our AI employee, runs software and services herself and separates what works as-is from what still needs a human decision.
If you are interested, please like and follow.
Our products are listed at https://www.eln.ne.jp/products
EarthLink Network builds its own products to run the whole company on AI — each one born from a need along the way. See the full index of what we build:
→ The 18 products EarthLink Network builds in-house
For more about the company and each product, visit www.eln.ne.jp.