Function Call returning NULL message[0].content

I am trying to Implement function calling using ChatCompletion using Apex Programming Language. But for some reason I am getting Null message[0].content.

Here’s my function in Request:

{
    "tool_choice": "auto",
    "tools": [
        {
			"type": "function",
            "function": {
				"description": "Gets the Indian States and Their Captitals",
                "name": "get_states",
                "parameters": {
					"type": "object",
                    "properties": {
                        "states": {
                            "items": {
								"description": "List of states and their capitals.",
								"type": "array",
                                "properties": {
                                    "State_capital_name": {
                                        "description": "Name of the state capital",
                                        "type": "string"
                                    },
                                    "state_name": {
                                        "description": "Name of the state",
                                        "type": "string"
                                    }
                                },
                                "type": "object"
                            }
                            
                        }
                    }
                    
                }
                
            }
            
        }
    ],
    "model": "gpt-4",
    "messages": [
        {
            "content": "Give me all States of India with their capitals",
            "role": "user"
        },
        {
            "content": "",
            "role": "system"
        }
    ]
}

Here’s what am getting the response:

{
  "choices": [
    {
      "finish_reason": "tool_calls",
      "index": 0,
      "logprobs": null,
      "message": {
        "content": null,
        "role": "assistant",
        "tool_calls": [
          {
            "function": {
              "arguments": {},
              "name": "get_states"
            },
            "id": "call_CV3CjBqVDcJkD2l1FSpPtYdq",
            "type": "function"
          }
        ]
      }
    }
  ],
  "created": 1716831365,
  "id": "chatcmpl-9TYYPrwqRzaLu336DRRZlUS5tqGUN",
  "model": "gpt-4-0613",
  "object": "chat.completion",
  "system_fingerprint": null,
  "usage": {
    "completion_tokens": 7,
    "prompt_tokens": 84,
    "total_tokens": 91
  }
}

If anyone Interested, here’s the apex code am using:

Map<String, Object> stateCustomFunction = new Map<String, Object>{
            'type' => 'function',
            'function' => new Map<String, Object>{
                'name' => 'get_states',
                'description' => 'Gets the Indian States and Their Captitals',
                'parameters' => new Map<String, Object>{
                'type' => 'object',
                'properties' => new Map<String, Object>{
                    'states' => new Map<String, Object>{
                        'type' => 'array',
                        'description' => 'List of states and their capitals.',
                        'items' => new Map<String, Object>{
                            'type' => 'object',
                            'properties' => new Map<String, Object>{
                                'state_name' => new Map<String, Object>{
                                    'type' => 'string',
                                    'description' => 'Name of the state'
                                },
                                'State_capital_name' => new Map<String, Object>{
                                    'type' => 'string',
                                    'description' => 'Name of the state capital'
                                }
                            }
                        }
                    }
                }
            }
            }
        };

What am I doing wrong here?

If you read the main description of the function, the function can be interpreted as listing all the states and all the capitals - no parameters needed and none passed as they are all optional.

It is typical for the AI to not produce language to a user when it invokes a function. The response to the user comes after the new knowledge is returned by the function which can fulfill the user’s request. You have to return what the AI requested first.

It seems like you perhaps have a misunderstanding of what the purpose of a function is. It is not for the AI to produce a response. It is for the AI to take action, such as “send an email”, or to retrieve knowledge, such as “today’s top news”.

You can ignore the description. I am using OpenAI to first get all Indian States and their capital and then using function calling to have the response in a structured way.

The way I understand the Function Calling is to structure the response coming to a schema. Because

"response_format": { "type": "json_object" }

Donot give me a specific schema.

You just have to instruct the AI properly, and the output comes correctly - no mode, no function:

From a system message:

// purpose
You are an AI that knows all things about India.

// response schema for listing Indian states in UI
{
    "$schema": "https://json-schema.org/draft/2019-09/schema",
    "description": "Output format for listing Indian states and their capital cities",
    "type": "object",
    "properties": {
        "states": {
            "type": "array",
            "items": {
                "type": "object",
                "description": "List of state and city pairings.",
                "properties": {
                    "State_capital_name": {
                        "description": "Name of the state capital",
                        "type": "string"
                    },
                    "state_name": {
                        "description": "Name of the state",
                        "type": "string"
                    }
                }
            }
        }
    }
}
1 Like

Okay, let me try this. But can you explain the purpose of this?

Do you not have an AI that you can ask such questions? I do.

The "$schema" key you see in the JSON snippet is used to declare the version of the JSON Schema standard that the JSON document conforms to. JSON Schema is a vocabulary that allows you to annotate and validate JSON documents, defining the structure and the rules the data should adhere to.

In the snippet you provided:

  • "$schema": "https://json-schema.org/draft/2019-09/schema"

This indicates that the JSON document is intended to be validated against the JSON Schema Draft 2019-09 (also known as Draft 8). This URL points to the meta-schema that defines the rules and structures that the schema itself must follow. By specifying this URL, tools and libraries that support JSON Schema can understand how to interpret and validate the JSON document based on the rules of the specified draft version.

In practical terms, including this key helps ensure that the JSON data structured according to the schema is consistent and follows a standard that can be universally understood and validated by developers and software that support that particular version of JSON Schema.

Draft 2020-12 is current, but we can play it safe and use a draft version that pretraining may have more knowledge of (although this schema is rudimentary).

Knowledge of JSON schemas - and then what keys are useless to send to the API - is part of making function specifications.

You can also use this schema to validate what you parse out of the AI response.