Issue with Function Calls: Only One Specific Function Fails to Invoke Correctly

Hi everyone,

I’m currently developing an application using OpenAI’s API and I’ve run into an issue with function calls. I’ve defined three different functions that can be called based on user input. The functions are:

  1. OracleInsight: Provides a tarot reading based on a keyword.
  2. webPilot: Performs an online search based on a user’s query.
  3. tool3: Performs image recognition based on a vision keyword.

The parameters passed to each function are structured similarly. Here is the setup:

const tools = [
    {
        "type": "function",
        "function": {
            "name": "OracleInsight",
            "description": "Provides a tarot reading and divination based on a keyword",
            "parameters": {
                "type": "object",
                "properties": {
                    "keyword": { "type": "string" }
                },
                "required": ["keyword"]
            }
        }
    },
    {
        "type": "function",
        "function": {
            "name": "webPilot",
            "description": "Performs an online search based on a user's query",
            "parameters": {
                "type": "object",
                "properties": {
                    "userQuery": { "type": "string" }
                },
                "required": ["userQuery"]
            }
        }
    },
    {
        "type": "function",
        "function": {
            "name": "tool3",
            "description": "Performs image recognition based on a vision keyword",
            "parameters": {
                "type": "object",
                "properties": {
                    "visionKeyword": { "type": "string" }
                },
                "required": ["visionKeyword"]
            }
        }
    }
];

When the user provides input, the appropriate function should be called. This logic is handled as follows:

const payload = {
    "model": "gpt-4-turbo",
    "messages": messages,
    "tools": tools,
    "tool_choice": "auto"  // Let GPT automatically decide if it needs to call a tool
};

const options = {
    "method": "post",
    "headers": {
        "Content-Type": "application/json",
        'Authorization': 'Bearer ' + apiKey,
    },
    "payload": JSON.stringify(payload)
};

try {
    var response = UrlFetchApp.fetch(ep, options);
    var json = response.getContentText();
    var data = JSON.parse(json);
    var choice = data.choices[0];

    if (choice.message.tool_calls && choice.message.tool_calls.length > 0) {
        var toolCall = choice.message.tool_calls[0];
        var functionCall = toolCall.function;
        var functionArgs = JSON.parse(functionCall.arguments);

        if (functionCall.name === "OracleInsight" && functionArgs.keyword) {
            OracleInsight(functionArgs.keyword, replyToken);
        } else if (functionCall.name === "webPilot" && functionArgs.userQuery) {
            webPilot(functionArgs.userQuery, replyToken);
        } else if (functionCall.name === "tool3" && functionArgs.visionKeyword) {
            tool3(functionArgs.visionKeyword, replyToken);
        } else {
            Logger.log('Missing arguments for function call: ' + JSON.stringify(functionArgs));
        }
    } else {
        gptreplyToLine(replyToken, [{ type: 'text', text: choice.message.content }]);
    }
} catch(e) {
    Logger.log('Error: ' + e.message);
    if (e.response) {
        Logger.log('Response Code: ' + e.response.getResponseCode());
        Logger.log('Response Text: ' + e.response.getContentText());
    }
}

While the OracleInsight and webPilot functions are called correctly, the tool3 function consistently fails to invoke, regardless of whether I use GPT-3.5-turbo, GPT-4-turbo, or GPT-4o models. The failure is indicated by missing arguments for the function call, as seen in the logs:

Missing arguments for function call: {"visionKeyword":"describe character features in the image"}

Here’s the definition and implementation of tool3 :

function tool3(visionKeyword, replyToken) {
    var scriptProperties = PropertiesService.getScriptProperties();
    var imageUrl = scriptProperties.getProperty('latestImageUrl'); // From PropertiesService

    var requestContent = visionKeyword;
    var chatHistory = GetAndUpdateChatHistory(visionKeyword, "");

    var historyMessages = chatHistory.map(entry => ({
        role: entry.role,
        content: [{ type: "text", text: entry.content }]
    }));

    var gptRequest = {
        model: "gpt-4o",
        messages: [
            {
                role: "system",
                content: [{ type: "text", text: "Your system prompt here." }],
            },
            ...historyMessages,
            {
                role: "user",
                content: [{
                    type: "text",
                    text: requestContent
                }, {
                    type: "image_url",
                    image_url: {
                        url: imageUrl,
                        detail: "auto"
                    }
                }]
            }
        ],
        max_tokens: 360
    };

    try {
        var gptResponse = UrlFetchApp.fetch("https://api.openai.com/v1/chat/completions", {
            method: "post",
            contentType: "application/json",
            payload: JSON.stringify(gptRequest),
            headers: {
                "Authorization": "Bearer " + apiKey
            }
        });

        var gptResult = JSON.parse(gptResponse.getContentText());
        var botResponse = gptResult.choices[0].message.content;

        GetAndUpdateChatHistory(requestContent, botResponse);
        replyVisionMessage(replyToken, gptResult.choices[0].message.content);
    } catch (e) {
        Logger.log('Error: ' + e.message);
        if (e.response) {
            Logger.log('Response Code: ' + e.response.getResponseCode());
            Logger.log('Response Text: ' + e.response.getContentText());
        }
    }
}

I am unsure why fails while the other functions work perfectly with similar setups. Any insights or suggestions on resolving this issue would be greatly appreciated.tool3

Thank you!

Additional Information: Successful Invocation with Swapped Function Names

Hi OpenAI Team,

I have encountered an interesting and possibly critical issue related to function calls when using GPT-4 for image recognition tasks.

In my previous post, I reported that I received the error message “Missing arguments for function call: {“visionKeyword”:“describe character features in the image”}” despite properly defining the tool3 function for image recognition.

To further investigate, I swapped the names of the OracleInsight and tool3 functions in my code. Specifically, I renamed the OracleInsight function to handle image recognition and renamed the tool3 function to handle tarot readings. Surprisingly, the image recognition task was successfully invoked when the function name was OracleInsight.

This leads me to believe that there might be an underlying issue with how GPT-4 is handling function calls for image-related tasks. It appears that the model might be biased or incorrectly configured to handle image-related queries, causing it to fail when using a function explicitly named for this purpose.

Here are the specific steps I took and the observations made:

  1. Original Configuration:
  • OracleInsight for tarot readings.
  • tool3 for image recognition.
  • Error: “Missing arguments for function call: {“visionKeyword”:“describe character features in the image”}”
  1. Swapped Configuration:
  • OracleInsight for image recognition.
  • tool3 for tarot readings.
  • Result: Image recognition was successfully invoked using the OracleInsight function.

This suggests that there may be a bug related to how GPT-4 interprets and invokes functions specifically for image-related tasks. The issue might stem from how the model processes the visionKeyword parameter or how it identifies functions meant for image recognition.

I hope this additional information helps in diagnosing the issue. I am happy to provide further details or conduct more tests if necessary.

Thank you for your attention to this matter.

Best regards, Wayne

How about trying to rename tool3 to something descriptive of its function like the others, say AnalyzeImage?

Thank you for the suggestion. I initially used descriptive names like AnalyzeImage for the image recognition function. I tried several similar function names, but the same error occurred each time. The issue seems unrelated to the function name itself.

i got curious so i tested your functions. i renamed tool3 to imageRecognitionByKeyword.

{
                    "type": "function",
                    "function": {
                        "name": "OracleInsight",
                        "description": "Provides a tarot reading and divination based on a keyword",
                        "parameters": {
                            "type": "object",
                            "properties": {
                                "keyword": { "type": "string" }
                            },
                            "required": ["keyword"]
                        }
                    }
                },
                {
                    "type": "function",
                    "function": {
                        "name": "webPilot",
                        "description": "Performs an online search based on a user's query",
                        "parameters": {
                            "type": "object",
                            "properties": {
                                "userQuery": { "type": "string" }
                            },
                            "required": ["userQuery"]
                        }
                    }
                },
                {
                    "type": "function",
                    "function": {
                        "name": "imageRecognitionByKeyword",
                        "description": "Performs image recognition based on a vision keyword",
                        "parameters": {
                            "type": "object",
                            "properties": {
                                "visionKeyword": { "type": "string" }
                            },
                            "required": ["visionKeyword"]
                        }
                    }
                }

I tested it and all three functions can be invoked as expected. For the image analysis, i tried this:

can you analyze this image: a faint dot of light in the far distance.

tool output:

[{“function”: {“arguments”: “{"visionKeyword":"faint dot of light"}”, “name”: “imageRecognitionByKeyword”}, “id”: “call_cjFfdnxsxJXjtBjszHsQPq31”}]

1 Like

