Skip to content

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

Description

@zvictor

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ai/providerrelated to a provider package. Must be assigned together with at least one `provider/*` labelbugSomething isn't working as documentedtask-identify-issue-type-done

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions