Skip to content

unaiverse.uai

Agents and worlds exchange text. A world often needs more than text: a quiz needs a form with options, a weather station needs a chart and a museum guide needs a picture. The interactive-message protocol puts these elements inside an ordinary text message as fenced blocks. The message thereby stays one string from the sender to the receiver. Every receiver reads the same string in its own way. The webapp draws each block as a widget, a person at a terminal reads a plain description of it and a language model reads an instruction written for it. The answer to a form travels back as a string too. The receiver turns it into one normalized event whether a person, a model or the webapp wrote it.

The package unaiverse.uai is the Python side of this protocol. Its only job is to turn strings into typed data and back. It depends on no other part of the SDK and on no third-party library. A world without a processor can therefore call it from its actions and the same code runs in the browser. The JavaScript SDK implements the same contract and both implementations run the same shared test vectors. Narrative explanations and the behavior of the agent are in Interactive messages.

Import every name from unaiverse.uai and never from its submodules. The layout of the submodules follows the JavaScript SDK and may change without notice. The names listed at the end of this page are the public API. The module pages below document each object in the module that defines it. The helpers that the package does not re-export are documented there too.

from unaiverse.uai import build_form, serialize_block, interpret_reply, check_reply

The message

A message is ordinary Markdown with zero or more blocks. A block is a fenced code block whose info string is exactly uai and whose body is exactly one JSON object.

Hi, I need your profile.

```uai
{"v":1,"type":"form","id":"p1","name":"Profile","fields":[{"name":"height","type":"number","label":"Height","required":true,"min":50,"max":250,"unit":"cm"}],"alt":"Reply with lines 'field: value'. Fields: Height (number, in cm, between 50 and 250, required)."}
```

The prose before, between and after the blocks is preserved verbatim and in order. An opening fence without a closing fence is prose. A body larger than 8192 bytes is never parsed. The exact grammar is in fence.

Blocks

Every block is a JSON object with "v": 1 (PROTOCOL_VERSION) and exactly one of type (a content block) or kind (the answer to a form). The three content types are listed in BLOCK_TYPES.

Block Carries Required keys Built by
media A picture, a video, an audio file or a PDF at an https URL. src, mime, alt build_media, compose_media
data A chart of kind bar, line or pie (CHART_KINDS) with 1 to 10 series of up to 500 points. chart, series, alt build_chart, compose_chart
form A form with 1 to 20 fields of the types of FIELD_TYPES. id, name, fields, alt build_form, compose_form
reply ("kind": "reply") The answer to a form. to (the id of the form), values encode_reply
x-* An experiment. Every receiver shows its alt. alt written by hand

A block that breaks a rule never breaks the message. The receiver shows its alt when the block has a usable one and its raw body otherwise. The rest of the message is unaffected. On the sending side the composer is strict and raises UaiError instead of emitting an invalid block. validate lists every check and its error code.

The alt text and the model view

Every content block carries an alt. A model reads this text, an old client and a terminal show it and a screen reader announces it. For a form the composer generates it from the spec with generate_form_alt. The result is a deterministic instruction in English or in Italian (according to the lang of the form) that asks for one field: value line per field. An author who needs a different instruction passes ai_hint.

A model never reads the JSON of a block. It reads the model view built by to_model_text. The view keeps the prose untouched and replaces every content block with its alt and every reply with its name: value lines. For the message above the model reads the following text.

Hi, I need your profile.
Reply with lines 'field: value'. Fields: Height (number, in cm, between 50 and 250, required).

Replies

The answer to a form is one reply block. It names the form in to, carries the canonical values by field name in values and a readable projection of the answer in alt. The optional raw holds the words from which the values were read. The webapp widget produces it when a person fills the form. encode_reply produces the same string in Python.

```uai
{"v":1,"kind":"reply","to":"p1","values":{"height":180},"alt":"Height: 180 cm","raw":["I am 180 cm tall"]}
```

