Skip to main content

Parse partial JSON generated by LLM

Project description

Partial JSON Parser

Sometimes we need LLM (Large Language Models) to produce structural information instead of natural language. The easiest way is to use JSON.

But before receiving the last token of response, the JSON is broken, which means you can't use JSON.parse to decode it. But we still want to stream the data to the user.

Here comes partial-json-parser, a lightweight and customizable library for parsing partial JSON strings. Here is a demo.

(Note that there is a JavaScript implementation too)

Installation

pip install partial-json-parser # or poetry / pdm / conda

partial-json-parser is implemented purely in Python, with good type hints.

Usage

Importing the library

You can import the loads function and the Allow object from the library like this:

from partial_json_parser import loads, Allow

The Allow object is just an Enum for options. It determines what types can be partial. types not included in allow only appears after its completion can be ensured.

Parsing complete / partial JSON strings

The loads function works just like the built-in json.loads when parsing a complete JSON string:

result = loads('{"key":"value"}')
print(result)  # Outputs: {'key': 'value'}

You can parse a partial JSON string by passing an additional parameter to the loads function. This parameter is a bitwise OR of the constants from the Allow flag:

(Note that you can directly import the constants you need from partial-json-parser.options)

from partial_json_parser import loads, Allow
from partial_json_parser.options import STR, OBJ

result = loads('{"key": "v', STR | OBJ)
print(result)  # Outputs: {'key': 'v'}

In this example, Allow.STR tells the parser that it's okay if a string is incomplete, and Allow.OBJ tells the parser so as a dict. The parser then try to return as much data as it can.

If you don't allow partial strings, then it will not add "key" to the object because "v is not close:

result = loads('{"key": "v', OBJ)
print(result)  # Outputs: {}

result = loads('{"key": "value"', OBJ)
print(result)  # Outputs: {'key': 'value'}

Similarity, you can parse partial lists or even partial special values if you allow it:

(Note that allow defaults to Allow.ALL)

result = loads('[ {"key1": "value1", "key2": [ "value2')
print(result)  # Outputs: [{'key1': 'value1', 'key2': ['value2']}]

result = loads("-Inf")
print(result)  # Outputs: -inf

Handling malformed JSON

If the JSON string is malformed, the parse function will throw an error:

loads("wrong")  # MalformedJSON: Malformed node or string on line 1

API Reference

loads(json_string, [allow_partial])

  • json_string <string>: The JSON string to parse.
  • allow_partial <Allow | int>: Specify what kind of partialness is allowed during JSON parsing (default: Allow.ALL).

Returns the parsed Python value.

Allow

Enum class that specifies what kind of partialness is allowed during JSON parsing. It has the following members:

  • STR: Allow partial string.
  • NUM: Allow partial number.
  • ARR: Allow partial array.
  • OBJ: Allow partial object.
  • NULL: Allow partial null.
  • BOOL: Allow partial boolean.
  • NAN: Allow partial NaN.
  • INFINITY: Allow partial Infinity.
  • _INFINITY: Allow partial -Infinity.
  • INF: Allow both partial Infinity and -Infinity.
  • SPECIAL: Allow all special values.
  • ATOM: Allow all atomic values.
  • COLLECTION: Allow all collection values.
  • ALL: Allow all values.

Testing

To run the tests for this library, you should clone the repository and install the dependencies:

git clone https://github.com/promplate/partial-json-parser.git
cd partial-json-parser
pdm install

Then, you can run the tests using Hypothesis and Pytest:

pdm test

Please note that while we strive to cover as many edge cases as possible, it's always possible that some cases might not be covered.

License

This project is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

partial_json_parser-0.1.2.1.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

partial_json_parser-0.1.2.1-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file partial_json_parser-0.1.2.1.tar.gz.

File metadata

  • Download URL: partial_json_parser-0.1.2.1.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.10.0 CPython/3.12.0

File hashes

Hashes for partial_json_parser-0.1.2.1.tar.gz
Algorithm Hash digest
SHA256 6cf95cfff8dd4e2aa74469d2eb790faa176fd901014744a3b2b8d14e8bfd0110
MD5 028f024d6566b971336f5baa37d1781a
BLAKE2b-256 decf934400a43e6074dbc275fe41732d182bb7db1f58288f01a8af59a501fb08

See more details on using hashes here.

Provenance

File details

Details for the file partial_json_parser-0.1.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for partial_json_parser-0.1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3f8552c4ca6bae9e785645652f1a966334899cc13e11c178dc41144c375103b4
MD5 43cfa43adac0d56abecf9f46beee4dea
BLAKE2b-256 bd0bd6d76ed3ab44ab7e05cc803f11d37bf3616e20d7955fc3b426964550f992

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page