Skip to main content

Generate JSON that strictly matches a schema with automatic retries.

Project description

LetsJSON

让模型输出强约束 JSON:

  • 按 schema 校验字段与类型
  • 不符合时自动重试(默认 3 次)
  • 超过重试次数仍失败则抛错

使用

uv add letsjson

或者:

pip install letsjson
from openai import OpenAI
from letsjson import LetsJSON
import os
client = OpenAI(
    api_key="你的 API key",
    base_url="你的 API base URL",
)
generator = LetsJSON(client, model="你的 model name")  # repeat 可选,默认 3

schema = {
    "title": str,
    "steps": [{"time": str, "location": str, "detail": str}],
}
result = generator.gen('''给我一个2天1夜的上海旅游计划''', schema)
print(result)

Schema 支持

  • 对象:{"name": str, "age": int}
  • 列表:{"items": [str]}(列表 schema 必须且只能有 1 个元素类型)
  • 嵌套:{"user": {"name": str}, "tags": [str]}
  • 类型严格校验:
    • int 不接受 bool
    • float 接受 intfloat(不接受 bool

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.4.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

letsjson-0.1.4-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file letsjson-0.1.4.tar.gz.

File metadata

  • Download URL: letsjson-0.1.4.tar.gz
  • Upload date:
  • Size: 3.8 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

Hashes for letsjson-0.1.4.tar.gz
Algorithm Hash digest
SHA256 4a6bde3ba41fc6bbc1cea5e9abcf1f2116647bff77b71149845f46f9fa7c88c3
MD5 d21c1fb633448bb57a3729e019f0fc7e
BLAKE2b-256 ccd00e2ca1ca1e47470e016118de3a02557e8fa1518bd0c97a2707e7f0031f73

See more details on using hashes here.

File details

Details for the file letsjson-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: letsjson-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 4.2 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

Hashes for letsjson-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 f792f269c917d9f22b089583447a5c352603af5ea2c28a96921ca7795e1c1a78
MD5 ff529e41e1be6d2002ec75b38a779c8a
BLAKE2b-256 b7068b5f8fb46647c7d48164874cd08cc4f67406ee4afb8acaf8cbde14fbb0a5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page