Skip to main content

Tiny schema validator for structured LLM responses. Python port of @mukundakatta/llm-response-schema-lite.

Project description

llm-response-schema-lite-py

PyPI Python License: MIT

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) -> ValidationResult
  • parse_and_validate(json_str, schema) -> ValidationResult
  • validate_response(value, schema) -- JS-parity alias of validate.

ValidationResult is a dataclass with valid: bool, value: Any | None, errors: list[dict].

License

MIT

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

llm_response_schema_lite_py-0.1.0.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

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

llm_response_schema_lite_py-0.1.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file llm_response_schema_lite_py-0.1.0.tar.gz.

File metadata

File hashes

Hashes for llm_response_schema_lite_py-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a21b57cab51ff9fd5a4546cdb66d04840a6b4846cda6dfe13af907bf8e2dbf69
MD5 bd795430c2ae609a89345bcb5ae6f08d
BLAKE2b-256 b2d8a3a934b6f136b3d3f4da4e0ddc8dcbca4411e4dc06db8275f472e46e91dd

See more details on using hashes here.

File details

Details for the file llm_response_schema_lite_py-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for llm_response_schema_lite_py-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3efd54210a7c6c16015eb5f2b293b579f82bba79c7c3ec719989332ef7b25e52
MD5 b7f49ab981d1ccad85bab386b58cb4b7
BLAKE2b-256 d5d1ca45162c62128b70b191f6419db963ac4faf400967b08645da5143f008af

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