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: define tool parameters with JSON schema instead of Zod #1624

Closed
doowb opened this issue May 16, 2024 · 4 comments
Closed

feat: define tool parameters with JSON schema instead of Zod #1624

doowb opened this issue May 16, 2024 · 4 comments
Labels
ai/core enhancement New feature or request

Comments

@doowb
Copy link

doowb commented May 16, 2024

Description

I'm trying to use streamText with a plain tools object where the parameters on each tool valid JSON schema. This works fine when I use the openai sdk (with a tools array), but I get the following error before the request is even made:

TypeError: Cannot read properties of undefined (reading 'typeName')

Code example

import { openai } from '@ai-sdk/openai';
import { streamText } from 'ai';

const response = await streamText({
  model: openai('gpt-4o'),
  prompt: 'This is a prompt',
  tools: {
    my_tool: {
      description: 'A tool that does something',
      parameters: {
        param1: {
          type: 'string',
          description: 'A parameter that does something'
        }
      }
    }
  }
});

Additional context

No response

@nicoalbanese
Copy link
Contributor

nicoalbanese commented May 16, 2024

Hi - sorry you're running into issues! This error is because the SDK uses Zod for the parameters schema.

Try this:

import { openai } from '@ai-sdk/openai';
import { streamText } from 'ai';
import { z } from "zod";

const response = await streamText({
  model: openai('gpt-4o'),
  prompt: 'This is a prompt',
  tools: {
    my_tool: {
      description: 'A tool that does something',
      parameters: z.object({
        param1: z.string().describe('A parameter that does something')
        }),
      }
    }
  }
});

@doowb
Copy link
Author

doowb commented May 16, 2024

Is there anyway to do this without zod? Or would this project be open to supporting JSON Schemas?

I'm mainly interested in this because my tool schemas are JSON Schemas and stored as JSON strings, which I've been using with the openai sdk and the previous stream helpers with ai.

@amcclure-super
Copy link

If helpful, there are some npm libraries like Json-Schema-to-Zod (https://www.npmjs.com/package/json-schema-to-zod) and Zod to Json Schema (https://www.npmjs.com/package/zod-to-json-schema) that might be of use

@lgrammel lgrammel added the enhancement New feature or request label May 17, 2024
@lgrammel lgrammel changed the title TypeError: Cannot read properties of undefined (reading 'typeName') feat: define tool parameters with JSON schema instead of Zod May 17, 2024
@lgrammel
Copy link
Collaborator

Duplicates #1062

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

No branches or pull requests

4 participants