Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for validation libraries other than zod (or no validation) #1062

Closed
yutakobayashidev opened this issue Mar 3, 2024 · 12 comments
Closed
Labels
ai/core ai/rsc enhancement New feature or request

Comments

@yutakobayashidev
Copy link

yutakobayashidev commented Mar 3, 2024

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.

@MaxLeiter
Copy link
Member

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

@lgrammel lgrammel changed the title ai/rsc: Support for validation libraries other than zod Support for validation libraries other than zod May 24, 2024
@lgrammel lgrammel added enhancement New feature or request ai/core ai/rsc labels May 24, 2024
@lgrammel lgrammel changed the title Support for validation libraries other than zod Support for validation libraries other than zod (or no validation) May 24, 2024
@robahtou
Copy link

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.

@lgrammel
Copy link
Collaborator

lgrammel commented Jun 1, 2024

@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.

@robahtou
Copy link

robahtou commented Jun 1, 2024

@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)

@nicolas-angelo
Copy link

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.

@fabian-hiller
Copy link

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.

@lgrammel
Copy link
Collaborator

@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

@fabian-hiller
Copy link

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: fabian-hiller/valibot#373

@lgrammel
Copy link
Collaborator

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.

@fabian-hiller
Copy link

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

@lgrammel
Copy link
Collaborator

Landed in main

@fabian-hiller
Copy link

@lgrammel we released our official toJsonSchema function today. I will probably look into this next week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ai/core ai/rsc enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

6 participants