Metadata-Version: 2.4
Name: fuzy-jon
Version: 0.2.1
Project-URL: homepage, https://github.com/livingbio/fuzzy-json
Project-URL: repository, https://github.com/livingbio/fuzzy-json
Author-email: lucemia <lucemia@gmail.com>
License: MIT
License-File: LICENSE
Keywords: json,llm,openai
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Natural Language :: English
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Requires-Python: >=3.10
Requires-Dist: json5
Provides-Extra: dev
Requires-Dist: coveralls>=3.3.1; extra == 'dev'
Requires-Dist: mypy>=1.18.2; extra == 'dev'
Requires-Dist: pre-commit>=4.3.0; extra == 'dev'
Requires-Dist: pytest-cov<8.0,>=4.1; extra == 'dev'
Requires-Dist: pytest-recording>=0.13.4; extra == 'dev'
Requires-Dist: syrupy>=5.0.0; extra == 'dev'
Description-Content-Type: text/markdown

# fuzzy-json
[![CI](https://github.com/livingbio/fuzzy-json/workflows/python-unittest/badge.svg?branch=main)](https://github.com/livingbio/fuzzy-json/actions?query=workflow%3Apython-unittest++branch%3Amain++)
[![codecov](https://codecov.io/gh/livingbio/fuzzy-json/graph/badge.svg?token=B95PR629LP)](https://codecov.io/gh/livingbio/fuzzy-json)
[![pypi](https://img.shields.io/pypi/v/fuzy-jon.svg)](https://pypi.python.org/pypi/fuzy-jon)
<!-- [![downloads](https://pepy.tech/badge/fuzy-jon/month)](https://pepy.tech/project/fuzy-jon) -->
[![versions](https://img.shields.io/pypi/pyversions/fuzy-jon.svg)](https://github.com/livingbio/fuzy-jon)
[![license](https://img.shields.io/github/license/livingbio/fuzzy-json.svg)](https://github.com/livingbio/fuzzy-json/blob/main/LICENSE)
[![Checked with mypy](https://www.mypy-lang.org/static/mypy_badge.svg)](https://mypy-lang.org/)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
[![Imports: isort](https://img.shields.io/badge/%20imports-isort-%231674b1?style=flat&labelColor=ef8336)](https://pycqa.github.io/isort/)


![logo](./static/276545475-2f7178c0-70d9-4ebd-8ca2-b6a317062944.png)

- Fuzzy-JSON is a compact Python package with no dependencies, designed to address the pesky `JSONDecodeError` that sometimes occurs when utilizing OpenAI's powerful `call function`.
- These errors, although small, can be quite bothersome. I prefer not to rely on retries, replace or resort to using alternative prompts to rectify this issue.
- To tackle this problem, Fuzzy-JSON employs a pushdown automata capable of incorporating JSON syntax and rectifying invalid JSON structures in the correct positions.
- It boasts user-friendliness, making it exceptionally easy to use.
- Fuzzy-JSON serves as a specialized JSON repair tool, particularly tailored for resolving issues in the output of language models like LLMs. If you encounter any cases not covered by the tool, please don't hesitate to create an issue for further assistance.

> The arguments to call the function with, as generated by the model in JSON format. Note that the model does not always generate valid JSON, and may hallucinate parameters not defined by your function schema. Validate the arguments in your code before calling your function.

https://platform.openai.com/docs/api-reference/chat/object


## Installation

You can install fuzy-jon using pip, the Python package manager:

```bash
pip install fuzy-jon
```

## Getting Started

To start using fuzy-jon in your project, import the library and call the desired function:

```python
from fuzzy_json import loads

...

response = await openai.ChatCompletion.acreate(**kwargs)
args = response.choices[0].message.function_call.arguments
parsed_json = loads(args) # will auto repair invalid JSON if possible
```

## Examples

`fuzzy_json.loads` works as a drop-in replacement for `json.loads`, but automatically repairs common issues found in LLM-generated JSON:

### Trailing commas

```python
>>> from fuzzy_json import loads
>>> loads('{"name": "Alice", "age": 30,}')
{'name': 'Alice', 'age': 30}
```

### Unescaped quotes inside strings

```python
>>> loads('{"description": "She said \\"hello\\" to him"}')
{'description': 'She said "hello" to him'}
```

### Missing closing braces/brackets

```python
>>> loads('{"name": "Alice", "items": [1, 2, 3}')
{'name': 'Alice', 'items': [1, 2, 3]}
```

### Markdown-wrapped JSON (e.g. `` ```json `` blocks)

```python
>>> loads('json\n{"name": "Alice"}')
{'name': 'Alice'}
```

### Valid JSON passes through unchanged

```python
>>> loads('{"name": "Alice", "scores": [95, 87, 92]}')
{'name': 'Alice', 'scores': [95, 87, 92]}
```

### Disabling auto-repair

If you want strict parsing (no repair), set `auto_repair=False`:

```python
>>> import json
>>> try:
...     loads('{"trailing": "comma",}', auto_repair=False)
... except json.JSONDecodeError:
...     print("Invalid JSON!")
Invalid JSON!
```

## Why fuz[z]y-j[s]on?

- Today marks the 3rd birthday of my beloved daughter, Patty, and I'm eager to make it a memorable day for her.
- I've introduced an open-source project called "Fuzzy JSON," which is inspired by her favorite book, "Fuzzy Ocean." You can find the book here: https://www.amazon.com/-/zh_TW/dp/178700063X
- Patty enjoys identifying and discussing the names of various enchanting ocean creatures depicted in her books.
- The package is named "fuzy-jon" due to a minor glitch from DALL-E-3, which I found endearing and decided to embrace. I believe it aligns perfectly with the package's intended purpose.
- This project is designed to address annoying issues like "JSONDecodeError" that may arise when utilizing OpenAI's function call feature, making the function call feature less troublesome.
- With these adjustments, I hope to bring joy to everyone's day.

![fuzy-jon](./static/393729620_10228059130956650_7731223954878062302_n.jpg)
