Skip to main content
Generative user interfaces (Generative UI) allows agents to go beyond text and generate rich user interfaces. This enables creating more interactive and context-aware applications where the UI adapts based on the conversation flow and AI responses. Agent Chat showing a prompt about booking/lodging and a generated set of hotel listing cards (images, titles, prices, locations) rendered inline as UI components. LangSmith supports colocating your React components with your graph code. This allows you to focus on building specific UI components for your graph while easily plugging into existing chat interfaces such as Agent Chat and loading the code only when actually needed.

Tutorial

1. Define and configure UI components

First, create your first UI component. For each component you need to provide an unique identifier that will be used to reference the component in your graph code.
src/agent/ui.tsx
Next, define your UI components in your langgraph.json configuration:
The ui section points to the UI components that will be used by graphs. By default, we recommend using the same key as the graph name, but you can split out the components however you like, see Customise the namespace of UI components for more details. LangSmith will automatically bundle your UI components code and styles and serve them as external assets that can be loaded by the LoadExternalComponent component. Some dependencies such as react and react-dom will be automatically excluded from the bundle. CSS and Tailwind 4.x is also supported out of the box, so you can freely use Tailwind classes as well as shadcn/ui in your UI components.

2. Send the UI components in your graph

src/agent.py

3. Handle UI elements in your React application

On the client side, you can use useStream() and LoadExternalComponent to display the UI elements.
src/app/page.tsx
Behind the scenes, LoadExternalComponent will fetch the JS and CSS for the UI components from LangSmith and render them in a shadow DOM, thus ensuring style isolation from the rest of your application.

How-to guides

Provide custom components on the client side

If you already have the components loaded in your client application, you can provide a map of such components to be rendered directly without fetching the UI code from LangSmith.

Show loading UI when components are loading

You can provide a fallback UI to be rendered when the components are loading.

Customise the namespace of UI components.

By default LoadExternalComponent will use the assistantId from useStream() hook to fetch the code for UI components. You can customise this by providing a namespace prop to the LoadExternalComponent component.

Access and interact with the thread state from the UI component

You can access the thread state inside the UI component by using the useStreamContext hook.

Pass additional context to the client components

You can pass additional context to the client components by providing a meta prop to the LoadExternalComponent component.
Then, you can access the meta prop in the UI component by using the useStreamContext hook.

Streaming UI messages from the server

You can stream UI messages before the node execution is finished by using the onCustomEvent callback of the useStream() hook. This is especially useful when updating the UI component as the LLM is generating the response.
Then you can push updates to the UI component by calling ui.push() / push_ui_message() with the same ID as the UI message you wish to update.

Remove UI messages from state

Similar to how messages can be removed from the state by appending a RemoveMessage you can remove an UI message from the state by calling remove_ui_message / ui.delete with the ID of the UI message.

Learn more