Using zod union with generateObject #5089
-
hi folks, trying to use
inspecting the schema that zod generates in this case:
but apparently this does not work well with AI SDK when using gemini:
or gpt4, albeit with a different error message:
behavior is the same whether i use however, using a schema that does not use an union sort of works:
i'm trying to re-use schemas I have elsewhere in the app so would like to avoid having to translate these objects. any idea what's going on? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Models don't have full support for all Zod schemas. There are several layers involved: Zod to JSON schema, and then the JSON schema support that the LLM provider offers / the LLM supports. Ideally the top level should be an object. Even with that though, there are limitations (especially in the union case, or when schemas get large) |
Beta Was this translation helpful? Give feedback.
-
Having possibility to use union would be very helpful. |
Beta Was this translation helpful? Give feedback.
Models don't have full support for all Zod schemas. There are several layers involved: Zod to JSON schema, and then the JSON schema support that the LLM provider offers / the LLM supports. Ideally the top level should be an object. Even with that though, there are limitations (especially in the union case, or when schemas get large)