Hello,

Thank you for your response and testing my functions. I have followed your suggestions and renamed tool3 to imageRecognitionByKeyword. However, I’m still encountering the same issue where the function call fails due to missing arguments. I would like to know how you have written your system prompt that allows all three functions (OracleInsight, webPilot, and imageRecognitionByKeyword) to be invoked correctly.

Could you please share the exact system prompt you used in your setup?

Here are my current function definitions for reference:

const tools = [
    {
        "type": "function",
        "function": {
            "name": "OracleInsight",
            "description": "Provides a tarot reading and divination based on a keyword",
            "parameters": {
                "type": "object",
                "properties": {
                    "keyword": { "type": "string" }
                },
                "required": ["keyword"]
            }
        }
    },
    {
        "type": "function",
        "function": {
            "name": "webPilot",
            "description": "Performs an online search based on a user's query",
            "parameters": {
                "type": "object",
                "properties": {
                    "userQuery": { "type": "string" }
                },
                "required": ["userQuery"]
            }
        }
    },
    {
        "type": "function",
        "function": {
            "name": "imageRecognitionByKeyword",
            "description": "Performs image recognition based on a vision keyword",
            "parameters": {
                "type": "object",
                "properties": {
                    "visionKeyword": { "type": "string" }
                },
                "required": ["visionKeyword"]
            }
        }
    }
];

const systemPrompt = `
You are an AI assistant that helps with tarot readings, online searches, and image recognition.
- When a user asks about tarot, prophecy, or divination, you should call the OracleInsight function with the user's keyword.
- When a user asks a question that requires online information, you should call the webPilot function with the user's query.
- When a user asks about image analysis, you should call the imageRecognitionByKeyword function with the user's query. Do not ask the user to provide an image URL; use the pre-uploaded image URL instead.
- If the user asks about anything else, respond normally without calling any functions.

The OracleInsight function takes one parameter: "keyword".
The webPilot function takes one parameter: "userQuery".
The imageRecognitionByKeyword function takes one parameter: "visionKeyword".

Here are examples of how to call the functions:
1. OracleInsight:
{
  "name": "OracleInsight",
  "parameters": {
    "keyword": "tarot reading"
  }
}
2. webPilot:
{
  "name": "webPilot",
  "parameters": {
    "userQuery": "current weather in Boston"
  }
}
3. imageRecognitionByKeyword:
{
  "name": "imageRecognitionByKeyword",
  "parameters": {
    "visionKeyword": "analyze this image for text"
  }
}
`;

Any advice or corrections on how to improve the system prompt to ensure the correct invocation of functions would be greatly appreciated. Thank you!

My system prompt is just: You are a helpful assistant.. can you give a sample conversation with the missing argument?

Thank you for your response. When I changed my system prompt to the same as yours, “You are a helpful assistant,” GPT only engages in general conversation for image recognition requests.

Here is an example of the conversation:

USER: describe the person in this image

GPT: I’m not able to view or analyze images directly. If you can provide a description of the person or details from the image, I can help you further based on that information!

It seems that GPT is not invoking the imageRecognitionByKeyword function as expected. Could you please provide more details on how you have configured your setup to ensure that the function is called correctly?

Thank you!

The way your function supposed to work, based in your definition, is you will supply it with visionKeyword. Are you supplying some image url or base64 string?

Also, from what I gather, I think the vision component of the API might not work in trying to recognize people so it will refuse to do so.

Thank you for your response.

To clarify, I am supplying a URL for the image. However, the API does not reach the step where it processes the image; it fails earlier with the error “Missing arguments for function call: {“visionKeyword”:“describe this image”}”.

I have also tried changing the query to “describe this image,” but I still receive the same error. The issue seems to be with the function call arguments rather than the content of the query itself.

how do you supply the image url? in the user message?

describe this image? http://example.com/your/image/here.png

like this?
unless, you are doing like above, it will not be able to include it in the visionKeyword parameter. if you intend to supply the image url yourself after the function is invoked, remove visionKeyword from required.

Dear supershaneski,

I apologize for my mistake. When I created a copy of the callGPTFunction, I forgot to change the function name. This led to the API looking for a non-existent image recognition condition in the copy. I’m sorry for taking up your time to debug this issue, and I truly appreciate your help. I hope you are not upset. Thank you very much for your assistance.

Yours sincerely,

Wayne

1 Like