dzhng / zod-gpt

Get structured, fully typed, and validated JSON outputs from OpenAI and Anthropic models.
MIT License
574 stars 12 forks source link

support a union of types on the top level #6

Open uriva opened 1 year ago

uriva commented 1 year ago

A common scenario is to have multiple types that each signify an action. This could be translated to different function calls in a natural way.

Say I have action X and action y.

I'd build a type which is like this:

type action = { type: 'x', xParam: number } | { type: 'y', yParam: string }

in zod this would be a z.union.

but the zod-gpt doesn't let you use union and the user needs to resort to something like thiis:

type action = { type: 'x' | 'y', xParam: number, yParam: string } // mixing both actions parameters

this would result in much less type safety, not to mention name collisions that disagree on the property type.

maybe there is a solution I missed?

if not, maybe this is possible to implement by mapping each action to a different function_call.

dzhng commented 11 months ago

Yea, this would be a pattern that should map to a different function call. I haven't got a chance to add this yet, mostly because I don't need this in my day-to-day. Will leave this open to add in the future.