#Malformed JSON in tool calls

6 messages · Page 1 of 1 (latest)

restive berry
#

out of 10 tool calls, there are around 2 invalid json payloads, even for simple stuff,

[{
    "path": "tests/prefs.json",
    "content": {
  "likes_cats": true,
  "gender": "female"
}

thats with anthropic/claude-3.5-sonnet

and here the tool

#
function: async (ret) => {
                    try {
                        if(!target) {
                            logger.error(`Tool::Structure Path and name are required to create project structure`)
                            return
                        }
                        let { files } = ret as any
                        if(isString(files)){
                            try {
                                files = JSON.parse(files)
                            } catch (error) {                         
                                logger.error(`Tool::Structure Error parsing files`, error, ret)
                                write(path.join(target, 'tools-output.json'), files)
                                return
                            }
                        }
                        for (const file of files) {
                            const filePath = path.join(target, file.path);
                            logger.debug(`Tool:Structure Writing file ${filePath}`)
                            await write(filePath, file.content)
                        }
                    } catch (error) {
                        logger.error(`Error creating project structure`, error)
                    }
                },
                parse: JSON.parse,
#

sometimes it just sends strings 🙂

sacred pilot
#

can you show us how you are sending the tool calling prompt?

restive berry
#

thanks, yes

the params

let _tools = template.tools || []
  const params = {
    model: options.model,
    messages: [
      {
        role: "system",
        content: "You are software engineer :)"
      },
      ...template.messages,
      {
        role: "user",
        content: `Current files:\n${JSON.stringify(files, null, 2)}`
      },
      {
        role: "user",
        content: `${query}`
      }
    ],
    tools: _tools,
    tool_choice: 'auto',
    stream: true
  }
#

the call


let runner = null
  try {
    runner = await client.beta.chat.completions.runTools(params as ChatCompletionToolRunnerParams<any>)
      .on('message', (msg) => {
        if (!msg.content) return
        const messageWithMetadata = {
          ...msg,
          timestamp: new Date().toISOString(),
          sessionId,
          query: options.query
        }
        sessionMessages.messages.push(messageWithMetadata)
      })
      .on('content', (diff) => process.stdout.write(diff))
  } catch (error) {
    logger.error('Failed to create runner:', error.message, error.issues)
    return
  }