Tiny schema validator for structured LLM responses. Python port of @mukundakatta/llm-response-schema-lite.
Project description
llm-response-schema-lite-py
Tiny schema validator for structured LLM responses. Two schema formats: a one-line shape spec ({"name": "str", "age": "int"}) and the JS-sibling rule format ({"name": {"type": "str", "required": True, "enum": [...]}}). Returns a (valid, errors) result you can hand straight back to the LLM as feedback. Zero runtime dependencies.
Python port of @mukundakatta/llm-response-schema-lite.
Install
pip install llm-response-schema-lite-py
Quick start
Shape format (Python-friendly, 1-liner)
from llm_response_schema_lite import validate
schema = {"name": "str", "age": "int", "tags": "list", "email": "str?"}
validate({"name": "Ada", "age": 36, "tags": ["scientist"]}, schema)
# ValidationResult(valid=True, value={...}, errors=[])
validate({"name": "Ada", "age": "thirty-six"}, schema)
# ValidationResult(valid=False, errors=[
# {"path": "age", "message": "expected_int"},
# {"path": "tags", "message": "required"}
# ])
Suffix a type with ? to mark it optional. Supported types: str, int,
float, bool, list, dict (and JS aliases: string, number,
boolean, array, object).
JS-sibling rule format (full feature parity)
schema = {
"status": {"type": "str", "required": True, "enum": ["ok", "error"]},
"code": {"type": "int", "required": False},
}
validate({"status": "ok", "code": 200}, schema) # valid
validate({"status": "weird"}, schema) # not_in_enum
validate({}, schema) # required
Parse JSON + validate in one shot
from llm_response_schema_lite import parse_and_validate
result = parse_and_validate('{"name": "Ada"}', {"name": "str"})
result.valid # True
result.value # {"name": "Ada"}
If JSON parsing fails, you get back ValidationResult(valid=False, value=None, errors=[{"path": "$", "message": "invalid_json"}]).
API
validate(value, schema) -> ValidationResultparse_and_validate(json_str, schema) -> ValidationResultvalidate_response(value, schema)-- JS-parity alias ofvalidate.
ValidationResult is a dataclass with valid: bool, value: Any | None, errors: list[dict].
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
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 llm_response_schema_lite_py-0.1.0.tar.gz.
File metadata
- Download URL: llm_response_schema_lite_py-0.1.0.tar.gz
- Upload date:
- Size: 5.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a21b57cab51ff9fd5a4546cdb66d04840a6b4846cda6dfe13af907bf8e2dbf69
|
|
| MD5 |
bd795430c2ae609a89345bcb5ae6f08d
|
|
| BLAKE2b-256 |
b2d8a3a934b6f136b3d3f4da4e0ddc8dcbca4411e4dc06db8275f472e46e91dd
|
File details
Details for the file llm_response_schema_lite_py-0.1.0-py3-none-any.whl.
File metadata
- Download URL: llm_response_schema_lite_py-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3efd54210a7c6c16015eb5f2b293b579f82bba79c7c3ec719989332ef7b25e52
|
|
| MD5 |
b7f49ab981d1ccad85bab386b58cb4b7
|
|
| BLAKE2b-256 |
d5d1ca45162c62128b70b191f6419db963ac4faf400967b08645da5143f008af
|