-
Notifications
You must be signed in to change notification settings - Fork 146
feat: add comprehensive zod schema support #388
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
err there might be a handful of cases that I might be overlooking, but hopefully it should be a good scaffold for supporting Zod. |
I've gotten a lot of feedback to support zod and structured extraction like enforcing schema's with json objects etc. While I'm also a user of zod people have pointed me to other libs like https://github.com/ianstormtaylor/superstruct and said we should support. I'm ok with being opinionated and just pick zod since it's the most popular. |
I guess the big question I have is whats the point of generating signatures from zod if zod validation (eg .min(10)) is not being used? We already have two apis for generating signatures the Not against this just trying to understand the usecase better cause I know people will just say don't convert to signature (no downgrading) and just use zod validation for input's and outputs. |
Likewise, I agree. There are things like This PR would likely only cover stories such as if they are migrating from something like Mastra workflows (that is Zod heavy) and they wish to convert it to signatures rather than duplicate schemas. Although, I don't think I made a public round-trip API to convert signatures -> Zod, like DSPy signatures -> Pydantic yet. Let me add those in a bit just for reference. If the use-case is more first-class and foundational support for Zod, that's probably where we begin to diverge from the simple DSPy and begin to piggyback some of the more niceties of Zod. I do have a |
we do have dspy assertions maybe the zod config should be stored in the signature and then the internal signature validator can still run for the streaming validation and the zod one on the final result. this would be even better if we can call the validator of each field in the zod object as the field become available. that way it can work with streaming. (i don't know if we can do that with zod) |
i'm open to deep integration with zod. |
What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)
Feature + tooling polish: end-to-end Zod schema support, downgrade diagnostics, docs/examples, and several conversion bug fixes.
What is the current behavior? (You can also link to an open issue here)
Ax signatures must be authored manually and existing Zod schemas can’t be reused. Any fallbacks (records/maps/unions/transforms) happen silently, array optionality leaks from element wrappers, and there’s no discoverable guidance on Zod → Ax mapping. Issue Standard Schema for Signature Input/Output? #348 captures the gap.
What is the new behavior (if this is a feature change)?
•
AxSignature.fromZod
converts Zod input/output objects (v3/v4) into signatures with full type inference and downgrade telemetry.•
AxSignature.debugZodConversion
,getZodConversionIssues
, andreportZodConversionIssues
expose conversion metadata; strict mode now throws with actionable suggestions.• Conversion logic handles primitives, literals/enums/native enums, branded schemas, nested arrays, discriminated unions, records/maps, and now explicitly flags effects/pipe transformations so runtime-altering schemas don’t masquerade as primitives.
• Fixed optional-element arrays leaking optionality, stabilized literal/class downgrade handling, and re-exported the new
AxSignatureFromZodOptions
.• Docs (
README.md
,docs/SIGNATURES.md
) outline the mapping matrix, downgrade semantics, and auditing workflow.• Added
src/examples/zod-signature-example.ts
(console demo) andsrc/examples/zod-live-example.ts
(OpenAI-backed flow + AxGen usingAxSignature.fromZod
).• Expanded
src/ax/dsp/zodToSignature.test.ts
with comprehensive coverage (supported scenarios, downgrade cases, branded schemas, nested structures, transforms).• Declared a
zod
peer dependency so consumers get a compatible version.Other information:
• Effects/pipes are conservatively downgraded to
json
; if future work unlocks richer Ax types for transformed schemas we can adjust.• Zod constructs that Ax can’t represent yet (e.g.,
ZodFunction
, complex intersections) intentionally fall back tojson
; conversion telemetry makes these visible but they remain unsupported.• Example coverage relies on an
OPENAI_APIKEY
; adding a deterministic fixture-driven smoke test in a follow-up issue would harden CI coverage.• Docs now surface the feature, but discoverability across other guides (e.g., flow recipes) might benefit from cross-links in a future polish pass.
npm run tsx -- src/examples/zod-live-example.ts
npm run tsx -- src/examples/zod-signature-example.ts