Search code examples
jsonopenai-api

What is the OpenAI Chat Completion API tools/functions property format?


Is there any clear documentation on the format of OpenAI's Chat Completion API tools/functions object format? I understand it's JSON, but there appear to be underlying requirements of what property names/types are allowed inside of the objects.

I tried figuring out what all property types OpenAI allowed in their tools/functions definition, but their documentation isn't great (they just have links to a simple example and an explanation of JSON schema 💩). They define description and name, but leave parameters pretty much open to interpretation.

Image of tools property definition


Solution

  • After an hour or two of searching around, experimenting, and cobbling something together, I think I finally created a template that I could reference in the future. Hopefully this saves someone else some time in the future.

    {
      "description": "This is a template that you can start from to build your tool",
      "name": "new_tool",
      "parameters": {
        "properties": {
          "array_property_name": {
            "description": "A property that returns an array of items (can be any type mentioned below, including an object)",
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "boolean_property_name": {
            "description": "A property that returns a boolean",
            "type": "boolean"
          },
          "enum_property_name": {
            "description": "A property that returns a value from a list of enums (can be any type)",
            "enum": [
              "option 1",
              "option 2",
              "option 3"
            ],
            "type": "string"
          },
          "number_property_name": {
            "description": "A property that returns a number",
            "type": "number"
          },
          "object_property_name": {
            "description": "A property that returns an object",
            "properties": {
              "foo": {
                "description": "A property on the object called 'foo' that returns a string",
                "type": "string"
              },
              "bar": {
                "description": "A property on the object called 'bar' that returns a number",
                "type": "number"
              }
            }
          },
          "string_property_name": {
            "description": "A property that returns a string",
            "type": "string"
          }
        },
        "required": [
          "array_property_name",
          "number_property_name"
        ],
        "type": "object"
      }
    }