GitHub - vercel-labs/json-render: AI → JSON → UI
Key Points
- 1json-render is a framework enabling AI to generate user interfaces like dashboards and widgets by safely constraining AI output to a predefined catalog of components and actions.
- 2It ensures predictable and guardrailed output by defining strict schemas for components and actions, allowing AI to generate JSON that can be streamed and rendered progressively.
- 3The system provides advanced features such as conditional visibility, rich actions with confirmations and callbacks, and built-in validation to streamline AI-powered UI development.
json-render is a framework designed to enable the generation of predictable, guardrailed, and fast user interfaces (UI) such as dashboards, widgets, and data visualizations, directly from natural language prompts provided by end-users. It addresses the inherent challenge of ensuring AI-generated UI is safe, consistent, and adheres to predefined structural and behavioral constraints.
The core methodology is predicated on a strict separation of concerns, involving three primary steps:
- Defining the Catalog: This step establishes the "guardrails" for the AI. Developers explicitly define a vocabulary of permissible UI components and actions using
@json-render/coreandZodfor schema validation.- Components: For each UI component (e.g.,
Card,Metric,Button), aZodschema is provided for itsprops. This schema dictates the exact structure and types of properties the AI is allowed to specify for that component. For instance, aCardcomponent might haveprops: z.object({ title: z.string() }), ensuringtitleis always a string. ThehasChildrenboolean property indicates if a component can accept child elements, enabling hierarchical UI structures. - Actions: The catalog also defines a set of named, abstract
actions(e.g.,export_report,refresh_data). These actions represent intents that the AI can declare, decoupling the AI's declaration from the actual implementation of the action.
- Components: For each UI component (e.g.,
- Registering Components for Rendering: This step defines *how* the cataloged components are rendered into actual UI elements. Developers provide a
registryobject where component names from the catalog are mapped to their corresponding React functional components.- Each registered React component receives an
elementprop, which is the AI-generated JSON object for that component (including itsprops), and achildrenprop for nested components. - This approach ensures that the rendering logic is entirely controlled by the developer. It allows for integration with existing design systems, state management, and data-binding mechanisms (e.g., via
useDataValuehooks to bindvaluePathto external data, oronActioncallbacks to trigger defined actions).
- Each registered React component receives an
- AI Generation and Progressive Rendering: The framework facilitates the interaction between the user prompt, the AI, and the rendering engine.
- The
useUIStreamhook from@json-render/reactmanages the communication with an AI API endpoint. It is designed for streaming, allowing the UI to render progressively as the AI generates the JSON tree, enhancing perceived performance. DataProviderandActionProvidercontexts make data and action handlers available to the rendered components, enabling dynamic and interactive UIs.- The
Renderercomponent then consumes the AI-generated JSON tree and the developer-defined componentregistryto display the UI.
- The
Key Features:
- Conditional Visibility: Components can specify complex visibility rules based on data paths, authentication status, or logical combinations (
and,or,not). This allows dynamic display of UI elements. For example,{"visible": {"auth": "signedIn"}}for an admin panel, or{"visible": {"and": [{"path": "/form/hasError"}, {"not": {"path": "/form/errorDismissed"}}]}}for an alert. - Rich Actions: Actions declared in the catalog can be enriched with parameters, confirmation dialogs (e.g.,
confirm: { title, message, variant }), and lifecycle callbacks (onSuccess,onError). Parameters can be dynamically sourced from data paths (e.g.,paymentId: {"path": "/selected/id"}). Success and error callbacks can trigger state updates within the UI itself (e.g.,{"set": {"/ui/success": true}}). - Built-in Validation: Input components can include an array of
checkswith predefined functions (e.g.,required,email) and custom error messages. Validation can be configured to trigger on specific events likeblur.
The overall workflow is: User Prompt → AI + Catalog (guardrailed) → JSON Tree (predictable, streamed) → Your React Components (rendered). This architecture ensures that AI's creative output is always channeled through a developer-defined, secure, and predictable UI grammar.