unaiverse.uai.alt¶
A model cannot fill a widget. It reads the alt of a form, a deterministic instruction that names each field with its type and constraints and asks for one field: value line per field. The reply interpreter then reads those lines back (rung labeled of interpret_reply). The instruction lists the options of a select by their label because a model writes words far more readily than identifiers. The interpreter maps each label back to its value. When the author of the form gives an aiHint, the hint replaces the whole instruction. build_form calls generate_form_alt whenever its alt argument is None. Import both functions from unaiverse.uai. The protocol is described in the package page and the wording comes from templates.
The instruction has this shape in English and in every unknown language:
The Italian shape is [Passo {step} di {total}. ]Rispondi con righe 'campo: valore'. Campi: {field}[, {field}]*. The strings are part of the contract. The shared test vectors pin them and the JavaScript SDK produces the same bytes.
describe_field
¶
Source code in unaiverse/uai/alt.py
Describes one interactive field as {label} ({descriptors}). The descriptors are joined with , in the order fixed by the protocol.
- The type word. A
selectwithmax1 givesone of: L1, L2. Aselectwithmaxgreater than 1 gives{min} to {max} of: L1, L2whenminis above 0 ormaxis smaller than the number of options (withmindefaulting to 0) andone or more of: L1, L2otherwise. Every other type gives its word:text,long text,number,integer,yes or noordate YYYY-MM-DD. - The unit:
in {unit}. - The range of a
number,integerordatefield:between {min} and {max},at least {min}orat most {max}. - The length:
at most {maxLength} characters. - The format:
format email,format urlorformat phone. requiredwhenrequiredisTrueandoptionalotherwise.
The help of the field is never part of the description.
| Argument | Type | Default | Meaning |
|---|---|---|---|
f |
dict |
required | One interactive field of a validated spec. A section row has no name and raises KeyError. |
t |
dict |
required | A language table of TEMPLATES, usually the second item returned by template_for. |
It returns a str. The label falls back to the field name through field_label.
_, t = template_for("en")
describe_field({"name": "kids", "type": "integer", "label": "Children", "max": 4}, t)
# 'Children (integer, at most 4, optional)'
describe_field({"name": "extras", "type": "select", "label": "Extras", "max": 2, "options": [
{"value": "b", "label": "Bike"}, {"value": "k", "label": "Kayak"}, {"value": "g", "label": "Guide"}]}, t)
# 'Extras (0 to 2 of: Bike, Kayak, Guide, optional)'
_, t = template_for("it")
describe_field({"name": "email", "type": "text", "label": "Email", "format": "email",
"maxLength": 80, "required": True}, t)
# 'Email (testo, massimo 80 caratteri, formato email, obbligatorio)'
generate_form_alt
¶
Source code in unaiverse/uai/alt.py
Returns the instruction of a validated form. A non-blank aiHint is returned unchanged. Otherwise the function picks the language table from lang with template_for, writes the progress prefix when the spec has progress and lists every interactive field with describe_field. Section rows are left out.
| Argument | Type | Default | Meaning |
|---|---|---|---|
spec |
dict |
required | A validated form spec. |
It returns a str. The function does not check the length. build_form raises UaiError with reason alt_too_long when the result exceeds 2000 characters.
spec = build_form("Trip", [
{"type": "section", "label": "Who"},
{"name": "email", "type": "text", "label": "Email", "format": "email", "maxLength": 80,
"required": True, "help": "We never share it"},
{"name": "day", "type": "date", "label": "Day", "min": "2026-01-01"},
{"name": "meals", "type": "select", "label": "Meals", "max": 2,
"options": [{"value": "l", "label": "Lunch"}, {"value": "d", "label": "Dinner"}]},
{"name": "ok", "type": "bool", "label": "Insurance"},
], form_id="t", progress=(2, 3))
generate_form_alt(spec)
# "Step 2 of 3. Reply with lines 'field: value'. Fields: Email (text, at most 80 characters,
# format email, required), Day (date YYYY-MM-DD, at least 2026-01-01, optional), Meals (one or
# more of: Lunch, Dinner, optional), Insurance (yes or no, optional)."
The real result is one line. The comment wraps it for reading.