-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Fix Zod v4 schema description extraction #1296
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
Conversation
commit: |
Zod v4 stores descriptions in `z.globalRegistry`, not in `._zod.def.description`. Both v3 and v4 expose a `.description` getter, so simplify to just use that. Also removes the incorrect `description` field from `ZodV4Internal` interface. Fixes #1277
9f891fb to
1a0a906
Compare
| } | ||
| const v3Schema = schema as unknown as ZodV3Internal; | ||
| // v3 may have description on the schema itself or in _def | ||
| return (schema as { description?: string }).description ?? v3Schema._def?.description; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ?? v3Schema._def?.description is unnecessary because ZodType on v3 already has this getter: https://github.com/colinhacks/zod/blob/main/packages/zod/src/v3/types.ts#L164-L166
get description(): string | undefined {
return this._def.description;
}
| if (isZ4Schema(schema)) { | ||
| const v4Schema = schema as unknown as ZodV4Internal; | ||
| return v4Schema._zod?.def?.description; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In v4 .describe() stores in globalRegistry with the .description getter reading from it directly via v4Schema.description rather than on _def so we don't need this.
https://github.com/colinhacks/zod/blob/main/packages/zod/src/v4/classic/schemas.ts#L223-L230
|
@dclark27 @colinhacks @mattzcarey in case I'm missing any edge cases you were thinking of here with this helper - but it looks to me like branching on zod version might have been unnecessary here? |
mattzcarey
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
Zod v4 stores descriptions in
z.globalRegistry, not in._zod.def.descriptionas the original implementation assumed. Both v3 and v4 expose a.descriptiongetter, so this simplifiesgetSchemaDescription()to just use that.Motivation and Context
Users registering prompts with described arguments were not seeing the descriptions in the output:
Root cause: The original v4 implementation looked for descriptions in
._zod.def.description, but this property doesn't exist in Zod v4.See: https://github.com/colinhacks/zod/blob/main/packages/zod/src/v4/classic/schemas.ts shows the
.descriptiongetter reads from the global registry:The https://zod.dev/metadata confirm this architecture:
So the correct way to access descriptions is via the
.descriptiongetter (which internally reads from the registry), not._zod.def.description(which doesn't exist).How Has This Been Tested?
Breaking Changes
None - this is a bug fix.
Types of changes
Checklist
Additional context
Fixes #1277