Generate JSON that strictly matches a schema with automatic retries.
Project description
LetsJSON
Let LLMs generate exactly the JSON you define.
Generate strongly constrained JSON from LLM outputs:
- Validate fields and types against your schema
- Auto-retry on invalid outputs (default: 3 attempts)
- Raise an error if all retries fail or return empty value
- It's very liteweight, with only 230+ lines of code.
Installation
uv add letsjson
or:
pip install letsjson
Usage
from openai import OpenAI
from letsjson import LetsJSON
client = OpenAI(
api_key="your API key",
base_url="your API base URL",
)
generator = LetsJSON(client, model="your model name") # repeat is optional, default is 3
schema = {
"title": str,
"steps": [{"time": str, "location": str, "detail": str}],
}
result = generator.gen("Give me a 2-day London travel plan", schema)
print(result)
# Streaming output (optional)
result = generator.gen_stream(
"Give me a 2-day London travel plan",
schema,
on_chunk=lambda chunk: print(chunk, end="", flush=True),
)
print("\n--- parsed json ---")
print(result)
# return:
# {
# "title": "2-Day London Travel Plan",
# "steps": [
# {"time": "Day 1 Morning",
# "location": "British Museum",
# "detail": "Explore ancient artifacts and world history."},
# {"time": "Day 1 Afternoon",
# "location": "Covent Garden",
# "detail": "Enjoy street performances and shopping."},
# ...
# ]
Supported Schema Types
- Object:
{"name": str, "age": int} - List:
{"items": [str]}(list schema must contain exactly one element type) - Nested:
{"user": {"name": str}, "tags": [str]} - Strict type checks:
intdoes not acceptboolfloatacceptsintandfloat(does not acceptbool)
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
letsjson-0.1.5.tar.gz
(504.4 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file letsjson-0.1.5.tar.gz.
File metadata
- Download URL: letsjson-0.1.5.tar.gz
- Upload date:
- Size: 504.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.11 {"installer":{"name":"uv","version":"0.9.11"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
946b7ff8435c0cede3464bc0c2ccd5615a9cf5ab287108cee535733b2cbd85ca
|
|
| MD5 |
0e06af75c724fadb21cd235a8eeba91a
|
|
| BLAKE2b-256 |
7959a4a783d8f2479ff4ff42d8776e0fe564fe66f2c1bc1fd72855448cc44a2e
|
File details
Details for the file letsjson-0.1.5-py3-none-any.whl.
File metadata
- Download URL: letsjson-0.1.5-py3-none-any.whl
- Upload date:
- Size: 5.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.11 {"installer":{"name":"uv","version":"0.9.11"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0f5dcf2beecbca34ac2704da0fbe1b7348fb54295b146953495078a36f8a66cf
|
|
| MD5 |
d14a0f3acefd909748e60946aed4d327
|
|
| BLAKE2b-256 |
f3f70d5cd2d157851383719766925ee5054185c9eca3835d54c2a21409b9c746
|