vercel / ai

Build AI-powered applications with React, Svelte, Vue, and Solid
https://sdk.vercel.ai/docs
Other
10.26k stars 1.55k forks source link

GoogleGenerativeAI crashes when returning value from tool call #2711

Open jensroth-git opened 3 months ago

jensroth-git commented 3 months ago

Description

Getting the following exception when providing a return value from a tool call when using the google generative ai provider.

Uncaught APICallError AI_APICallError: Invalid value at 'contents[2].parts[0].function_response.response' (type.googleapis.com/google.protobuf.Struct), 25

Code example

import { createGoogleGenerativeAI } from '@ai-sdk/google';
import { generateText } from 'ai';
import { z } from 'zod';

async function main()
{
    let google = createGoogleGenerativeAI({
        apiKey: GoogleAIKey
    });

    let gemini = google("models/gemini-1.5-pro-latest");

    let result = await generateText({
        model: gemini,
        tools: {
            getTemperature: {
                description: "get the current temperature in a city",
                parameters: z.object({
                    city: z.string().describe("the city to get the temperature for")
                }),
                execute: async ({ city }) =>
                {
                    console.log("getting temperature for " + city);
                    return 25;
                }
            },
        },
        prompt: "Get the temperature in Berlin",
        maxToolRoundtrips: 1
    });

    console.log(result.text);
}

main();

Additional context

No response

lgrammel commented 3 months ago

It turns out that you cannot return number values with Gemini. The following works:

execute: async ({ city }) => {
  console.log('getting temperature for ' + city);
  return {
    temperature: 25,
  };
},
Clement-Martzloff commented 3 weeks ago

It works for me if I return an object, even if I do not define some sort of type verification somewhere... image