Overview
Level 3: Advanced · Module 8: Generative UI · 20 min read
Why this matters
Because Large Language Models are stochastic systems, Generative UI will occasionally fail: the model might output an unknown component name (FlightCardV3 instead of FlightCard), omit a non-optional prop (like a missing currency field), or pass an invalid date format that triggers a runtime error inside a third-party charting library. In standard React applications, an uncaught runtime error crashes the entire component tree, turning the user's screen completely white. In Generative UI, every component must be isolated inside an Error Boundary and supported by a multi-tier Fallback Ladder, guaranteeing that a broken widget degrades gracefully into an informative summary without disrupting the active chat session.
Learning objectives
- Design and implement a multi-tier Fallback Ladder for Generative UI.
- Isolate generative widgets with granular React Error Boundaries.
- Handle partial prop validation failures with intelligent default interpolation.
- Enforce brand consistency rules across AI-generated UI states.
- Capture and report component degradation events to observability platforms.
The 4-Tier Fallback Ladder
┌─────────────────────────────────────────────────────────────────────────────┐
│ TIER 1: Full Interactive Component (Optimal Experience) │
│ ➔ Props 100% valid, verified in registry, renders rich interactive widget. │
├─────────────────────────────────────────────────────────────────────────────┤
│ TIER 2: Simplified Static Card (Graceful Downgrade) │
│ ➔ Minor prop missing; renders key facts in a static summary card. │
├─────────────────────────────────────────────────────────────────────────────┤
│ TIER 3: Formatted Key-Value Inspector (Read-Only Fallback) │
│ ➔ Component unknown; renders structured key-value list for transparency. │
├─────────────────────────────────────────────────────────────────────────────┤
│ TIER 4: Plain Text Fallback + Raw JSON Toggle (Emergency Safety Net) │
│ ➔ Runtime crash; error boundary catches error, renders safe plain text. │
└─────────────────────────────────────────────────────────────────────────────┘graph TD
Payload["Model Tool Payload"] --> Gate1{"Is Component in Registry?"}
Gate1 -->|Yes| Gate2{"Do Props Pass Strict Zod Validation?"}
Gate1 -->|No| Tier3["Tier 3: Formatted Key-Value Inspector"]
Gate2 -->|Yes| Mount["Mount <ErrorBoundary><Component /></ErrorBoundary>"]
Gate2 -->|Minor Mismatch| Tier2["Tier 2: Simplified Static Summary Card"]
Mount --> RuntimeCheck{"Did Component throw runtime error?"}
RuntimeCheck -->|No| Success["Tier 1: Full Interactive Component Active!"]
RuntimeCheck -->|Runtime Crash!| Tier4["Tier 4: Catch Boundary -> Safe Text Fallback"]Implementation: The Generative Error Boundary
Every generative component must be isolated so a crash in an experimental widget never tears down the surrounding chat application:
// components/gen-ui/GenerativeErrorBoundary.tsx
"use client";
import React, { Component, ErrorInfo, ReactNode } from "react";
interface Props {
componentName: string;
rawPayload: any;
children: ReactNode;
}
interface State {
hasError: boolean;
error: Error | null;
}
export class GenerativeErrorBoundary extends Component<Props, State> {
public state: State = {
hasError: false,
error: null,
};
public static getDerivedStateFromError(error: Error): State {
return { hasError: true, error };
}
public componentDidCatch(error: Error, errorInfo: ErrorInfo) {
console.error(`[GenerativeUI Crash] Error in <${this.props.componentName}>:`, error, errorInfo);
// Send telemetry to Sentry / Datadog in production
}
public render() {
if (this.state.hasError) {
// Tier 4 Emergency Fallback
return (
<div className="my-3 p-4 rounded-xl border border-amber-200 dark:border-amber-900 bg-amber-50/60 dark:bg-amber-950/20 text-xs">
<div className="flex items-center gap-2 font-bold text-amber-800 dark:text-amber-300 mb-1">
<span>⚠️</span>
<span>Could not display interactive {this.props.componentName}</span>
</div>
<p className="text-gray-600 dark:text-gray-400 mb-2">
The data was received, but the interactive component encountered an unexpected error.
</p>
<details className="mt-2 text-gray-500 font-mono text-[11px]">
<summary className="cursor-pointer hover:underline">View raw data</summary>
<pre className="mt-1 p-2 bg-gray-900 text-gray-100 rounded overflow-x-auto">
{JSON.stringify(this.props.rawPayload, null, 2)}
</pre>
</details>
</div>
);
}
return this.props.children;
}
}Tier 3 Fallback: Formatted Key-Value Inspector
When the model emits a tool output that has no registered component, do not render an ugly raw JSON blob. Instead, render a clean key-value inspector:
// components/gen-ui/FormattedKeyValueFallback.tsx
export function FormattedKeyValueFallback({
type,
data,
}: {
type: string;
data: Record<string, any>;
}) {
return (
<div className="my-3 p-4 rounded-xl border border-gray-200 dark:border-gray-800 bg-gray-50 dark:bg-gray-900 text-xs">
<div className="flex justify-between items-center pb-2 mb-2 border-b border-gray-200 dark:border-gray-800">
<span className="font-bold text-gray-700 dark:text-gray-300 uppercase text-[10px] tracking-wider">
Structured Data: {type}
</span>
<span className="text-[10px] text-gray-400 font-mono">Unregistered Component</span>
</div>
<div className="grid grid-cols-2 gap-2 font-mono">
{Object.entries(data).map(([key, value]) => (
<div key={key} className="p-2 rounded bg-white dark:bg-gray-950 border border-gray-150 dark:border-gray-800/80">
<span className="text-gray-400 block text-[10px]">{key}</span>
<span className="font-semibold text-gray-800 dark:text-gray-200 truncate block">
{typeof value === "object" ? JSON.stringify(value) : String(value)}
</span>
</div>
))}
</div>
</div>
);
}Interview Angle: How to Answer in System Design
- Question: "How do you design error recovery and resilience for Generative UI when the model outputs corrupted or partial props?"
- Target Answer:
"Generative UI requires a Defense-in-Depth Fallback Ladder:
- Granular React Error Boundaries: Every generated component is wrapped in an isolated boundary, ensuring a runtime crash in one widget never crashes the entire chat page.
- Multi-Tier Degradation:
- If props pass Zod validation, we mount the rich interactive component (Tier 1).
- If non-critical props are missing, we interpolate safe defaults and render a static card (Tier 2).
- If the component name is unknown, we mount an accessible key-value inspector (Tier 3).
- If a component throws an uncaught JavaScript error, the boundary gracefully falls back to a clean disclosure with raw data (Tier 4).
- Telemetry: All degradation events log structured telemetry (model, componentName, missingKeys) to alert the engineering team to schema drift."
Quick Check
- Why is wrapping every generative component in a React Error Boundary mandatory?
- What are the four tiers of a Generative UI fallback ladder?
- How does a formatted key-value inspector improve user experience over an unformatted JSON dump?