Skip to main content

libration

prek Code style: ruff uv python License: MIT PyPI

Let a language model choose from a closed list: a strict schema for the model, a checked choice for your code.

When a program asks a model to decide something — where a ticket goes, which step comes next — the answer has to be one of the things the program can act on. Free text is not. libration turns that decision into a menu: a small TOML file naming the options and their arguments. From the menu it builds what the model is told, and it reads what the model answers into exactly one of two things: a Choice the menu allows, or a Refusal that says what is wrong.

A libration is the slow swing of a body bound to a point of balance: it moves, but it does not get away. The answers here may wander. They do not leave the menu.

  • The list is closed. An answer outside the menu never reaches your code as a choice. No fuzzy matching, no repair, no guessing.
  • A bad answer is a value, not an exception. Models get it wrong as a matter of course, so read returns a Refusal with the reason, ready to send back for a retry.
  • A bad menu fails at once. A menu no answer could satisfy raises MenuError when it is loaded, not in production on the first request.
  • Menus are data. They live in TOML beside your code, and anything your application needs to attach to an option travels along with it.
  • No dependencies. The standard library, nothing else. libration calls no model: you keep your client, your retries and your provider.

Install

uv add cyberise-libration        # or: pip install cyberise-libration

Requires Python 3.14 or newer. The import is libration.

Quickstart

Declare the decision:

name = "ticket-triage"

[[option]]
name = "escalate"
description = "The ticket needs a person."

[option.arguments.ticket]
kind = "string"
description = "The id of the ticket."

[option.arguments.team]
kind = "string"
description = "Which team takes it."
values = ["billing", "support"]

[[option]]
name = "close"
description = "Nothing is left to do."

Hand it to the model and read the answer. client is any client of the OpenAI Chat Completions API:

from libration import answers, menus, schemas

menu = menus.load(text=menu_toml)

completion = client.chat.completions.create(
    model=model,
    messages=[{"role": "user", "content": f"{ticket_text}\n\n{schemas.instructions(menu)}"}],
    response_format=schemas.response_format(menu),
)
reply = completion.choices[0].message.content

match answers.read(answer=reply, menu=menu):
    case answers.Choice(option="escalate", arguments=arguments):
        assign(ticket=arguments["ticket"], team=arguments["team"])
    case answers.Choice(option="close"):
        close_ticket()
    case answers.Refusal(reason=reason):
        retry_with(feedback=reason)

The model answers with one JSON object. reason is its own one-sentence justification, kept on the Choice:

{
  "choice": "escalate",
  "reason": "The customer was charged twice.",
  "arguments": {"ticket": "TICKET-4711", "team": "billing"}
}

There is no built-in "do nothing". A menu that allows skipping names an option for it, as close does above.

Why both a schema and a reader

  • schemas.response_format asks the provider for strict structured output, so a capable model cannot leave the shape. The shape has a gap, though: strict mode refuses anyOf at the root, so the root is one object with choice as an enum and arguments as an anyOf of one object per option. The schema allows the arguments of one option under the name of another.
  • schemas.instructions says the same in words. It matters for a model that takes no schema at all: send only the instructions and leave response_format out.
  • answers.read closes the gap and holds the line whatever the provider did. It checks the choice, then every argument against the option actually chosen.

response_format carries the shape of the OpenAI Chat Completions API. For a provider with a different request format, schemas.schema returns the bare JSON schema.

Writing a menu

Key Where Meaning
name menu Required. 1–64 letters, digits, - or _: it names the JSON schema
[[option]] menu At least one. Names are unique, the order is kept
name, description option Required. The description is what the model reads
[option.arguments.<name>] option Optional. Every argument is required in an answer
kind argument Required. string, integer, number, boolean, array or object
description argument Required
values string argument Optional. Closes the argument to a list of strings
items array argument Required. An argument describing each item
[….fields.<name>] object argument Required. An argument per field

Arrays and objects nest, and they stay strict all the way down. A free-form object does not exist: strict mode needs every key named.

[option.arguments.tags]
kind = "array"
description = "Labels for the ticket."
items = { kind = "string", description = "One label." }

[option.arguments.customer]
kind = "object"
description = "Who wrote in."

[option.arguments.customer.fields.email]
kind = "string"
description = "Their address."

An argument takes only the keys above: a misspelt value would leave it open, so it raises MenuError. Any other key on an option is yours. libration does not read it and keeps it on Option.extra, which is how a choice can carry what it sets off:

[[option]]
name = "escalate"
description = "The ticket needs a person."
route = { queue = "on-call" }
queue = menu.option(choice.option).extra["route"]["queue"]

What gets refused

answers.read never raises for a bad answer. It returns a Refusal carrying the first problem found and the answer as it came, when the answer:

  • is None, which a client returns when the model refused or called a tool
  • is not JSON, or not a JSON object — a code fence around it is read through
  • lacks choice, reason or arguments, or carries any other key
  • has a reason that is empty or no string, or arguments that are not an object
  • chooses something that is not on the menu
  • leaves out an argument of the chosen option, or adds one it does not take
  • gives an argument the wrong kind, or a value outside its values

The checks are JSON's, not Python's: true is no integer, and NaN and Infinity are no numbers. Inside arrays and objects the reason names the path, such as tags[1] or customer.email.

API

Name What it does
menus.load(*, text) Reads TOML into a Menu, or raises MenuError
menus.Menu, Option, Argument The menu as named tuples. One built in code skips the checks of load
Menu.option(name) The option of that name, or None
schemas.schema(menu) The JSON schema of an answer
schemas.response_format(menu) That schema as a strict response_format
schemas.instructions(menu) The same in words, for the prompt
answers.read(*, answer, menu) The reply as Choice(option, arguments, reason) or Refusal(reason, answer)

Development

just install   # dependencies, hooks
just test
just lint
just build     # the wheel
just release   # mint a release on GitHub, then: just publish <tag>

Changes are listed in CHANGES.md.

Licence

MIT

Metadata

Release files for cyberise-libration 2026.11.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cyberise-libration 2026.11.3
File Size Uploaded Provenance Actions
cyberise_libration-2026.11.3.tar.gz 12.5 kB With provenance Details for cyberise_libration-2026.11.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for cyberise-libration 2026.11.3
File Interpreter ABI Platform Actions
cyberise_libration-2026.11.3-py3-none-any.whl Python 3 none any Details for cyberise_libration-2026.11.3-py3-none-any.whl

Total release size: 24.5 kB

Release files / cyberise_libration-2026.11.3.tar.gz

Download URL cyberise_libration-2026.11.3.tar.gz
Size 12.5 kB
Tags Source
SHA-256 checksum
How to use checksums
a05e1fe8543b509e5a77e01674d0ee5d148f7c511bebecf7b039f045d905660f
BLAKE2b-256 checksum
How to use checksums
4658586ccefd55242363362733d575316db91fba6aff9ab6d4448f70d8764df6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 13, 2026.

Transparency log

Release files / cyberise_libration-2026.11.3-py3-none-any.whl

Download URL cyberise_libration-2026.11.3-py3-none-any.whl
Size 11.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
792c386c2d6112006919e1800519a44b21da0337544977dc129d94b452788483
BLAKE2b-256 checksum
How to use checksums
746c6e1d5f8150564aec03152520d5e09f6b5fce36e6d33f6602f8b1e6b84559
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 13, 2026.

Transparency log

Security for cyberise-libration 2026.11.3

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for cyberise_libration-2026.11.3.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 13, 2026.

Transparency log

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page