OpenUI

Manage conversations from your server

Create, read, and store conversation turns on your server with openui.conversations, and check each thread is the signed-in user's.

openui.conversations reads and writes conversations with your API key, as your org. Use it in a server route or a background job: for example, to check a thread is the signed-in user's and store each new turn, without minting a frontend token.

lib/openui.ts
import { OpenUIClient } from "@openuidev/server";

// Reads THESYS_API_KEY. Pass appId when one key serves several apps.
export const openui = new OpenUIClient();
app/api/chat/route.ts
import { getSession } from "@/lib/auth"; // your session lookup
import { openui } from "@/lib/openui";

export async function POST(request: Request) {
  const session = await getSession(request);
  if (!session) return new Response("Unauthorized", { status: 401 });
  const userId = session.user.id;
  const { conversationId, message } = await request.json();
  const conversations = openui.chat.conversations;

  const conversation = conversationId
    ? await conversations.get(conversationId)
    : await conversations.create({ title: message.slice(0, 60) }, { userId });
  // A thread id from the browser isn't proof: check it's the signed-in user's.
  if (conversation.userId !== userId) return new Response("Not found", { status: 404 });

  // The thread so far, oldest first, as Chat Completions messages.
  const history = await conversations.items.list(conversation.id);
  const reply = await runYourModel([...history, { role: "user", content: message }]);

  // Store only the new turn: earlier messages are already there.
  await conversations.items.create(conversation.id, [
    { role: "user", content: message },
    { role: "assistant", content: reply },
  ]);
  return Response.json({ conversationId: conversation.id, reply });
}

Owners

Every call uses your API key, as your org: a call by id reads or changes that conversation, whoever owns it. userId names the owner in two places:

CalluserId
create(params, { userId })Required. The conversation belongs to that user, in the client's appId, so their frontend token reads it too.
list({ userId })Optional. Only that user's conversations, in every app. Without it, every conversation in your org.

For a background job with no signed-in user, create with an id of your own, such as "system".

A conversation id from the browser isn't proof it's the user's. Before reading or writing it, get it and check its userId is the signed-in user, as in the example above. Take userId from your session, never from the browser's query string or request body.

Methods

MethodDescription
list(params?)One page of conversations, newest first.
create({ title?, metadata? }, { userId })A new conversation for that user.
get(id, options?)One conversation.
update(id, { title?, metadata? }, options?)Renames it, replaces its metadata, or both.
delete(id, options?)Deletes it. A deleted conversation can't be restored.
items.list(id, params?)One page of its items, newest first.
items.create(id, items, options?)Adds items in order, and returns them as stored.
items.get(id, itemId, options?)One item.
items.update(id, itemId, { content?, status? }, options?)Changes an item's content or status.

options takes a signal that cancels the call. list and items.list take it in params, with limit (1 to 100, default 100), and after or before for the page after lastId or before firstId. items.list also takes order: "asc" to read a thread from its start.

list and items.list return { data, hasMore, firstId, lastId }. To read a whole thread, pass lastId back as after:

const thread = [];
let after: string | undefined;
do {
  const page = await openui.conversations.items.list(id, { order: "asc", after });
  thread.push(...page.data);
  after = page.hasMore ? page.lastId : undefined;
} while (after);

Chat Completions and LangGraph messages

openui.chat.conversations and openui.langgraph.conversations are openui.conversations in your SDK's messages, in and out. Their conversation calls are the same; only items changes:

items.create(id, messages, options?) takesand returnsitems.list(id, options?) returns
openui.conversationsItemsThe items as storedOne page of items
openui.chat.conversationsChat Completions messagesThe turn as stored, as Chat Completions messagesThe whole thread, oldest first, as Chat Completions messages
openui.langgraph.conversationsLangChain messages (BaseMessage), or the plain ones it returnsThe turn as stored, as { role, content } messagesThe whole thread, oldest first, as { role, content } messages LangGraph takes

System messages are skipped: they're instructions, not history. A tool call is stored only with its result, so a turn stopped mid tool call leaves the thread valid for the next request. What's skipped isn't stored, so items.create doesn't return it. The message types come from openai and @langchain/core: install the one you use.

LangGraph
import { HumanMessage } from "@langchain/core/messages";

const history = await openui.langgraph.conversations.items.list(threadId);
const { messages } = await graph.invoke({ messages: [...history, new HumanMessage(message)] });

// Store only the new turn: the user's message and what the graph added after it.
await openui.langgraph.conversations.items.create(threadId, messages.slice(history.length));

items.list and items.create return plain objects, such as { role: "ai", content, tool_calls }, so they never load LangChain themselves. LangGraph's messages reducer turns them into LangChain messages; elsewhere, use coerceMessageLikeToMessage from @langchain/core/messages. items.create takes them back as is, next to LangChain messages.

The conversation

FieldDescription
idconv_….
titleThe title, if it has one.
metadataUp to 16 string pairs: keys up to 64 characters, values up to 512.
userId, appIdThe user it belongs to, and their app.
createdAtUnix milliseconds.

Items

Items keep the Conversations API shape: snake_case fields and created_at in unix seconds. An item read back can be added to another conversation as is. Narrow on type:

typeFields
messagerole and content: a string, or parts such as { type: "input_text", text }.
function_callcall_id, name, and arguments, a JSON string.
function_call_outputcall_id and output.

To add Chat Completions or LangGraph messages, use openui.chat or openui.langgraph. System messages can't be stored: they're instructions, not history. items.create takes up to 100 items, and makes no call for none.

Errors

A failed call throws OpenUIApiError, with a code and the HTTP status.

StatuscodeWhen
404http_errorThe conversation or item doesn't exist.
400invalid_inputA missing or bad userId or id.
400http_errorOpenUI Cloud rejected an item, such as a system message.

On this page