{"slug": "same-positive-integer-rule-different-precision-pydantic-and-zod-split-because-is", "title": "Same positive-integer rule, different precision: Pydantic and Zod split because JSON is read differently before validation", "summary": "An AI employee named Akari ran eleven JSON inputs through Pydantic and Zod using the same \"positive integer\" rule and found nine matched while two boundary cases split: 900.0 is rejected by Pydantic but accepted by Zod, and 9007199254740993 passes Pydantic's validation but fails Zod's. The divergence originates in each language's JSON reader rather than the validators, since JavaScript numbers lose integer precision above 2^53 and Python retains 900.0 as a float. Akari then built two fixes, a Pydantic BeforeValidator that converts whole floats to ints and a JSON.parse reviver that inspects raw source text, each adding a shared 1,000,000 upper bound, and verified all fourteen test inputs matched.", "body_md": "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.\n\n**Series contents**\n\n`{\"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.\n\nThe rule, written once for each library:\n\n```\nclass Order(BaseModel):\n    model_config = ConfigDict(strict=True, extra=\"forbid\")\n    quantity: int = Field(gt=0)\njs\nconst Order = z.strictObject({ quantity: z.int().positive() });\n```\n\nStrict 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\".\n\nAkari ran all eleven inputs through both sides and printed the answers side by side.\n\nNine of the eleven matched. Wrong types, a missing field and an extra field were rejected on both sides. Only the two boundary inputs split:\n\n| Input | Python (Pydantic) | TypeScript (Zod) | \n|---|---|---|\n| `900.0` | rejected | accepted | \n| `9007199254740993` | over capacity | rejected | \n\nThese inputs were picked to sit on the edges, so \"2 out of 11\" says nothing about how often this happens with real traffic.\n\nNeither 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.\n\n`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.\nBoth 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?\n\nAnswering 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`.\n\nTreat `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.\n\n``` python\ndef float_to_int_if_whole(v):\n    if isinstance(v, float) and v.is_integer():\n        return int(v)\n    return v\n\nquantity: Annotated[int, BeforeValidator(float_to_int_if_whole), Field(gt=0, le=1_000_000)]\n```\n\nAll 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.\n\nReject `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.\n\n```\nJSON.parse(text, function (key, value, context?: { source?: string }) {\n  if (key === 'quantity' && typeof value === 'number' && !/^-?\\d+$/.test(context?.source ?? '')) {\n    return `not-digits:${context?.source}`;\n  }\n  return value;\n});\n```\n\nAll fourteen match again. The only difference from fix A is that `900.0` and `9e2` are now rejected on both sides.\n\nThe test shows that either fix works. Which one to use is a business rule, not a library setting:\n\nWhen 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.\n\nIn this series, Akari, our AI employee, runs software and services herself and separates what works as-is from what still needs a human decision.\n\nIf you are interested, please like and follow.\n\nOur products are listed at [https://www.eln.ne.jp/products](https://www.eln.ne.jp/products)\n\n**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:\n\n→ [The 18 products EarthLink Network builds in-house](https://dev.to/uehara/the-18-products-earthlink-network-builds-in-house-hb1)\n\nFor more about the company and each product, visit [www.eln.ne.jp](https://www.eln.ne.jp).", "url": "https://wpnews.pro/news/same-positive-integer-rule-different-precision-pydantic-and-zod-split-because-is", "canonical_source": "https://dev.to/uehara/same-positive-integer-rule-different-precision-pydantic-and-zod-split-because-json-is-read-2npp", "published_at": "2026-11-27 01:02:43+00:00", "updated_at": "2026-11-27 01:14:33.822555+00:00", "lang": "en", "topics": ["developer-tools"], "entities": ["Pydantic", "Zod", "Python", "JavaScript", "TypeScript", "Akari"], "also_reported_by": [], "takeaway": "Adding a shared 1,000,000 upper bound plus a Pydantic BeforeValidator that converts whole floats to ints made all fourteen test inputs match between Pydantic and Zod.", "recommendation": {"advice": "Bound integer fields on both sides and normalize whole floats to ints before strict validation, since JSON readers in Python and JavaScript disagree on 900.0 and values above 2^53.", "playbook": "output-verification", "playbook_url": "https://wpnews.pro/playbooks/output-verification"}, "alternates": {"html": "https://wpnews.pro/news/same-positive-integer-rule-different-precision-pydantic-and-zod-split-because-is", "markdown": "https://wpnews.pro/news/same-positive-integer-rule-different-precision-pydantic-and-zod-split-because-is.md", "text": "https://wpnews.pro/news/same-positive-integer-rule-different-precision-pydantic-and-zod-split-because-is.txt", "jsonld": "https://wpnews.pro/news/same-positive-integer-rule-different-precision-pydantic-and-zod-split-because-is.jsonld"}}