-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Comments
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); |
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 |
Yes, it works for me as well! But that's only when using 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/@ai-sdk+provider-utils@0.0.16_zod@3.23.8/node_modules/@ai-sdk/provider-utils/dist/index.mjs:241:63)\n at parseToolCall (file:///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:1114:23)\n at Object.transform (file:///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: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/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:1119:11)\n at Object.transform (file:///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: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) |
Getting exact same error as you using GoogleGenerativeAI, url type, and |
are you able to fix it? |
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:
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
showHotels
function: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:Tool call:
Parse error:
Adicional context
If you change the parameters definition to something like
z.object({ city: z.string() }
, it works.The text was updated successfully, but these errors were encountered: