ai icon indicating copy to clipboard operation
ai copied to clipboard

v3.0.1 render function in submitUserMessage not parsing parameters correctly

Open stephenkalnoske-sans opened this issue 11 months ago • 4 comments

Description

The generator function for render has the parameters as a string instead of a parsed JSON object. The way it is in the docs right now will not work, have to manually take the string and parse it. In the code example, I put what works for me.

Code example

tools: {
      get_course_info: {
        description: 'Get available courses for the student to choose in order to get information or tutoring',
        parameters: z.object({
          studentId: z.string().describe('The studentID of the student requesting the course information or tutoring')
        }).required(),
        render: async function* (args) {
          yield <div>Loading</div>

          const { studentId } = JSON.parse(args as unknown as string);
          console.log(studentId) // This is the correct 

          // Promise that gets resolved in 2 seconds
          await new Promise((resolve) => setTimeout(resolve, 2000));
          
          aiState.done([
            ...aiState.get(),
            {
              role: "assistant",
              content: `available courses for student ${studentId} are SEC401 and SEC504`
            }
          ]);
 
          return (
            <CoursePickerComponent courses={["SEC401", "SEC504"]} />
          )
        }
      }
    }

Additional context

No response

stephenkalnoske-sans avatar Mar 01 '24 21:03 stephenkalnoske-sans

Noticed the same. Since Zod is used to define the json schema it would be also nice if the parameters would be parsed by Zod. This way one would be certain that the parameters are actually what have been defined in the schema.

valstu avatar Mar 03 '24 13:03 valstu

In case of validation errors the Zod error messages could be passed back to the language model which usually handles them quite nicely and fixes the function call parameters. I used this method when I built internal UI rendering and it seemed to be pretty good solution.

Another option is that typing of the args is changed to string and args validation would happen on user land 🤷 ?

valstu avatar Mar 03 '24 14:03 valstu

Was stuck diagnosing this for an hour as the parameters were returning undefined. Manually parsing args to an object fixed the issue.

sajadmh avatar Mar 04 '24 07:03 sajadmh

Seems to be fixed here, not yet released, as I still experience it in 3.0.5

https://github.com/vercel/ai/commit/a09abd09d0fb35a354ed2b44517e6a2f2dd31d1b

UPDATE: never mind, was reverted

mharris717 avatar Mar 05 '24 23:03 mharris717