A person and a model often answer without the block. interpret_reply matches any text against the forms that are still pending and tries five rungs in order: a reply block (block), key: value lines (labeled), a bare value for a form with one field (bare), comma-separated values in field order (csv) and finally free text (freetext). The interpreter never drops a text. It returns a ReplyEvent with the accepted values, the required fields still missing and the issues of the refused values. The issue codes are the closed list type, below_min, above_max, too_long, format and unknown_option (see constants).

Values that come from a peer are untrusted until they have been checked against the form that the world sent. A world keeps the spec returned by build_form and calls check_reply on what arrives. The check drops the keys that the form does not declare.

spec = build_form("Profile", [{"name": "height", "type": "number", "label": "Height",
                               "unit": "cm", "min": 50, "max": 250, "required": True}], form_id="p1")
event = interpret_reply("height: 180 cm", pending=[spec])
event.via, event.values     # ('labeled', {'height': 180.0})

Corrective rounds

An agent whose processor is a model answers a form by itself. The agent remembers the last form of each peer and matches the text of the model against it with read_answer. An answer that satisfies the form travels as a reply block that encode_reply builds with the words of the model in raw. An answer that falls short is not sent as it is. The agent asks the model again within the same turn with a corrective prompt written by retry_prompt. The prompt restates the question, quotes the answer and names what is missing or wrong. After the first round the prompt asks only for the missing fields and the agent merges the values of all attempts. The agent asks again at most two times (AgentBasics.UAI_MAX_RETRIES). When the retries are exhausted the agent sends a partial reply built by encode_partial that carries the values it could read and the words of every attempt. Nothing is sent when the model produced no text at all or when the interaction that carried the form is about to expire. A person at a terminal receives instead the line written by reprompt_person and their next line is matched against the same form. fill documents each piece of this loop and the decisions of the agent (AnswerOutcome, AnswerWithheld).

Modules

Module Content
constants The fence tag, the version, the vocabularies, the rung names, the issue codes and the size limits.
fence The grammar of the fence: finding, extracting and wrapping blocks.
validate The structural validation of a block and its error codes.
serialize The canonical JSON of a block.
parse From a message to ordered typed parts.
model The model view of a message.
templates The wording of each language.
alt The generated instruction of a form.
compose Building media, chart and form blocks.
coerce From a written answer to the canonical value of a field.
interpret The five rungs of the reply interpreter and the check of a reply.
draft The readable projection of an answer and its encoding as a reply block.
fill Reading the answer of a processor and asking again.

uai

Public API

The package exports these names in __all__. The groups are those of the source.

Group Names
Reading what arrived parse_message, to_model_text, parts_to_model_text, parse_reply, check_reply, interpret_reply, ReplyEvent, find_form, newest_form, find_reply, has_interactive_parts, fill_slots, read_answer, describe_answer, retry_prompt, reprompt_person, encode_partial, AnswerOutcome, AnswerWithheld
Writing what leaves build_form, compose_form, build_media, compose_media, build_chart, compose_chart, encode_reply, project_draft, gen_id, guess_mime, truncate_outside_fences, UaiError, re_ask, build_re_ask
Lower level validate_block, serialize_block, canonical_json, generate_form_alt, describe_field, interactive_fields, field_label, normalize_label, is_iso_date, is_https_url, extract_fences, fence_spans, has_fence, wrap_fence, scan_labeled, apply_canonical, coerce_text, check_canonical, check_constraints, parse_number, parse_date, project_value, num_to_text, Coerced, TEMPLATES, template_for
Contract terms FENCE_TAG, PROTOCOL_VERSION, BLOCK_TYPES, FIELD_TYPES, CHART_KINDS, TEXT_FORMATS, LANGS, DEFAULT_LANG, MAX_BLOCK_BYTES, MAX_ALT_CHARS, MAX_BLOCKS_MODEL_VIEW, MAX_INBOX_PEERS, MAX_MODEL_VIEW_BYTES, MAX_OUTPUT_BYTES, VIA_BLOCK, VIA_LABELED, VIA_BARE, VIA_CSV, VIA_FREETEXT, ISSUE_TYPE, ISSUE_BELOW_MIN, ISSUE_ABOVE_MAX, ISSUE_TOO_LONG, ISSUE_FORMAT, ISSUE_UNKNOWN_OPTION