Reading OpenUI Lang
OpenUI Lang is a list of one-line assignments. Each line names a piece of the interface, and the names wire the pieces into a tree. Once you can read one line, you can read any program a model writes.
By the end of this lesson you can
- name every part of a program: statement, identifier,
root, reference, component call, arguments and values; - map a call such as
TextContent("Revenue", "large-heavy")onto its signature, argument by argument; - draw the element tree a program describes by following its references from
root; - state the three rules models most often break: every statement must be reachable, arguments are positional, and optional arguments can only be dropped from the end.
One program, labelled
Here is a complete program: a heading and a bar chart. It is four statements, one per line, and every statement has the same shape, identifier = Expression. Step through the figure to see what each part is called and what it does.
Anatomy of a program
- A statement is one line:
identifier = Expression. This program has four. Statements are separated by newlines outside brackets, so a long array may wrap over several lines and still be one statement. - The identifier on the left names the statement. Names are how the pieces connect. If two statements share a name, the later one wins, and a line with no
=is silently skipped. rootis the entry point. The renderer starts here and draws only what it can reach from here. Its value must be a component, and for the stock library that isStack.- A bare name inside an expression, such as
titleorrev, is a reference. It stands for the value of the statement with that name. The arrows show each reference landing on its definition, all of them further down the text. Forward references are allowed. - A capitalised name followed by parentheses is a component call:
Stack,TextContent,BarChart,Series. The name must exist in the component library the app gave the renderer. - Arguments are positional.
"Revenue"is the text because it comes first;"large-heavy"is the size because it comes second. There are no argument names in the language. - Everything else is a value: strings in double quotes, numbers, arrays in square brackets, and also
true,false,nulland{key: value}objects. A value can contain component calls and references, as[rev]does.
The table below lists every value form you will meet in this course. The ones in the last rows ($variables, @ calls, operators) only appear when the app turns on state or tools, and they get lessons of their own.
| Form | Example | Notes |
|---|---|---|
| Component call | Tag("Q3", null, "sm") | Becomes an element in the tree. |
| String | "Revenue ($k)" | Double quotes, backslash escapes. |
| Number | 820, 12.5, -1 | |
| Boolean, null | true, false, null | null also skips an optional argument. |
| Array | [title, chart] | Children lists are arrays. |
| Object | {required: true} | Keys are bare names, so here a colon is correct. |
| Reference | title | The value of the statement named title. |
| State, built-ins, operators | $city, @Count(rows), a + b | See Reactive state and Query and Mutation. |
From signature to call
How does the model know that the first argument of TextContent is the text? The app told it. Each component in the library is defined with a Zod schema, and the order of the keys in that schema becomes the order of the arguments. The generated system prompt prints that order as a signature line, and the parser uses the same order to turn the model's values back into named props.
One schema, one signature, one call
- The app defines
TextContentwith a Zod object whose keys aretext, thensize. That order is the whole contract. - The library turns the schema into a signature line for the system prompt:
TextContent(text: string, size?: "small" | "default" | "large" | "small-heavy" | "large-heavy"). A?marks an optional argument. Required keys come first in the schema so that optional ones can be left off the end. - The model writes only values, in signature order:
TextContent("Revenue", "large-heavy"). - The parser maps position 1 to the first key,
text, and checks it is a string. - Position 2 maps to
size."large-heavy"is one of the allowed values, so it passes. - The result is an element node with named props, exactly what a React component receives:
{ text: "Revenue", size: "large-heavy" }. - Swap the two values and the meaning changes.
textbecomes"large-heavy", which is a valid string.sizegets"Revenue", which is not an allowed size, so the parser reportstype-mismatchand drops that optional prop. The page shows the words "large-heavy" in the default size. Nothing crashes, and the screen is wrong.
The signature is the only description of a component the model ever sees, so it is worth reading the real ones. These are four of the stock library's lines, exactly as openuiLibrary prints them (descriptions trimmed):
Stack(children: any[], direction?: "row" | "column", gap?: "none" | "xs" | "s" | "m" | "l" | "xl" | "2xl", align?: …, justify?: …, wrap?: boolean) TextContent(text: string, size?: "small" | "default" | "large" | "small-heavy" | "large-heavy") Tag(text: string, icon?: Icon, size?: "sm" | "md" | "lg", variant?: "neutral" | "info" | "success" | "warning" | "danger") BarChart(labels: string[], series: Series[], variant?: "grouped" | "stacked", xLabel?: string, yLabel?: string, height?: number)
Series[] in BarChart means the slot only accepts Series elements. That comes from z.array(Series.ref) in the schema, covered in Defining components.
References build the tree
The text is a flat list of lines, but the interface is a tree. The tree comes from the references: start at root, replace every name with the statement it points to, and repeat. The order of lines in the text does not matter for the finished tree. It matters a great deal while the text is still streaming, which is the next lesson.
From lines to a tree
root = Stack([title, chart])creates aStackwith two children. Their names are known, their statements are not yet: both are unresolved.titlearrives and its slot becomes aTextContentelement.chartarrives as aBarChart. Its series list holds one more name,rev, which is now the only unresolved one.revarrives as aSeries. Every name resolves, so the tree is complete.noteis a valid statement, but nothing refers to it. There is no path fromroot, so it never renders. The parser raises no error; it lists the name inmeta.orphaned. This is the most common silent failure in model output: the model writes a piece and forgets to put it in a parent's children.- The finished tree is the same whatever order the lines came in. Write
revfirst androotlast and you get the same four elements once the text is complete.
Try it: edit a program
The editor re-renders 150 ms after you stop typing. Below it, a status line shows what the parser found: statements, unresolved names and orphaned statements. Errors appear in the list the real Renderer passes to onError. Work through the exercises in order. For each one, predict first, then make the change (type it, or press the button), then open "What should happen".
-
1. Add a Tag. Add this line, then add
badgeto therootarray betweentitleandchart. Thenullskips the optionaliconargument so that"sm"lands insize.badge = Tag("Q3", null, "sm", "success")Predict: where does the tag appear, and what happens if you add the line but forget to edit
root?What should happen
A green "Q3" pill appears between the heading and the chart, because children render in array order. Before you add
badgeto the array, nothing appears and the status line showsorphaned: badge: a valid statement that nothing reaches. -
2. Break it: reorder the arguments. Change the heading to
TextContent("large-heavy", "Revenue").Predict: does the heading disappear, show "Revenue", or something else? How many errors?
What should happen
The heading reads "large-heavy" in normal-sized text. The first value is always the text, so the text is now the size name.
"Revenue"lands insize, fails the enum check, and that optional prop is dropped with onetype-mismatcherror. Order is meaning: there is no way to say "this one is the size" except by putting it second. -
3. Reference a new statement. Add a note below everything else, then make it render.
note = Callout("info", "Source", "Q3 board pack, unaudited")Predict: what renders after you add only the line? Is there an error?
What should happen
With only the line, the page does not change and the error list stays empty. The status line shows
orphaned: note. Addnoteto therootarray and the callout appears where you put it in the list. A statement that is defined but never referenced is dropped without an error. -
4. Skip an argument in the middle. Change the tag from exercise 1 to
Tag("Q3", "sm"), leaving out thenull.Predict: is the tag small?
What should happen
The tag renders at its default size with a
type-mismatcherror:field "/icon" expects Icon but got string."sm"is the second value, so it goes to the second parameter,icon, not tosize. Optional arguments can be left off only at the end; to skip one in the middle, writenullin its place.
The editor runs the course's teaching simulator, a subset of @openuidev/react-lang that draws about 20 of the 95 stock components as plain HTML. The signatures, parse results and error messages above match the real parser; the visual styling does not.
Rules worth memorising
The generated system prompt lists these rules for the model. Models still break them, and when they do, the parser usually renders what it can without complaint. Knowing them lets you read a broken screen and see the cause.
OpenUI rule
Arguments are positional. They map to props by Zod key order. There is no name: value syntax for arguments: TextContent(text: "Revenue") is not a named argument, and the statement is dropped. When the model gets it wrong shows exactly what the parser makes of it.
OpenUI rule
Optional arguments can be omitted only from the end. To skip one in the middle, pass null: Tag("Q3", null, "sm", "success"). This is why schemas put required keys first.
OpenUI rule
Every statement except root must be referenced. The prompt tells the model "Unreferenced variables are silently dropped and will NOT render." The parser lists them in meta.orphaned and reports no error.
Myth
"The renderer draws statements in the order they appear." It draws the tree reachable from root, in the order of each children array. Line order only decides when each piece can appear during streaming.
Quick check
The signature is Callout(variant: "info" | "warning" | "error" | "success" | "neutral", title: string, description: string, visible?: boolean). Which call is correct?
visible is left off the end. Named arguments are not part of the language, and Callout("Q3", "Up 12%", "info") puts every value in the wrong slot.A program defines footer = TextContent("Updated today"), but the footer never shows and the error list is empty. What is the most likely cause?
meta.orphaned. Size is optional, and forward references mean line order does not matter for the final tree.Why do component schemas put required keys before optional ones?
null placeholder, and every call that forgot the placeholder would shift its later values into the wrong props.Recap
- A program is one
identifier = Expressionstatement per line.rootis the entry, and its value is a component. - Bare names are references. Following them from
rootgives the element tree; forward references are fine. - Arguments are positional, in the order of the component's Zod keys, which the prompt prints as a signature line.
nullskips an optional argument; only trailing ones can be omitted. - Mistakes are quiet: a swapped argument renders the wrong thing with one error, and an unreferenced statement renders nothing with no error.