Schema awareness

A Tiptap schema describes the elements the document can (and cannot) contain. Tiptap's schema awareness capabilities allow AI models to understand the document better.

Why schema awareness?

Without schema awareness, the AI model might generate content that the Tiptap editor does not support. For example, it might generate a table in a document that does not support tables. With schema awareness enabled, the AI model will know that table nodes are not supported and will refuse to generate them.

Guide: give schema awareness to your AI model

Integrate the schema awareness feature to an AI agent like the one built in the AI agent chatbot guide.

Step 1: get the schema awareness string

Get the schema awareness string from the AI Toolkit. This string contains a message describing the document's schema, that is optimized for the AI model.

const toolkit = getAiToolkit(editor)
// Retrieve the schema awareness string
const schemaAwareness = toolkit.getHtmlSchemaAwareness()

Step 2 (optional): configure custom nodes

If your document contains custom Nodes and Marks, add the addHtmlSchemaAwareness option to the extension configuration. This way, the AI model will be able to generate this custom Node or Mark accurately. For example, if you have a custom node called 'alert', you can allow the AI model to generate it by configuring it like this:

import { Node } from '@tiptap/core'

const CustomExtension = Node.configure({
  name: 'alert',

  addHtmlSchemaAwareness() {
    return {
      tag: 'div',
      name: 'Alert Box',
      description: `A highlighted box used to display important information, warnings, or tips to the user.
It can have inline content inside like text and formatting tags.`,
      attributes: [
        {
          attr: 'data-alert',
          value: '',
          description: 'Indicates that this is an alert box',
        },
        {
          attr: 'data-type',
          description:
            'The type of alert. Can be one of these 4 values: info, warning, error, or success',
        },
      ],
    }
  },

  // ... Other extension configuration options
})

Alternatively, you can configure the customNodes option of the getHtmlSchemaAwareness method. Learn more about the configuration options in the API Reference.

What about official Tiptap extensions?

Schema awareness for official Tiptap extensions is automatically supported by the AI Toolkit. They do not need to be configured. You can still override the default schema awareness information for official Tiptap extensions by extending the extension and adding the addHtmlSchemaAwareness option.

Step 3: add the schema awareness string to the system prompt

When you call the AI model, send the schema awareness string as a parameter to the API endpoint that returns the AI model response.

The code below shows an API endpoint built with Next.js and the Vercel AI SDK, like the one in the AI agent chatbot guide.

// app/page.tsx
import { useEditor } from '@tiptap/react'
import { getAiToolkit } from '@tiptap-pro/ai-toolkit'
import { useChat } from '@ai-sdk/react'
import { DefaultChatTransport } from 'ai'

// Inside the React component

const editor = useEditor()
const toolkit = getAiToolkit(editor)

// Get the schema awareness string
const schemaAwareness = toolkit.getHtmlSchemaAwareness()

const { messages } = useChat({
  transport: new DefaultChatTransport({
    api: '/api/chat',
    // Send the schema awareness string as a parameter to the API endpoint
    body: { schemaAwareness },
  }),
  // ...
})

Then, inside the API endpoint handler, add the schema awareness string to the end of the system prompt.

// app/api/chat/route.ts
import { openai } from '@ai-sdk/openai'
import { toolDefinitions } from '@tiptap-pro/ai-toolkit-ai-sdk'
import { convertToModelMessages, streamText, UIMessage } from 'ai'

export async function POST(req: Request) {
  // Get the schema awareness string from the request body
  const { messages, schemaAwareness }: { messages: UIMessage[]; schemaAwareness: string } =
    await req.json()

  const result = streamText({
    model: openai('gpt-5'),
    // Add the schema awareness string to end of the system prompt
    system: `You are an assistant that can edit rich text documents.
${schemaAwareness}`,
    messages: convertToModelMessages(messages),
    tools: toolDefinitions(),
  })

  return result.toUIMessageStreamResponse()
}

You've now set up schema awareness for your AI model.

End result

The app below is a variant of the AI agent chatbot demo with schema awareness configured. Try asking the AI to create an alert box: the AI will recognize the custom element and insert it into the document.

See the source code on GitHub.

Next steps