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's schema.

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.

getSchemaAwareness

Returns a string describing the document's schema. This string should be added to the end of the system prompt that is sent to the AI model.

Currently, the getSchemaAwareness method only supports AI models that generate HTML content. We plan to add support for other formats in the future.

Parameters (GetSchemaAwarenessOptions)

  • customNodes? (SchemaAwarenessItem[]): Custom schema awareness items to include in addition to the default ones. Default: [].

Every SchemaAwarenessItem object contains these properties:

  • extensionName (string): The name of the extension that provides this element
  • tag (string): The HTML tag name for this element
  • name (string): The human-readable name of the element in English
  • description? (string | null): Explanation of what the element is and how it is displayed
  • attributes? (SchemaAwarenessItemAttribute[]): Possible attributes of the HTML tag. If undefined, there are no attributes. Each SchemaAwarenessItemAttribute object contains these properties:
    • name (string): The name of the attribute in the HTML code
    • value? (string): If value is not undefined, the attribute always has that value for this element. This is used for attributes that have fixed values
    • description? (string | null): Explanation of the attribute in English for AI model understanding

Returns

  • string: Human-readable schema awareness text suitable for system prompts

Example

If your document contains custom nodes or marks, you can include them in the schema awareness string by providing the customNodes option.

// Include a custom node in the awareness text
const awareness = toolkit.getSchemaAwareness({
  customNodes: [
    {
      extensionName: 'alert',
      tag: 'div[data-alert]',
      name: 'Alert Box',
      description:
        'A highlighted box used to display important information, warnings, or tips to the user',
      // Describe the HTML attributes of the node as it is rendered in HTML
      attributes: [
        {
          name: 'data-alert',
          // Specify the "value" property if the attribute always has that value
          value: '',
          description: 'Indicates that this is an alert box',
        },
        {
          name: 'data-type',
          description: 'The type of alert: info, warning, error, or success',
        },
      ],
    },
  ],
})