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
import os
from letsjson import LetsJSON
generator = LetsJSON(
base_url=os.getenv("OPENAI_BASE_URL"),
model=os.getenv("OPENAI_MODEL"),
api_key=os.getenv("OPENAI_API_KEY"),
temperature=0.2, # optional
)
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.6.tar.gz
(504.5 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.6.tar.gz.
File metadata
- Download URL: letsjson-0.1.6.tar.gz
- Upload date:
- Size: 504.5 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 |
9c3ac987f7088c8af9d8a6d63418056f8fc901eb40ded6e878c65b0749f42ac5
|
|
| MD5 |
0471f6ba1e665b40898baa5a86e2106a
|
|
| BLAKE2b-256 |
53bcbb0efe6698b7a5106d426ca3f2194db793890b3f44291e4501cb7f479530
|
File details
Details for the file letsjson-0.1.6-py3-none-any.whl.
File metadata
- Download URL: letsjson-0.1.6-py3-none-any.whl
- Upload date:
- Size: 5.8 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 |
e87fa74cca48c86e4ca0e248e72eff25b712b9c90563b946b78608f6dfe76c42
|
|
| MD5 |
c06644700271cfca8ba05454cf5a4c9e
|
|
| BLAKE2b-256 |
49863c32a13583a6b2ca81ed5d3578e748c6911d8b9df9796a202081647204ae
|