Tool calling with OpenAI API not working

I’m trying to use the OpenAI Node.js SDK to use function calling on Gemini using this example: Compatibilité OpenAI  |  Gemini API  |  Google AI for Developers

But I get a 400 response when I provide the function result.

The example in the documentation doesn’t cover the function response.

Here is the messages object I provide in the call:

[
  {
    "role": "system",
    "content": "My system prompt"
  },
  {
    "role": "user",
    "content": "number of customers using xxx"
  },
  {
    "role": "assistant",
    "tool_calls": [
      {
        "function": {
          "arguments": "{\"query\":\"\\n[\\n    {\\n        \\\"$match\\\": {\\n            \\\"kind\\\": \\\"xxx\\\"\\n        }\\n    },\\n    {\\n        \\\"$group\\\": {\\n            \\\"_id\\\": \\\"$customer\\\",\\n            \\\"count\\\": {\\n                \\\"$sum\\\": 1\\n            }\\n        }\\n    },\\n    {\\n        \\\"$count\\\": \\\"total\\\"\\n    }\\n]\\n\"}",
          "name": "execute_mongo_aggregate_query"
        },
        "id": "0",
        "type": "function"
      }
    ]
  },
  {
    "role": "tool",
    "content": "{\"data\":[{\"total\":24}]}",
    "tool_call_id": "0"
  }
]

The same is working with the OpenAI API

The API does not support the “legacy” functions parameter.

It only will accept tools.

Also note that the Google API compatibility isn’t correctly returning the index field in tool_calls, employed for OpenAI models supporting parallel tool calls, which has been there since the introduction of “tools”, which also match an ID between tool call and tool return, for ordering context. I haven’t yet unraveled further incompatibility in matching conversational history.

You might omit sending back a tool call ID and see how that performs.

1 Like