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)
Metadata
Release files for letsjson 0.1.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| letsjson-0.1.6.tar.gz | 504.5 kB | Without provenance | Details for letsjson-0.1.6.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| letsjson-0.1.6-py3-none-any.whl | Python 3 | none | any | Details for letsjson-0.1.6-py3-none-any.whl |
Total release size: 510.3 kB
Release files / letsjson-0.1.6.tar.gz
| Download URL | letsjson-0.1.6.tar.gz |
|---|---|
| Size | 504.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9c3ac987f7088c8af9d8a6d63418056f8fc901eb40ded6e878c65b0749f42ac5
|
|
BLAKE2b-256 checksum How to use checksums |
53bcbb0efe6698b7a5106d426ca3f2194db793890b3f44291e4501cb7f479530
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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}
|
Release files / letsjson-0.1.6-py3-none-any.whl
| Download URL | letsjson-0.1.6-py3-none-any.whl |
|---|---|
| Size | 5.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e87fa74cca48c86e4ca0e248e72eff25b712b9c90563b946b78608f6dfe76c42
|
|
BLAKE2b-256 checksum How to use checksums |
49863c32a13583a6b2ca81ed5d3578e748c6911d8b9df9796a202081647204ae
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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}
|
Security for letsjson 0.1.6
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.