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

feat(ai): add support for Valibot schemas #3015

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

fabian-hiller
Copy link

This is a draft PR as there are a few questions that need to be answered before I can finalize the implementation.

The initial idea is to support Valibot for the parameters property in addition to Zod. Valibot has seen extreme growth in recent months, increasing the likelihood that users of the AI SDK will want to use Valibot instead of raw JSON Schema or Zod. Our library offers the advantage that the API design is modular, and schemas typically require only a few hundred bytes.

To make Valibot a great match with AI tools (we are also in exchange with OpenAI) we added a bunch of new features in the last weeks. Valibot now supports a title and description metadata action that is compatible with JSON Schema. Furthermore, we implemented an official toJsonSchema function that reliably converts our schemas to JSON Schema format.

Valibot's API seems to be stable. We are just waiting with our v1 RC release as there are efforts in the background to make Zod, Valibot and other schema libraries more compatible with common interface properties, which would result in easier integration of these schema libraries.

This PR adds a valibotSchema function to the ui-utils package that works similar to zodSchema. However, it is unclear to me whether it is preferable to export valibotSchema in the ai package or to integrate it into the asSchema function. The latter would allow users to pass Valibot schemas directly to parameters without a wrapper function, resulting in a smoother DX.

import { generateText, tool } from 'ai';
import * as v from 'valibot';

const result = await generateText({
  model: yourModel,
  tools: {
    weather: tool({
      description: 'Get the weather in a location',
      parameters: v.object({
        location: v.pipe(
          v.string(),
          v.describe('The location to get the weather for')
        ),
      }),
      execute: async ({ location }) => ({
        location,
        temperature: 72 + Math.floor(Math.random() * 21) - 10,
      }),
    }),
  },
  toolChoice: 'required',
  prompt: '...',
});

Copy link

socket-security bot commented Sep 15, 2024

New and removed dependencies detected. Learn more about Socket for GitHub ↗︎

Package New capabilities Transitives Size Publisher
npm/@valibot/[email protected] None 0 44.4 kB fabian-hiller
npm/[email protected] environment, filesystem, network, shell, unsafe +10 98.9 MB vercel-release-bot
npm/[email protected] None +1 6.4 MB fb, gnoff, react-bot, ...2 more
npm/[email protected] environment 0 237 kB react-bot
npm/[email protected] None 0 1.46 MB fabian-hiller

🚮 Removed packages: npm/[email protected], npm/[email protected], npm/[email protected]

View full report↗︎

Copy link

socket-security bot commented Sep 15, 2024

👍 Dependency issues cleared. Learn more about Socket for GitHub ↗︎

This PR previously contained dependency changes with security issues that have been resolved, removed, or ignored.

View full report↗︎

@lgrammel
Copy link
Collaborator

My preferred way would be to have a separate @ai-sdk/valibot package for now. I'll look into this tomorrow.

@fabian-hiller
Copy link
Author

Thank you for your response. I recommend waiting a few days before taking any action, as we are currently trying to standardize a common interface for TS schema libraries. If our ideas are well received by other schema libraries as well as libraries like the Vercel AI SDK, the integration of schema libraries will become much easier and less complicated.

@lgrammel
Copy link
Collaborator

@fabian-hiller do you have a timeline for the standardization? I could also just release the valibot integration now and then update once the standard is out.

@fabian-hiller
Copy link
Author

We are trying to release a v0 or v1 of the Standard Schema this week. I will keep you posted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants