ai icon indicating copy to clipboard operation
ai copied to clipboard

ai/rsc: Support for validation libraries other than zod

Open yutakobayashidev opened this issue 1 year ago • 1 comments

Feature Description

Currently, the schema for tools and functions is created using zod and zod-to-json-schema. However, the bundle size of zod is quite large, and it would be beneficial to support other validation libraries, such as valibot.

If this idea is well-received, I can create a pull request to implement the support for additional validation libraries.

yutakobayashidev avatar Mar 03 '24 08:03 yutakobayashidev

If you have a solution for supporting other libraries without sacrificing (too much of) the current DX then we might be able to ship it. As it is, zod should only be bundled on the server (and users likely have it available already), so we're not too worried about the bundle size.

Can you make a PoC with what you're thinking about? cc @lgrammel

MaxLeiter avatar Mar 03 '24 19:03 MaxLeiter

I think it is a good idea to support other validation libraries. Currently we use Joi everywhere (front & backend) and now we have to support zod for our ai use cases. There is another team that uses exclusively ajv.

robahtou avatar May 31 '24 23:05 robahtou

@robahtou many validation libraries are limited in the json schema conversion (the LLM use case is a bit more than just validation). zod has the advantage that it supports describe(), which has the benefit of adding descriptions to the json schema that give the LLMs more context on how to use tools / generate objects. i have only looked at valibot so far, which is limited in that regard.

lgrammel avatar Jun 01 '24 07:06 lgrammel

@lgrammel makes sense. In that case ajv json schema supports the keyword description (https://ajv.js.org/json-schema.html#metadata-keywords) and here is an example https://json-schema.org/learn/miscellaneous-examples#basic. (ajv follows the json schema spec)

robahtou avatar Jun 01 '24 12:06 robahtou

Also looking for this, ideally without enforcing the use of a validation library since we are allowing dynamic schema and pulling it in at runtime.

nicolas-angelo avatar Jun 03 '24 21:06 nicolas-angelo

I am the author of Valibot and am happy to help and answer questions. Since we are getting close to v1, it might make sense to start this process now.

fabian-hiller avatar Jun 13 '24 10:06 fabian-hiller

@fabian-hiller the crucial thing is json schema support (ideally with descriptions for individual properties), and a good match to what's possible in json schema (since that's what most llms use). the ai sdk uses the validation library for both validation and json schema generation

lgrammel avatar Jun 13 '24 11:06 lgrammel

There is a JSON schema community library similar to Zod: https://github.com/gcornut/valibot-json-schema

We are working on a metadata feature in the next few weeks to add custom information such as a description: https://github.com/fabian-hiller/valibot/issues/373

fabian-hiller avatar Jun 13 '24 12:06 fabian-hiller

Implemented in #2386

@fabian-hiller you could add support for valibot through a separate package that offers a function which takes a valibot schema and maps it to a AI SDK compatible JSON schema via the jsonSchema function.

lgrammel avatar Jul 24 '24 08:07 lgrammel

Thanks @lgrammel! I will take a closer look at this after we release our metadata feature.

fabian-hiller avatar Jul 24 '24 09:07 fabian-hiller

Landed in main

lgrammel avatar Jul 24 '24 09:07 lgrammel