# Function Calling parameter types

**URL:** <https://community.openai.com/t/function-calling-parameter-types/268564>\
**Category:** Prompting\
**Tags:** gpt-4\
**Created:** [June 17, 2023, 12:35pm UTC](https://community.openai.com/t/function-calling-parameter-types/268564 "2023-06-17T12:35:19Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![classyd](https://avatars.discourse-cdn.com/v4/letter/c/67e7ee/32.png) [@classyd](https://community.openai.com/u/classyd)\
**Post date:** [June 17, 2023, 12:35pm UTC](https://community.openai.com/t/function-calling-parameter-types/268564/1 "2023-06-17T12:35:19Z")

</div>

What are the types that can be passed as parameters? I can only seem to set the type as a string.  
can you set more complex types? i.e Array, two dimensional Array, objects, etc

I’m sure I’m doing something wrong. If I set the type to array I get 400 Bad request

```auto
properties: {
                       key: {
                           type: "string",
                           description: "example description"
                       },
}

```

---

<div class="post-metadata">

**Author:** ![EricGT](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/ericgt/32/20571_2.png) [@EricGT](https://community.openai.com/u/EricGT)\
**Post date:** [June 17, 2023, 12:52pm UTC](https://community.openai.com/t/function-calling-parameter-types/268564/2 "2023-06-17T12:52:31Z")

</div>

> [@classyd](#):
>
> What are the types that can be passed as parameters?

The other day I do remember seeing the JSON schema for OpenAI functions. I think I even bookmarked the web page because it was three degrees of separation from the [blog page](https://openai.com/blog/function-calling-and-other-api-updates).

Sadly I cannot find the page nor the link.

However ChatGPT does seem to possess some knowledge of the JSON schema so prompted ChatGPT.

* * *

**Prompt**  
[ChatGPT May 24 Version](https://help.openai.com/en/articles/6825453-chatgpt-release-notes)

What is the JSON schema for OpenAI function properties for a two dimensional array.

**Completion**

As of my knowledge cutoff in September 2021, OpenAI does not provide a specific JSON schema for function properties that includes a two-dimensional array. However, you can define the structure of a two-dimensional array within the function properties using a nested array structure. Here’s an example of how you might structure the properties for an OpenAI function with a two-dimensional array using JSON:

json

```auto
{
  "name": "MyFunction",
  "description": "This is a sample function",
  "parameters": [
    {
      "name": "array",
      "type": "array",
      "description": "Two-dimensional array",
      "items": {
        "type": "array",
        "items": {
          "type": "number"
        }
      }
    }
  ],
  "output": {
    "type": "array",
    "items": {
      "type": "array",
      "items": {
        "type": "number"
      }
    }
  }
}

```

In this example, the function has a parameter named “array” which is an array of arrays. The inner arrays represent rows in the two-dimensional array, and each element within the inner arrays represents a value in the two-dimensional array (in this case, numbers). The output of the function is also a two-dimensional array with the same structure.

Please note that the actual schema for OpenAI function properties may differ depending on the specific implementation and requirements of the OpenAI API. It’s always a good idea to refer to the official OpenAI documentation or resources for the most up-to-date and accurate information on function properties.

* * *

I have not tried this.

* * *

* * *

**EDIT**

IIRC this is what I found the other day, may or may not be correct.

This OpenAI API reference page

[https://platform.openai.com/docs/api-reference/chat/create](https://platform.openai.com/docs/api-reference/chat/create)

Section: Create chat completion

notes

> parameters object Optional
> 
> The parameters the functions accepts, described as a JSON Schema object. See the [guide](https://platform.openai.com/docs/guides/gpt/function-calling) for examples, and the [JSON Schema reference](https://json-schema.org/understanding-json-schema/) for documentation about the format.

Here is the noted JSON Schema

> **[JSON Schema](https://json-schema.org/understanding-json-schema)**
>
> JSON Schema

HTH

---

<div class="post-metadata">

**Author:** ![classyd](https://avatars.discourse-cdn.com/v4/letter/c/67e7ee/32.png) [@classyd](https://community.openai.com/u/classyd)\
**Post date:** [June 17, 2023, 12:55pm UTC](https://community.openai.com/t/function-calling-parameter-types/268564/3 "2023-06-17T12:55:34Z")

</div>

nice ok I will try this and see what happens 👌

---

<div class="post-metadata">

**Author:** ![PriNova](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/prinova/32/752175_2.png) [@PriNova](https://community.openai.com/u/PriNova)\
**Post date:** [June 17, 2023, 2:04pm UTC](https://community.openai.com/t/function-calling-parameter-types/268564/4 "2023-06-17T14:04:45Z")

</div>

You can have a look into [this post](https://community.openai.com/t/function-call-is-invalid-please-help/266803/5) and the reference @EricGT provided.

---

<div class="post-metadata">

**Author:** ![jochen](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/jochen/32/58664_2.png) [@jochen](https://community.openai.com/u/jochen)\
**Post date:** [June 26, 2023, 11:36am UTC](https://community.openai.com/t/function-calling-parameter-types/268564/5 "2023-06-26T11:36:39Z")

</div>

I found using pydantic made things easier for me and worked well with str, int, lists and dictionaries.

Here is an example where I extract data from a set of user interviews.

```auto
...
from pydantic import BaseModel, Field
from typing import Optional, List

# define function structure in pydantic

class Topic(BaseModel):
    name: str = Field(..., description="Classification category")
    count: int = Field(..., description="Count for the category")

class ExtractInsightsParams(BaseModel):
    interviewee_names: List[str] = Field(..., description="The names of the interviewees, e.g. Christoph, person y")
    interviewee_count: int = Field(..., description="Number of people interviewed")
    interview_topics: List[Topic] = Field(..., description="List of top 10 classification categories with their counts")

```

Then I use it in my function:

```auto
def get_completion_from_messages(messages, 
                                 model="gpt-3.5-turbo-16k-0613", 
                                 temperature=0):
    response = openai.ChatCompletion.create(
        model=model,
        messages=messages,
        temperature=temperature, 
        max_tokens=1000,
        functions=[
        {
          "name": "extract_insights",
          "description": "Extract insights from interviews",
          "parameters": ExtractInsightsParams.schema()
        }
    ],
        function_call="auto",
    )
    print(response)
    function_call = response.choices[0].message["function_call"]
    arguments = json.loads(function_call["arguments"])
    return arguments

```

---

<div class="post-metadata">

**Author:** ![PriNova](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/prinova/32/752175_2.png) [@PriNova](https://community.openai.com/u/PriNova)\
**Post date:** [June 26, 2023, 6:04pm UTC](https://community.openai.com/t/function-calling-parameter-types/268564/6 "2023-06-26T18:04:18Z")

</div>

Really clever way. Sometimes we cannot see the forest because of all the trees.

---

<div class="post-metadata">

**Author:** ![DarthFader](https://avatars.discourse-cdn.com/v4/letter/d/7ea924/32.png) [@DarthFader](https://community.openai.com/u/DarthFader)\
**Post date:** [September 28, 2023, 8:03pm UTC](https://community.openai.com/t/function-calling-parameter-types/268564/7 "2023-09-28T20:03:02Z")

</div>

Is this with Pydantic 2.0? It didn’t work with 1.10.x  
The `schema()` produces “$ref” references which refer to a `definitions` field in the schema and I think the API doesn’t understand that.

---

<div class="post-metadata">

**Author:** ![\_j](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/_j/32/766292_2.png) [@\_j](https://community.openai.com/u/_j)\
**Post date:** [September 28, 2023, 9:54pm UTC](https://community.openai.com/t/function-calling-parameter-types/268564/8 "2023-09-28T21:54:22Z")

</div>

> [@jochen](#):
>
> worked well with str, int, lists and dictionaries

However, in general, one must write their own function, with **JSON data types**.

```
string
number
boolean
null/empty
object
array

```

Lets look at a demonstration of them all,

- how you would specify them as a “functions” API parameter;
- how the specification is rewritten into AI language by the endpoint;
- how the AI calls the function as it is specified.

### Function specification sent to API

```json
functions = [
{
    "name": "data_demonstration",
    "description": "This is the main function description",
    "parameters": {
        "type": "object",
        "properties": {
"string_1": {"type": "string", "description": ""},
"number_2": {"type": "number", "description": ""},
"boolean_3": {"type": "boolean", "description": ""},
"empty_4": {"type": "null","description": "This is a description of the empty_4 null property"},
"string_5_enum": {"type": "string", "enum": ["Happy", "Sad"]},
        },
        "required": ["string_1","number_2","boolean_3", "empty_4", "string_5_enum"]
    }
}
]

```

### Function specification received by AI:

```plaintext
namespace functions {

// This is the main function description
type data_demonstration = (_: {
string_1: string,
number_2: number,
boolean_3: boolean,
// This is a description of the empty_4 null property
empty_4: any,
string_5_enum: "Happy" | "Sad",
}) => any;

} // namespace functions

```

### Function used by AI:

```json
{
  ..
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": null,
        "function_call": {
          "name": "data_demonstration",
          "arguments": "{
  "string_1": "Hello",
  "number_2": 42,
  "boolean_3": true,
  "empty_4": null,
  "string_5_enum": "Happy"
}"
        }
      },
      "finish_reason": "function_call"
    }
  ],...

```

(\n is rendered above for clarity)

* * *

### Function specification with an inner object

```json
functions = [
{
    "name": "data_demonstration",
    "description": "This is the main function description",
    "parameters": {
        "type": "object",
        "properties": {
            "object_1": {
                "type": "object",
                "description": "The object data type as a property",
                "properties": {
                "string_2a": {
                    "type": "string",
                    "enum": ["Happy", "Sad"]
                    },
                "string_2b": {
                    "type": "string",
                    "description": "Description in a second object is lost"
                    },
                },
            },
            "string_1": {"type": "string",
                         "description": "Not required gets a question mark"},
        },
        "required": ["object_1","placeholder_2"]
    }
}
]

```

### Function specification received by AI, with object:

```plaintext
namespace functions {

// This is the main function description
type data_demonstration = (_: {
// The object data type as a property
object_1: {
  string_2a: "Happy" | "Sad",
  string_2b: string,
},
// Not required gets a question mark
string_1?: string,
}) => any;

} // namespace functions

```

Above we see a big failure that people don’t realize, nesting objects gets you **no descriptions** for the inner properties. They don’t have the “not required” question mark.

### Function used by AI and output by API:

```json
{...
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": null,
        "function_call": {
          "name": "data_demonstration",
          "arguments": "{
  "object_1": {
    "string_2a": "Happy",
    "string_2b": "Hello World"
  },
  "string_1": "Example data"
}"
        }
      },
      "finish_reason": "function_call"
    }
  ],
...

```

I hope you take the time to follow each of these properties through their specification to the ultimate API output.

There is one more JSON data type, `array`, which is equivalent to a Python list. Left as an exercise for you.

(Not explored, the method of setting the API `function_call` parameter from auto to “required” or “none”.)

---

<div class="post-metadata">

**Author:** ![jochen](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/jochen/32/58664_2.png) [@jochen](https://community.openai.com/u/jochen)\
**Post date:** [September 30, 2023, 4:41pm UTC](https://community.openai.com/t/function-calling-parameter-types/268564/9 "2023-09-30T16:41:55Z")

</div>

Thanks for adding to the thread. I was not aware of this. Very helpful.

---

<div class="post-metadata">

**Author:** ![EricGT](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/ericgt/32/20571_2.png) [@EricGT](https://community.openai.com/u/EricGT)\
**Post date:** [December 13, 2023, 4:43pm UTC](https://community.openai.com/t/function-calling-parameter-types/268564/10 "2023-12-13T16:43:09Z")

</div>


