When the model gets it wrong
A model will sometimes invent a component, put an argument in the wrong place, or forget a line. OpenUI does not throw the whole answer away when that happens. The parser drops the broken piece, renders everything else, and tells you exactly what it dropped, in a form you can hand straight back to the model.
By the end of this lesson you can
- predict what renders when one statement in a program is broken;
- name every validation error code and write a one-line program that triggers it;
- spot the two mistakes that raise no error at all, orphaned statements and lines without
=, and the colon argument, whose errors never name the colon; - tell a fatal
no-rooterror from ano-rootwarning; - wire
onErrorinto a loop that asks the model to fix its own output.
The parser keeps going
The parser works statement by statement. When one statement is wrong, it drops that statement (or just the bad argument, when the argument is optional) and keeps the rest. Each problem becomes one entry in meta.errors with a code, the statement id, and a message written to be read by a model. The figure walks through the broken program from the first lab, one line at a time.
Seven lines in, one component out
- The parser splits the text into seven statements and starts from
root, which lists five children. okis a validTextContentcall. It renders, whatever happens to its neighbours.title = TextContent()leaves out the requiredtext. That ismissing-required, and the message carries the full signature, so a model reading it knows what to write.chartpasses42wherelabelsneeds an array (type-mismatch) and five arguments to a three-prop component (excess-args). A required prop with a bad value has no fallback, so the whole chart is dropped.Sparklineis not in the library.unknown-componentlists the components that are, which is the most useful thing a model can read at that moment.nameduses colon syntax. The parser does not understand names, so the line turns into three positional arguments withnullfirst:excess-argsplusnull-required. Dropped.strayis perfectly valid, but nothing references it. It is listed inmeta.orphanedand raises no error at all.- The result:
rootrenders with one child, and six errors are ready for the Renderer'sonError. The page did not go blank.
Key idea
A broken statement costs you that statement, not the page. Each error names the statement it came from, so you always know what is missing from the screen and why.
The same program, run for real
The first lab parses this exact program with the real @openuidev/lang-core 1.0.1 parser. Run it with cd openui && node 01-parse-stream/stream.mjs; the broken part comes after the streaming table.
The real output
broken program errors: [missing-required] title: missing required field "/text" — signature: TextContent(text*: string, size: "small"|"default"|"large-heavy") [type-mismatch] chart: field "/labels" expects array but got number [excess-args] chart: BarChart takes 3 arg(s), got 5 (2 excess dropped) [unknown-component] extra: Unknown component "Sparkline" — not found in catalog or builtins. Available components: Stack, TextContent, Ca [excess-args] named: TextContent takes 2 arg(s), got 3 (1 excess dropped) [null-required] named: required field "/text" cannot be null — signature: TextContent(text*: string, size: "small"|"default"|"large-h rendered children: [ 'TextContent' ] orphaned: [ 'stray' ]
The lab cuts each message at 110 characters. The full unknown-component message ends with every component in the library: Stack, TextContent, Callout, BarChart, Series, Table, Col.
Trigger every error code yourself
The parser has eight validation codes. Pick one below and the editor loads the smallest program that triggers it. Read the error, look at what still rendered, then edit the program until the error goes away.
| Code | What triggers it | What happens |
|---|---|---|
missing-required | A required prop is left out | The prop's default if it has one, otherwise the component is dropped |
null-required | A required prop is null | Same as missing-required |
type-mismatch | Wrong type or enum value, or plain data where only a component fits | Default if any; a bad optional value is left out; a bad required value drops the component |
excess-args | More arguments than props | Extras dropped, the component still renders |
unknown-component | A name that is not in the library | Component dropped |
inline-reserved | Query or Mutation written inside another expression | Evaluates to nothing |
unknown-function | @Name that is not a built-in, an action, or a library function | The call is null, the statement is kept |
no-root | No usable entry statement | A warning when the fallback applies, otherwise fatal |
The editor runs the course's teaching simulator, a subset of @openuidev/react-lang with about 20 of the 95 stock components, so its "Available components" list is shorter than the real one. The codes and message texts are the real ones. The real Renderer reports parser errors only after the stream ends; the editor always treats your text as a finished response.
Two more codes come from the Renderer rather than the validator: parse-failed (the text held no statements at all) and parse-exception (the parser crashed). Query, Mutation and render problems have codes of their own, covered in the Query and Mutation lesson.
Errors that fix themselves
While a response is still streaming, errors come and go. A statement cut off halfway through has missing arguments, and a reference to a statement that has not arrived yet is null for the moment. The lab's streaming table shows it: at chunk 4, chart has arrived but its regions have not, so the parser reports null-required and missing-required. Chunk 5 is clean, then the half-written tbl raises missing-required at chunks 6 and 7. By chunk 8 the last statement is complete and the list is empty, and it stays empty for the final chunk.
chunk chars stmts rootPartial unresolved errors
3 144 3 true ["chart","tbl"] -
4 192 4 true ["regions","tbl"] null-required,missing-required
5 240 5 true ["rev","tbl"] -
6 288 6 true ["tbl"] missing-required
7 336 7 true [] missing-required
8 384 7 true [] -
9 396 7 false [] -
OpenUI rule
The parser reports errors on every parse, but renderers such as the React Renderer hold them back until the stream ends. While streaming, onError is called with [] once (and only if the previous response had errors) to clear them; after the last chunk it gets the real list.
Failures that make no noise
Three mistakes are worth learning by sight, because onError will not tell you about the first and third, and the second is easy to misread.
1. An orphaned statement
root = Stack([greet])
greet = TextContent("Hello")
note = TextContent("Never shown")
note is valid, but no statement reachable from root uses it. It is an orphaned statement: it does not render, it raises no error, and its id appears in meta.orphaned. The generated system prompt warns the model about exactly this: "Unreferenced variables are silently dropped and will NOT render."
2. Colon arguments
row = Stack([left, right], direction: "row")
Arguments are positional. The parser has no named-argument syntax, so direction: "row" becomes extra positional arguments: the name direction is read as an unknown reference (null), and the value "row" lands two places to the right of where it was meant to go. In Stack that is the align slot, so you get type-mismatch on /align and a column instead of a row. On a component with fewer props the same mistake shows up as excess-args plus null-required and the whole component disappears. The errors are there, but they never say "colon": you have to recognise the pattern.
3. A line without =
root = Stack([title, greet])
title TextContent("Report")
greet = TextContent("Hello")
A line with no = (or no identifier) is not a statement, and the parser skips it without a word. Here title is then a reference to nothing: it stays in meta.unresolved and is dropped from the list. No error code says so.
Common misconception
"onError received an empty list, so the whole program rendered." Not necessarily. Orphaned statements and skipped lines never produce an error. If something is missing on screen, read meta.orphaned and meta.unresolved from onParseResult as well.
No root: an error or a warning
Every program should start with root = Stack(...) (or whatever root component the library names). When the root line is missing, one of two things happens:
- Some statement calls the root component. The first one is used as the entry. The page renders, and you get
no-rootwithseverity: "warning": "No root statement. Rendering the first statement that calls the root component Stack". - No statement calls it. Nothing renders, and
no-rootis an error: "No root statement, and no statement calls the root component Stack". Arootline whose value is not a component (root = "Hello") is fatal too: "root does not evaluate to a component".
The fallback only counts if that statement is valid itself. If it is dropped, for example main = Stack(42), you get the error instead, and its message still says no statement calls the root component, which is misleading. The component name in both messages is the library's root, Stack here.
no root, Stack fallback: [ '[no-root/warning] rendered=true' ] no root, no Stack: [ '[no-root/error] rendered=false' ]
OpenUI rule
With no root statement, the first statement that calls the library's root component is the entry. It renders, with a no-root warning. In every other case nothing renders. An error with severity: "warning" did not stop the page from rendering, so skip warnings when you send errors back to the model.
Break it: one response, every mistake
The broken sample collects the mistakes models actually make: a bad enum value, a number where a list belongs, a colon argument, an invented component, a missing required prop, an invented @ function, an orphaned statement and a line with no =. Six of them show up in the error list; two make no noise at all.
In the simulator, an unresolved reference is drawn as a grey skeleton so you can see where it would go. The real parser simply drops it.
For each change below, predict what happens first, then make it in the editor.
Feed the errors back
Every message in meta.errors was written for a model to read: it names the statement, says what was expected, and, for a missing prop or an unknown component, includes the signature or the list of components. That makes a correction loop cheap to build. The Renderer passes the errors to onError; your app turns them into a short message and sends it back as the next turn.
The self-correction loop
- Your server sends the generated system prompt and the user's message to the model.
- The model streams OpenUI Lang. Your server passes the chunks through to the browser, where the
Rendererparses the growing text as it arrives. - The
Rendererdraws everything that parses and drops the broken statements. Errors are held back while the stream is running. - When the stream ends,
onErrorreceives the list: parser, runtime, Query and Mutation errors, each withsource,code,messageand usuallystatementId. Your app drops the warnings. - The app formats the rest into one message ("Fix these errors:" followed by one line per error) and sends it to the model as a new turn.
- The model answers with a corrected program. When it parses cleanly,
onErroris called with[], which is your signal that the loop is done.
This is what the model would receive for the lab's broken program:
Fix these errors: [parser] "title": missing required field "/text" — signature: TextContent(text*: string, size: "small"|"default"|"large-heavy") [parser] "chart": field "/labels" expects array but got number [parser] "extra": Unknown component "Sparkline" — not found in catalog or builtins. Available components: Stack, TextContent, Callout, BarChart, Series, Table, Col
- Send code errors only.
onErroralso receives failed Query and Mutation calls: codestool-error,tool-not-foundandmcp-error, withsourceset to"query"or"mutation". Filter those out of the correction message bysourceorcode, and retry the tool instead. Regenerating the program does not fix a failed tool. - Cap the loop. One or two correction rounds is plenty. A model that keeps inventing the same component needs a better prompt or a different library, which is what the next two lessons are about.
- Without
onError, theRendererlogs errors toconsole.warninstead of swallowing them.
OpenUI rule
onError receives structured errors suitable for sending back to the model for correction, and is called with [] when they are resolved. OpenUI's hosted Gateway and its Autofix API offer the same repair as a service; both are optional.
Quick check
The model writes chart = BarChart(42, [s1]) in an otherwise valid dashboard. What does the user see?
labels is required and 42 is not an array. A required prop with a bad value and no default drops the component. Only that statement is lost; everything else renders.A program defines summary = TextContent("..."), but no other statement uses summary. What reaches onError?
meta.orphaned when something is missing.A response has no root line, but its first statement is page = Stack([title, chart]). What happens?
no-root entry has severity: "warning", and warnings should be skipped in a correction loop.Recap
- The parser is permissive: a broken statement is dropped and the rest renders. Each problem becomes one entry in
meta.errors. - Eight validation codes:
missing-required,null-required,type-mismatch,excess-args,unknown-component,inline-reserved,unknown-function,no-root. - Silent failures: orphaned statements (
meta.orphaned) and lines without=(skipped). Colon arguments do raise errors, but the messages never mention the colon. no-rootis a warning when a statement calls the root component and the page renders; otherwise nothing renders and it is an error.- Errors mid-stream are normal and clear themselves. The
Rendererreports toonErrorafter the stream ends, and the messages are written to be sent back to the model.