Returning tool / function results over OpenAI API

I am trying to implement tool use through the OpenAI endpoints. I get a 400 error (request contains an invalid argument), but I think it’s about passing back the function call (role: assistant, toolCalls message)

    "tools": [
        {
            "type": "function",
            "function": {
                "name": "mul",
                "description": "Multiply 2 numbers",
                "parameters": {
                    "type": "object",
                    "properties": {"x": {"type": "number"}, "y": {"type": "number"}},
                    "required": ["x", "y"],
                },
            },
        }
    ],
    "model": "models/gemini-2.0-flash-exp",
    "max_completion_tokens": 500,
    "messages": [
        {"role": "system", "content": "Use tools for all calculations where possible."},
        {"role": "user", "content": "what is two times three"},
        {
            "role": "assistant",
            "toolCalls": [
                {
                    "id": "0",
                    "type": "function",
                    "function": {"arguments": "{'x': 2, 'y': 3}", "name": "mul"},
                }
            ],
        },
        {"role": "tool", "tool_call_id": "0", "name": "mul", "content": "6.0000"},
    ],
}

I’m getting the toolCall from the first model response, but how should I format it and the result of the function for the next turn?

Another comment is that the OpenAI client expects “tool_calls” argument, but this endpoint is returning “toolCalls” so these aren’t being properly parsed by the client into the appropriate OpenAI client objects.

1 Like

Yes, I encountered the same problem and had to implement a temporary workaround.

@robince what workaround have you used ?

I haven’t found a workaround for the openai endpoints. I have switched to using the google.genai Python API.

I encountered the same issue and found that what’s causing this is "role" : "tool" for some reason. I changed the role to "user" and returned the function results exactly the same way and it worked fine.

There are lots of bugs in the API especially around tool use and it doesn’t look like anyone from the team is looking at the issues posted on here and fixing them, OpenAI SDK related or native API issues.

I havent looked at it again but at the time for me the problem was returning back the assistants toolcall in the message list, not the actual tool result. (ie the role: assistant, toolCalls block was causing the error).

For anyone trying to get this to work, I found that leaving tool_calls in the assistant’s response is fine, using the role “tool” is fine, but I needed to delete the content parameter for the assistant since it was an empty string, which was throwing a 400 error.

So, solution in my case is either to provide a non-empty content (like “The assistant is using a tool”) or to just delete the content parameter from the message.

One other thing to note – a tool call ID is not returned from the API, so I had to generate an index for internal record-keeping and matching with the tool call / compatibility with OpenAI & OpenRouter.

2 Likes