json-autocomplete

Autocomplete any prefix substring of a JSON to become valid


Install
pip install json-autocomplete==0.2.1

Documentation

JSON Autocomplete

This library offers a simple function to complete any prefix of a valid JSON string, in a way that makes it a valid JSON again (e.g. by closing all open brackets and quotes), in a minimal way.

There is only one function, json_autocomplete(json_prefix: str) -> str, which takes a prefix of a valid JSON string and returns a valid JSON string that is the shortest possible completion of the prefix.

The heck did I develop this for? When streaming a response from a LLM like ChatGPT, where the model generates a JSON string, you can render it before the model is done generating the response.

Another use case could be when you want to allow the user to enter a JSON string, but you want to offer autocomplete suggestions. You can use this function to get the shortest possible completion of the prefix the user has entered, and then offer that as a suggestion.

Examples

>>> json_autocomplete('')
'null'
>>> json_autocomplete('n')
'null'
>>> json_autocomplete('tr')
'true'
>>> json_autocomplete('-')
'-0'
>>> json_autocomplete('2.')
'2.0'
>>> json_autocomplete('[')
'[]'
>>> json_autocomplete('{')
'{}'
>>> json_autocomplete('{"')
'{"": null}'
>>> json_autocomplete('{"a": 1, "b": 2')
'{"a": 1, "b": 2}'

Installation

pip install json-autocomplete

Then, simply import the function:

from json_autocomplete import json_autocomplete

json_autocomplete('{"a": 1, "b": 2')

Development

After making any changes to either Cython files or C++ files, you must cythonize the files:

cythonize -i json_autocomplete/*.pyx

This will 1) compile the Cython files to C++ files, and 2) compile the C++ files to shared libraries.

Afterwards, you can install the package:

pip install .