# pi-schema

By Saurabh Kumar · TIL note, Nov 1, 2026 · Tags: javascript, performance

Source: https://saurabh-kumar.com/til/1793524067/

[pi-schema](https://github.com/earendil-works/pi/tree/main/packages/schema) is a JSON Schema validator from the Pi monorepo that never calls `eval` or `new Function`. Mario Zechner says he built it with a single prompt. It compiles each schema into plain closures, so it stays fast in places that block code generation.

- **The need is real.** [Cloudflare Workers](https://developers.cloudflare.com/workers/runtime-apis/web-standards/) block `eval()` and `new Function`. [Chrome MV3 extensions](https://developer.chrome.com/docs/extensions/develop/migrate/improve-security) cannot allow `'unsafe-eval'`. Ajv, the default JSON Schema validator in JS, compiles schemas with `new Function`. It broke MCP elicitation on Workers ([typescript-sdk#689](https://github.com/modelcontextprotocol/typescript-sdk/issues/689)).
- **How people cope today.** Ajv can [generate validators at build time](https://ajv.js.org/standalone), but that only works for schemas you know in advance. The MCP SDK lists [@cfworker/json-schema](https://github.com/cfworker/cfworker/tree/main/packages/json-schema), an interpreter, as a peer dependency. Zod and ArkType have `jitless` flags, and TypeBox falls back to its interpreter. Zod's flag has to be set before any import that uses Zod ([zod#5789](https://github.com/colinhacks/zod/issues/5789)).
- **The numbers.** Without eval, [the README](https://github.com/earendil-works/pi/blob/main/packages/schema/README.md#benchmarks) puts a nested schema at 407 ns for pi-schema, 42 µs for TypeBox and 43 µs for cfworker. pi-schema passes 2,222 of the 2,223 core tests in the official suite.

Agents make this matter. An agent learns its tools from MCP servers while it runs: `tools/list` returns each tool's JSON Schema. You cannot compile those ahead of time, and the harnesses are moving to the edge. Pi's [Chord design notes](https://github.com/earendil-works/pi/blob/main/packages/chord/docs/next/decisions.md) plan a Cloudflare Durable Object driver, and [Pi Durable](https://saurabh-kumar.com/til/1791374400/) already stores agent work as checkpointed tasks. The catches: these are the author's own benchmarks, Ajv is not in the comparison, the package is not on npm yet, and TypeBox's JIT still wins on small flat schemas (9 ns vs 31 ns).
