Generate JSON that strictly matches a schema with automatic retries.
Project description
LetsJSON
让模型输出强约束 JSON:
- 按 schema 校验字段与类型
- 不符合时自动重试(默认 3 次)
- 超过重试次数仍失败则抛错
使用(uv)
uv sync
from openai import OpenAI
from letsjson import LetsJSON
client = OpenAI(
api_key = "your_api_key",
base_url = "your_base_url",
)
generator = LetsJSON(
client=client,
model="your_model", # 必填
repeat=3, # 可选,默认 3
)
schema = {
"title": str,
"steps": [{"name": str, "minutes": int}],
}
result = generator.gen("给我一个 30 分钟早餐计划", schema)
print(result)
API
LetsJSON(client, model, repeat=3)gen(prompt: str, schema: dict[str, Any]) -> dict[str, Any]schema顶层必须是dict
Schema 支持
- 对象:
{"name": str, "age": int} - 列表:
{"items": [str]}(列表 schema 必须且只能有 1 个元素类型) - 嵌套:
{"user": {"name": str}, "tags": [str]} - 类型严格校验:
int不接受boolfloat接受int和float(不接受bool)
行为
- 返回值始终是
dict - 对象键必须和 schema 完全一致(不允许缺失或多余)
- 每次失败后会把上次错误拼到下一次提示词里继续重试
- 重试耗尽后抛出
LetsJSONGenerationError
OpenAI 客户端兼容性
- 优先调用
client.chat.completions.create(...) - 失败时回退到
client.responses.create(...) - 两者都不可用或都失败时抛出
LetsJSONGenerationError
输出解析容错
- 优先直接
json.loads(...) - 若模型返回了额外文本/Markdown,会尝试提取 JSON 代码块或首个完整 JSON 片段再解析
- 无法解析合法 JSON 时抛出
LetsJSONValidationError
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.3.tar.gz
(4.1 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.3.tar.gz.
File metadata
- Download URL: letsjson-0.1.3.tar.gz
- Upload date:
- Size: 4.1 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 |
be92a9e4f1fe44c2f97c61291a4383a09ae26a20415b1bc2aa2c39f3d23af089
|
|
| MD5 |
e56281fb43f0827d5ee27b586693c20c
|
|
| BLAKE2b-256 |
b38169f565a66ee786b8558084e0b93e4f628c55ef8b5edaac58de0bccb3697c
|
File details
Details for the file letsjson-0.1.3-py3-none-any.whl.
File metadata
- Download URL: letsjson-0.1.3-py3-none-any.whl
- Upload date:
- Size: 4.5 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 |
1495bda0bcd84decf25e1cc1402d3355d0f6c1ef4378e219bd87b421e9c29348
|
|
| MD5 |
7a64377784d24b5c1386f955b0710e0b
|
|
| BLAKE2b-256 |
47301da114713365ec8007677061308d1c3576b27b1f7c591f7f3cca7681fb87
|