DEV Community

Cover image for How to Stream Completed JSON from OpenAI’s GPT (and Kill the Spinner)
Joel Tankard
Joel Tankard

Posted on

How to Stream Completed JSON from OpenAI’s GPT (and Kill the Spinner)

OpenAI's GPT-4 and GPT-3.5 now allow developers to receive responses in structured JSON format, offering new UI possibilities. But, this feature has its challenges, especially with streaming outputs. When responses are streamed from the language model in JSON mode, they often result in incomplete JSON structures that can't be parsed.

Example of a JSON string being streamed in from GPT-4 using JSON mode

This leads to the need for spinners while waiting for the complete JSON, which can slow down the user experience. For instance, in listing available fruits from a partially streamed JSON, a spinner is used to indicate the response is still generating.

Example of using spinners while waiting for GPT to finish its response

Introducing json-autocomplete

JSON Autocomplete allows you to input an incomplete JSON string, which it completes for you:
Example of json-autocomplete finishing the incomplete JSON returned from GPT

This means you can directly show the AI's output in your UI, making your product feel faster.
Example of UI elements being streamed in using json-autocomplete

Setting Up json-autocomplete

Integrating JSON Autocomplete is straightforward. First, install it using npm or yarn:

npm install json-autocomplete
Enter fullscreen mode Exit fullscreen mode

Then, import it into your project and use it to convert incomplete JSON responses into complete ones.

import JSONAutocomplete from 'json-autocomplete';

const jsonResponseFromGPT = // your partially streamed JSON response from GPT;

const completeJsonString = JSONAutocomplete(jsonResponseFromGPT);
Enter fullscreen mode Exit fullscreen mode

Remember to parse the completed JSON string:

const parsedJson = JSON.parse(completeJsonString);
Enter fullscreen mode Exit fullscreen mode

Now, you can use parsedJson[0]?.name as soon as it streams from the modal.

Applications and Motivations

JSON Autocomplete improves user experience by quickly presenting data and enables new UI design possibilities, as demonstrated in the Operator app.

Example of json-autocomplete's use in the Operator app

This is still a work in progress so if any bugs arise create an issue in the repo: https://github.com/Operator-technology/json-autocomplete

Top comments (0)