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.
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-callmessages 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: .:ai/packages/core/core/generate-text/tool-call.ts
Lines 68 to 71 in 55a2368
Code example
Source definition of the
showHotelsfunction: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
showHotelsfunction:{ "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:
Adicional context
If you change the parameters definition to something like
z.object({ city: z.string() }, it works.