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

Incorrect args format in tool-call messages when calling functions with no parameters #2103

Open
zvictor opened this issue Jun 25, 2024 · 5 comments

Comments

@zvictor
Copy link

zvictor commented Jun 25, 2024

Description

While porting gemini-chatbot to the Anthropic's provider, I faced an error regarding tool/function calling with no parameters.

Functions that have no parameters lead to tool-call messages in which "args": "" instead of "args": "{}".
Example:

{
  "type": "tool-call",
  "toolCallType": "function",
  "toolCallId": "toolu_01RFn8zb3tbVnyiJ2FjhPqxb",
  "toolName": "showHotels",
  "args": ""
}

AI SDK does not accept that, leading to a AI_JSONParseError: JSON parsing failed: Text: .:

const parseResult = safeParseJSON({
text: toolCall.args,
schema: tool.parameters,
});

Code example

Source definition of the showHotels function:

import { tool } from 'ai'

tool({
  description: 'Show the UI to choose a hotel for the trip.',
  parameters: z.object({})
})

The code is located at lib/chat/tools/showHotels.tsx:14, which is then imported at lib/chat/actions.tsx:220.

JSON definition of the showHotels function:

{
  "type": "function",
  "name": "showHotels",
  "description": "Show the UI to choose a hotel for the trip.",
  "parameters": {
    "type": "object",
    "properties": {},
    "additionalProperties": false,
    "$schema": "http://json-schema.org/draft-07/schema#"
  }
}

Tool call:

{
  "type": "tool-call",
  "toolCallType": "function",
  "toolCallId": "toolu_01RFn8zb3tbVnyiJ2FjhPqxb",
  "toolName": "showHotels",
  "args": ""
}

Parse error:

AI_JSONParseError: JSON parsing failed: Text: .
Error message: Unexpected end of JSON input
    at safeParseJSON (webpack-internal:///(rsc)/./node_modules/.pnpm/@ai-sdk+provider-utils@0.0.16_zod@3.23.8/node_modules/@ai-sdk/provider-utils/dist/index.mjs:271:109)
    at parseToolCall (webpack-internal:///(rsc)/./node_modules/.pnpm/ai@3.2.7_openai@4.52.0_react@18.3.1_solid-js@1.8.17_svelte@4.2.18_vue@3.4.30_typescript@5.5.2__zod@3.23.8/node_modules/ai/dist/index.mjs:1184:92)
    ...

Adicional context

If you change the parameters definition to something like z.object({ city: z.string() }, it works.

@lgrammel
Copy link
Collaborator

Can you provide the tool definition in typescript / AI SDK code?

The following example works for me:

import { anthropic } from '@ai-sdk/anthropic';
import { generateText, tool } from 'ai';
import dotenv from 'dotenv';
import { z } from 'zod';

dotenv.config();

async function main() {
  const result = await generateText({
    model: anthropic('claude-3-opus-20240229'),
    maxTokens: 512,
    tools: {
      showBerlinCityAttractions: tool({
        description: 'Show Berlin city attractions',
        parameters: z.object({}),
        execute: async () => {
          console.log('Showing city attractions');
        },
      }),
    },
    prompt: 'Show Berlin city attractions',
  });

  console.log(JSON.stringify(result, null, 2));
}

main().catch(console.error);

@lgrammel lgrammel removed the bug Something isn't working label Jun 26, 2024
@zvictor
Copy link
Author

zvictor commented Jun 26, 2024

Can you provide the tool definition in typescript / AI SDK code?

import { tool } from 'ai'

tool({
  description: 'Show the UI to choose a hotel for the trip.',
  parameters: z.object({})
})

It's located at lib/chat/tools/showHotels.tsx:14, which is then imported at lib/chat/actions.tsx:220.

If you change the parameters definition to something like z.object({ city: z.string() }, it works.

@zvictor
Copy link
Author

zvictor commented Jun 26, 2024

The following example works for me

Yes, it works for me as well! But that's only when using generateText. If you go for streamText instead, it will throw the error:

Delta {"type":"error","error":{"name":"AI_InvalidToolArgumentsError","message":"Invalid arguments for tool showBerlinCityAttractions: JSON parsing failed: Text: .\nError message: Unexpected end of JSON input","cause":{"name":"AI_JSONParseError","message":"JSON parsing failed: Text: .\nError message: Unexpected end of JSON input","cause":{},"stack":"AI_JSONParseError: JSON parsing failed: Text: .\nError message: Unexpected end of JSON input\n    at safeParseJSON (file:///node_modules/.pnpm/@[email protected][email protected]/node_modules/@ai-sdk/provider-utils/dist/index.mjs:241:63)\n    at parseToolCall (file:///node_modules/.pnpm/[email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected]/node_modules/ai/dist/index.mjs:1114:23)\n    at Object.transform (file:///node_modules/.pnpm/[email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected]/node_modules/ai/dist/index.mjs:1313:30)\n    at invokePromiseCallback (node:internal/webstreams/util:180:10)\n    at Object.transformAlgorithm (node:internal/webstreams/util:185:23)\n    at transformStreamDefaultControllerPerformTransform (node:internal/webstreams/transformstream:526:37)\n    at transformStreamDefaultSinkWriteAlgorithm (node:internal/webstreams/transformstream:572:10)\n    at node:internal/webstreams/transformstream:377:16\n    at writableStreamDefaultControllerProcessWrite (node:internal/webstreams/writablestream:1127:5)\n    at writableStreamDefaultControllerAdvanceQueueIfNeeded (node:internal/webstreams/writablestream:1242:5)","valueText":""},"stack":"AI_InvalidToolArgumentsError: Invalid arguments for tool showBerlinCityAttractions: JSON parsing failed: Text: .\nError message: Unexpected end of JSON input\n    at parseToolCall (file:///node_modules/.pnpm/[email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected]/node_modules/ai/dist/index.mjs:1119:11)\n    at Object.transform (file:///node_modules/.pnpm/[email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected]/node_modules/ai/dist/index.mjs:1313:30)\n    at invokePromiseCallback (node:internal/webstreams/util:180:10)\n    at Object.transformAlgorithm (node:internal/webstreams/util:185:23)\n    at transformStreamDefaultControllerPerformTransform (node:internal/webstreams/transformstream:526:37)\n    at transformStreamDefaultSinkWriteAlgorithm (node:internal/webstreams/transformstream:572:10)\n    at node:internal/webstreams/transformstream:377:16\n    at writableStreamDefaultControllerProcessWrite (node:internal/webstreams/writablestream:1127:5)\n    at writableStreamDefaultControllerAdvanceQueueIfNeeded (node:internal/webstreams/writablestream:1242:5)\n    at writableStreamDefaultControllerWrite (node:internal/webstreams/writablestream:1116:3)","toolName":"showBerlinCityAttractions","toolArgs":""}}
Delta {"type":"finish","finishReason":"tool-calls","usage":{"promptTokens":346,"completionTokens":42,"totalTokens":388}}

Try this:

import { anthropic } from '@ai-sdk/anthropic'
import { streamText, tool } from 'ai'
import dotenv from 'dotenv'
import { z } from 'zod'

dotenv.config()

async function main() {
  const result = await streamText({
    model: anthropic('claude-3-haiku-20240307'),
    maxTokens: 512,
    tools: {
      showBerlinCityAttractions: tool({
        description: 'Show Berlin city attractions',
        parameters: z.object({}),
        execute: async () => {
          console.log('Showing city attractions')
        }
      })
    },
    prompt: 'Show Berlin city attractions'
  })

  for await (const delta of result.fullStream) {
    console.log(`Delta`, JSON.stringify(delta))
  }
}

main().catch(console.error)

@Legit4K
Copy link

Legit4K commented Jun 26, 2024

bumping this as I'm also experiencing an error when I try to use an empty object via zod for a tool's parameters. when I define a parameter for the tool, everything works as expected.

URL type parameters are also no longer working:
image
image

Additional Context

I'm using the Google Generative API provider and I haven't made any changes over the past 2 days. Everything was stable until earlier today for me. Btw - i'm using generateText

@mnalley95
Copy link

mnalley95 commented Jun 27, 2024

bumping this as I'm also experiencing an error when I try to use an empty object via zod for a tool's parameters. when I define a parameter for the tool, everything works as expected.

URL type parameters are also no longer working:

image

image

Additional Context

I'm using the Google Generative API provider and I haven't made any changes over the past 2 days. Everything was stable until earlier today for me. Btw - i'm using generateText

Getting exact same error as you using GoogleGenerativeAI, url type, and streamText. Was also working fine 2-3 days ago, then am getting the exact same error today.

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

No branches or pull requests

4 participants