Use Cases
Simple, copy‑pasteable examples for common situations.
Each section explains:
- What you’re trying to do
- The command to run
- How to use the result
The examples are short on purpose so you can read them fast and still trust them in a serious project.
1. TS → Python (one schema)
Section titled “1. TS → Python (one schema)”- Goal: You have a Zod schema in a TypeScript app and want a matching Pydantic model in Python.
- Write the schema in TypeScript (
schema.ts):
import { z } from 'zod';
export const userSchema = z.object({ id: z.string().uuid(), email: z.string().email(), createdAt: z.date(),});- Generate the Python model:
npx schemabridge convert zod path/to/schema.ts \ --export userSchema \ --to pydantic \ --out path/to/user.py- Change
path/to/schema.tsto the path of your.tsfile. - Change
userSchemaif your exported schema has a different name. - Change
path/to/user.pyto where you want the Python file to be written.
- Use it in Python:
from user import UserSchema
user = UserSchema( id="550e8400-e29b-41d4-a716-446655440000", email="user@example.com", createdAt="2024-01-01T00:00:00Z",)2. Monorepo → Python service
Section titled “2. Monorepo → Python service”- Goal: You have a folder of Zod schemas in a TypeScript package and you want Python models for all of them.
Run this from the root of your monorepo:
schemabridge convert folder ./packages/schemas \ --out ./services/api/models \ --to pydantic \ --init- Change
./packages/schemasto the folder that contains your Zod schemas. - Change
./services/api/modelsto the folder where you want Python models.
Then in Python:
from services.api.models.user import UserSchemafrom services.api.models.order import OrderSchema3. Python app using a TS package
Section titled “3. Python app using a TS package”- Goal: Your Zod schemas live in a TypeScript package, but your main app is in Python.
- Install the package that contains the schemas:
pnpm add shared-schemas- Create a small wrapper file that re‑exports the Zod schemas (
schemas.ts):
export { userSchema } from 'shared-schemas/user';export { orderSchema } from 'shared-schemas/order';- Generate Python models into your Python project:
schemabridge convert folder ./node_modules/shared-schemas \ --out ./python/models \ --to pydantic \ --init \ --allow-unresolved- Change
./node_modules/shared-schemasif your package lives in a different folder. - Change
./python/modelsto the folder where you keep generated Python code.
- Use the models in Python:
from python.models.user_schema import UserSchema4. TypeScript only (.d.ts types)
Section titled “4. TypeScript only (.d.ts types)”- Goal: You use Zod in a front‑end or Node app and want
.d.tstypes that always match the schemas.
For one file:
schemabridge convert zod path/to/schema.ts \ --export userSchema \ --to typescript \ --out path/to/user.d.tsFor a folder of schemas:
schemabridge convert folder ./src/schemas \ --out ./src/types \ --to typescript- Change the paths so they match your project layout.
- Change
userSchemaif your export has a different name.
Then import the types in your app:
import type { User } from './types/user';5. CI / build step
Section titled “5. CI / build step”- Goal: Make sure generated Python models are always up to date whenever Zod schemas change.
Add a script (scripts/generate-models.ts):
import { convertFolder } from 'schemabridge';
await convertFolder({ sourceDir: './src/schemas', outDir: './python/models', target: 'pydantic', preserveStructure: true, generateInitFiles: true,});Wire it into package.json:
{ "scripts": { "generate:models": "tsx scripts/generate-models.ts" }}Example CI job:
pnpm installpnpm generate:modelspytest # or your Python test runner6. One‑off conversion
Section titled “6. One‑off conversion”- Goal: You just want to try a schema and get Python code once, without wiring anything into a project.
- Create a scratch file (
tmp-schema.ts):
import { z } from 'zod';
export const payloadSchema = z.object({ id: z.string().uuid(), amount: z.number(), createdAt: z.date(),});- Convert and look at the output:
schemabridge convert zod tmp-schema.ts --export payloadSchema --to pydantic --out payload.pycat payload.py7. Nested schemas and enums
Section titled “7. Nested schemas and enums”- Goal: See how enums and nested objects in Zod appear in Python and TypeScript.
Zod input:
export const roleSchema = z.enum(['admin', 'viewer']);
export const userSchema = z.object({ id: z.string().uuid(), role: roleSchema,});Command:
schemabridge convert zod path/to/schema.ts \ --export userSchema \ --to all \ --out ./generated- Change
path/to/schema.tsto your file. - Change
userSchemato your export name if it is different. - Change
./generatedto the output folder you want.
You’ll get:
- Python: a
Literal["admin", "viewer"]field forrole. - TypeScript: a
"admin" | "viewer"union type forrole.
Enums always map this way; nested objects become nested classes/interfaces.
If your situation is a bit different, pick the example that is closest, adjust the paths, and change --to or the flags you need.
For full option lists, see the CLI Guide and API Reference.