Native JSON Output From GPT-4

Simon Farshid
6 min readJun 15, 2023

When integrating LLMs into your products, you often want to generate structured data, like JSON. With the help of function calling (released June 13th 2023), this process has become much simpler!

In this post, I will explore the new API.

Function Calling

Function calling allows GPT to call a function instead of returning a string. At the time of writing, this feature is available for the chat models gpt-3.5-turbo-0613 and gpt-4-0613.

For this feature, two new parameters have been introduced in the Chat Completions API:

  • functions: A list of functions available to GPT, each with a name, description and a JSON Schema of the parameters.
  • function_call: You can force GPT to use a specific function (or explicitly forbid calling any functions).

I realized that by setting the function_call to a specific function, you can reliably expect JSON as the response from GPT calls. No more strings, yay!

Let’s see it in action with a demo app, Recipe Creator.

Recipe Creator

Recipe Creator is an app where the user inputs the name of a dish and is provided with instructions on how to cook it. Of course, it can be used to generate recipes for completely fictional…

--

--

Simon Farshid

I post about things I encounter in my daily life, mostly revolving around tech and startups.