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-root error from a no-root warning;
  • wire onError into 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

What the model wrote What the parser produced root = Stack([ok, title, chart, extra, named]) ok = TextContent("This line still renders") title = TextContent() chart = BarChart(42, [], "grouped", "x", "y") extra = Sparkline([1, 2, 3]) named = TextContent(text: "colon args…") stray = TextContent("defined but never…") renders Rendered UI This line still renders dropped meta.errors missing-required · title dropped type-mismatch · chart excess-args · chart dropped unknown-component · extra dropped excess-args · named null-required · named unreached meta.orphaned: ["stray"] 1 child rendered · 6 errors for onError · 1 orphan, no error
  1. The parser splits the text into seven statements and starts from root, which lists five children.
  2. ok is a valid TextContent call. It renders, whatever happens to its neighbours.
  3. title = TextContent() leaves out the required text. That is missing-required, and the message carries the full signature, so a model reading it knows what to write.
  4. chart passes 42 where labels needs 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.
  5. Sparkline is not in the library. unknown-component lists the components that are, which is the most useful thing a model can read at that moment.
  6. named uses colon syntax. The parser does not understand names, so the line turns into three positional arguments with null first: excess-args plus null-required. Dropped.
  7. stray is perfectly valid, but nothing references it. It is listed in meta.orphaned and raises no error at all.
  8. The result: root renders with one child, and six errors are ready for the Renderer's onError. 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.

CodeWhat triggers itWhat happens
missing-requiredA required prop is left outThe prop's default if it has one, otherwise the component is dropped
null-requiredA required prop is nullSame as missing-required
type-mismatchWrong type or enum value, or plain data where only a component fitsDefault if any; a bad optional value is left out; a bad required value drops the component
excess-argsMore arguments than propsExtras dropped, the component still renders
unknown-componentA name that is not in the libraryComponent dropped
inline-reservedQuery or Mutation written inside another expressionEvaluates to nothing
unknown-function@Name that is not a built-in, an action, or a library functionThe call is null, the statement is kept
no-rootNo usable entry statementA 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:

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

  1. Your server sends the generated system prompt and the user's message to the model.
  2. The model streams OpenUI Lang. Your server passes the chunks through to the browser, where the Renderer parses the growing text as it arrives.
  3. The Renderer draws everything that parses and drops the broken statements. Errors are held back while the stream is running.
  4. When the stream ends, onError receives the list: parser, runtime, Query and Mutation errors, each with source, code, message and usually statementId. Your app drops the warnings.
  5. 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.
  6. The model answers with a corrected program. When it parses cleanly, onError is 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

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?

  • The dashboard without the chart, and onError gets a type-mismatch for chart
  • A blank page, because the program failed validation
  • A chart with no labels
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?

  • Nothing: it is listed in meta.orphaned and not rendered
  • An unknown-component error for the summary statement
  • A no-root warning, since summary is not the root
An orphaned statement is valid code that nothing reaches. It raises no error, which is why you should also look at meta.orphaned when something is missing.

A response has no root line, but its first statement is page = Stack([title, chart]). What happens?

  • Nothing renders, and no-root is an error
  • The parser renames page to root silently, with no error
  • page renders as the entry, with a no-root warning you should not send back to the model
The fallback uses the first statement that calls the library's root component. The page renders, so the no-root entry has severity: "warning", and warnings should be skipped in a correction loop.

Recap