vercel / ai

Build AI-powered applications with React, Svelte, Vue, and Solid
https://sdk.vercel.ai/docs
Other
10.35k stars 1.56k forks source link

ai/core: support reason + "array" #2947

Open bernaferrari opened 2 months ago

bernaferrari commented 2 months ago

Feature Description

Sequel to https://github.com/vercel/ai/issues/1486.

I wish I could have a reason field before the array, so that if I had

z.object({
reason: z.string(),
arr: z.array(...)
})

The array would stream in chunks and work with array but still allow other fields that are not array. So, ideally,

Could also be like "if something is inside an array, wait for it to finish to stream, else stream naturally"

lgrammel commented 2 months ago

@bernaferrari I don't want to hardcode something like reasoning, but I'd love to figure out a more flexible way to support array streaming. One thought that I had is to use a validator schema for intermediate results (partial result would only be sent if they pass that schema), but this will not solve the "is-object-completed" issue. The "is-object-completed" detection is tricky, because it needs to be specified to which level it applies to. Lmk if you have ideas.