Schema-first types
z.infer flows from the Zod schema straight into the resolved value. One source of truth, zero hand-written interfaces.
Define the expected shape with Zod. zodstructor turns free-form model output into validated, strongly typed data — with provider-native structured output, tool calling, and two independent retry layers.
A thin, testable layer that turns probabilistic text into deterministic data.
z.infer flows from the Zod schema straight into the resolved value. One source of truth, zero hand-written interfaces.
json_schema, tool_call, json_object, prompt, or auto — pick the strongest guarantee each provider actually supports.
Validation retries feed Zod issues back to the model for self-repair; transport retries back off independently on network failures.
Stream raw text as it arrives, render best-effort partial previews, and still finish with full schema validation.
OpenAI chat.completions, OpenAI Responses, OpenAI-compatible gateways, Anthropic, and Google Gemini.
Optional, default and catch fields become anyOf [T, null] in strict object mode — no silent schema drift.
Each mode trades compatibility against enforcement strength. Explicit beats auto.
| Mode | Behavior | Best for | Guarantee |
|---|---|---|---|
json_schema |
Provider-native schema enforcement where available | Highest guarantee on OpenAI structured output | |
tool_call |
Function / tool calling; arguments validated as JSON | Tool-native models and agent workflows | |
json_object |
JSON-only response without native schema enforcement | Compatible gateways with JSON mode | |
prompt |
Prompt injection only | Legacy or limited OpenAI-compatible endpoints | |
auto |
Backward-compatible default; prefer explicit modes in new code | Migration path |
Install the core, then only the provider SDKs you actually use.
zodstructor plus its peer, zod.
npm install zodstructor zod
Each provider is an optional peer dependency.
npm install openainpm install @anthropic-ai/sdknpm install @google/generative-aiImport the client and run your first completion.
npx tsc --noEmit && npm test
A simulated run of the real pipeline: request → streamed raw text → Zod validation → typed value.
Validation retries feed Zod issues back to the model so it can repair its own output. Transport retries use exponential backoff for network and HTTP failures — independently tunable.
During streaming, partial previews close unterminated strings and brackets without ever inventing fields; the final value still goes through full schema validation.
Every subsystem has its own deep-dive, in English and Chinese.
The completion pipeline, validation repair loop, and transport backoff.
Strict schema generation, anyOf nullables, and provider quirks.
OpenAI, Responses API, Anthropic, Gemini, and compatible gateways.
Truncated-JSON repair and how final validation stays authoritative.
Unit coverage plus the real-provider integration matrix.
The trade-offs and invariants behind the public API.
Straight answers about what zodstructor does and how it works.
zodstructor is an open-source TypeScript library for structured LLM output. You define the expected shape with a Zod schema, and it converts free-form model output into validated, strongly typed data whose TypeScript type is inferred directly from the schema.
Five: OpenAI chat.completions, the OpenAI Responses API, OpenAI-compatible gateways, Anthropic, and Google Gemini. Each provider SDK is an optional peer dependency, so you only install what you use.
Validation retries feed Zod issues back to the model so it can repair its own output (maxRetries). Transport retries handle network and HTTP failures independently with exponential backoff (transportRetries, retryDelayMs). The two layers are configured and counted separately.
json_schema uses provider-native schema enforcement for the strongest guarantee on supported OpenAI endpoints. tool_call routes through function/tool calling and validates the tool arguments as JSON, which suits tool-native models and agent workflows. json_object and prompt are weaker modes for compatible or legacy gateways.
Yes. streamStructured yields raw text as it arrives and can emit best-effort partial previews through partialSchema — the preview only closes unterminated strings and brackets and never invents fields. The final value always goes through full Zod validation.
zodstructor is in beta: the core path is tested and buildable, and real-provider compatibility should be gated by the integration matrix before production rollout. It is MIT licensed and free to use.
One Zod schema. Typed data, every time.