unaiverse.uai.serialize¶
A block is written in canonical form: keys in a fixed order for each block type, no whitespace outside strings, non-ASCII characters written without escapes and no trailing newline inside the fence. The Python package and the JavaScript SDK therefore produce the same bytes for the same spec. This property lets the shared test vectors compare outputs as strings and lets two implementations agree on the size limit of a block. Import canonical_json, serialize_block and num_to_text from unaiverse.uai. The key-order tables are module constants that only this module reads. The protocol is described in the package page.
The order of the keys of a block follows its table. Keys outside the table come after them in alphabetical order. Because the validator drops unknown keys, a validated spec holds only keys of the table.
serialize_block({"v": 1, "type": "media", "alt": "A cat.", "mime": "image/jpeg",
"src": "https://example.org/cat.jpg"})
# '```uai\n{"v":1,"type":"media","src":"https://example.org/cat.jpg","mime":"image/jpeg","alt":"A cat."}\n```'
BLOCK_ORDER
module-attribute
¶
BLOCK_ORDER = {'media': ('v', 'type', 'src', 'mime', 'title', 'poster', 'alt'), 'data': ('v', 'type', 'chart', 'series', 'alt'), 'form': ('v', 'type', 'id', 'name', 'lang', 'fields', 'progress', 'aiHint', 'alt'), 'reply': ('v', 'kind', 'to', 'values', 'alt', 'raw'), 'x': ('v', 'type', 'alt')}
A dict that maps each kind of block to the tuple of its keys in order.
| Kind | Order |
|---|---|
media |
v, type, src, mime, title, poster, alt |
data |
v, type, chart, series, alt |
form |
v, type, id, name, lang, fields, progress, aiHint, alt |
reply |
v, kind, to, values, alt, raw |
x (any type starting with x-) |
v, type, alt |
FIELD_ORDER
module-attribute
¶
FIELD_ORDER = ('name', 'type', 'label', 'required', 'help', 'default', 'placeholder', 'maxLength', 'format', 'min', 'max', 'unit', 'options', 'ui')
The key order of an interactive field of a form: name, type, label, required, help, default, placeholder, maxLength, format, min, max, unit, options, ui. A section row is always written as {"type": "section", "label": ...} with no other key. In a select field max and min therefore come before options.
The key order of a select option: value, label, help, media.
The key order of a chart series: label, points.
num_to_text
¶
Source code in unaiverse/uai/serialize.py
Writes a number as JavaScript writes it. Generated text such as the model instruction or the projection of an answer must match the JavaScript SDK byte for byte. The difference that matters is the integral float: JavaScript prints 180 where Python prints 180.0.
| Argument | Type | Default | Meaning |
|---|---|---|---|
x |
any | required | The value to write. |
| Input | Output |
|---|---|
True, False |
"true", "false" |
int |
str(x) (42 gives "42") |
finite float with no fractional part |
the integer (180.0 gives "180") |
other float |
repr(x) (0.1 gives "0.1") |
| anything else | str(x) |
A non-finite float follows repr and gives "nan", "inf" or "-inf". JavaScript, on the other hand, writes NaN and Infinity. Validated blocks never hold such values because the validator accepts only finite numbers.
canonical_json
¶
Source code in unaiverse/uai/serialize.py
Returns the canonical JSON body of a validated spec without the fence.
| Argument | Type | Default | Meaning |
|---|---|---|---|
spec |
dict |
required | A spec that has passed validate_block. The function does not validate it. |
The function picks the order from kind ("reply") or from type (x- prefix or a block type). It orders the fields and options of a form and the series of a chart with their own tables. The order of the values of a reply is part of the contract and the function keeps the order of the caller. Every integral float becomes an integer at any depth ({"b": 2.0} becomes {"b":2}). It writes non-ASCII characters as they are (ensure_ascii=False) with the separators , and :.
It returns a str. It raises ValueError with the message Interact: cannot serialize block of type ... when the spec has neither kind: "reply" nor a known or experimental type.
canonical_json({"v": 1, "kind": "reply", "to": "a", "values": {"b": 2.0, "a": 1}})
# '{"v":1,"kind":"reply","to":"a","values":{"b":2,"a":1}}'
encode_reply measures the size of a block on this string and the composer checks it against MAX_BLOCK_BYTES.
serialize_block
¶
Returns a validated spec as a fenced block ready to sit inside a message. It calls canonical_json and then wrap_fence and raises the same ValueError.
| Argument | Type | Default | Meaning |
|---|---|---|---|
spec |
dict |
required | A validated spec, for example the output of build_form. |
serialize_block(build_chart("pie", [{"label": "Votes", "points": [["Yes", 12], ["No", 5]]}],
"Votes: Yes 12, No 5."))
# '```uai\n{"v":1,"type":"data","chart":"pie","series":[{"label":"Votes","points":[["Yes",12],["No",5]]}],"alt":"Votes: Yes 12, No 5."}\n```'
A world that keeps the spec of a form calls serialize_block on it to put the form in a message. This is the same string that compose_form returns.