---
title: "Edit the document"
description: "Insert and stream AI-generated content in text, HTML, or JSON formats. Includes review options for user approval workflows."
canonical_url: "https://tiptap.dev/docs/content-ai/capabilities/ai-toolkit/api-reference/edit-the-document"
---

# Edit the document

Insert and stream AI-generated content in text, HTML, or JSON formats. Includes review options for user approval workflows.

The AI Toolkit provides methods to edit the document in different formats.

## `insertText`

Insert plain text into the editor.

### Parameters

- `content` (`string`): Text to insert
- `options?` (`InsertTextOptions`): Options for the `insertText` method
  - `position?` (`Range | number`): Where to insert. A `Range` (`{ from, to }`) replaces that range; a `number` inserts at that position. Default: entire document range.
  - `reviewOptions?` ([`ReviewOptions`](https://tiptap.dev/docs/content-ai/capabilities/ai-toolkit/api-reference/review-options.md)): Control preview/review behavior. [See available options](https://tiptap.dev/docs/content-ai/capabilities/ai-toolkit/api-reference/review-options.md).

### Returns

`void`

### Example

```ts
// Replace the entire document and show review markers
toolkit.insertText('AI content', { reviewOptions: { mode: 'review' } })
```

## `insertHtml`

Insert HTML into the editor.

### Parameters

- `content` (`string`): HTML to insert
- `options?` (`InsertHtmlOptions`): Options for the `insertHtml` method
  - `position?` (`Range | number`): Where to insert. A `Range` (`{ from, to }`) replaces that range; a `number` inserts at that position. Default: entire document range.
  - `reviewOptions?` ([`ReviewOptions`](https://tiptap.dev/docs/content-ai/capabilities/ai-toolkit/api-reference/review-options.md)): Control preview/review behavior. [See available options](https://tiptap.dev/docs/content-ai/capabilities/ai-toolkit/api-reference/review-options.md).

### Returns

`void`

### Example

```ts
// Insert a paragraph at the end of the document
toolkit.insertHtml('<p>AI paragraph</p>', { position: editor.state.doc.content.size })
```

## `insertJson`

Insert ProseMirror JSON into the editor.

### Parameters

- `content` (`any`): ProseMirror JSON node or slice
- `options?` (`InsertJsonOptions`): Options for the `insertJson` method
  - `position?` (`Range | number`): Where to insert. A `Range` (`{ from, to }`) replaces that range; a `number` inserts at that position. Default: entire document range.
  - `reviewOptions?` ([`ReviewOptions`](https://tiptap.dev/docs/content-ai/capabilities/ai-toolkit/api-reference/review-options.md)): Control preview/review behavior. [See available options](https://tiptap.dev/docs/content-ai/capabilities/ai-toolkit/api-reference/review-options.md).

### Returns

`void`

### Example

```ts
// Replace the entire document with a paragraph node
toolkit.insertJson({ type: 'paragraph', content: [{ type: 'text', text: 'AI' }] })
```

## `streamText`

Stream plain text content into the editor in real-time.

### Parameters

- `stream` (`AsyncIterable<string | Uint8Array>`): The stream of text content. It can be any object that implements the [Async Iterable protocol](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols), such as a [generator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_generators).
- `options?` (`StreamTextOptions`): Options for the `streamText` method
  - `position?` (`Range | number`): Where to insert. A `Range` (`{ from, to }`) replaces that range; a `number` inserts at that position. Default: entire document range.
  - `reviewOptions?` ([`ReviewOptions`](https://tiptap.dev/docs/content-ai/capabilities/ai-toolkit/api-reference/review-options.md)): Control preview/review behavior. [See available options](https://tiptap.dev/docs/content-ai/capabilities/ai-toolkit/api-reference/review-options.md).
  - `checkChunkForError?` (`(status: { chunk: string; content: string; range: Range }) => boolean`): A function to check if the chunk contains an error. Returns `true` if the chunk has an error, `false` otherwise. If `true`, the stream will be stopped and a `StreamingChunkCheckFailedError` will be thrown.
  - `onError?` (`(event: { error: unknown; chunk: string; content: string; range: Range }) => void`): A function to handle errors that occur during streaming.
  - `onChunkInserted?` (`(event: { chunk: string; content: string; range: Range }) => void`): A function to handle when a chunk is streamed. Called after the chunk is inserted into the editor.

### Returns

`Promise<void>`: A promise that resolves when the stream is complete.

### Example

```ts
const response = await fetch('/api/generate-text')
// The API endpoint returns a stream of text content
const stream = response.body

toolkit.streamText(stream)
```

## `streamHtml`

Stream HTML content into the editor in real-time.

### Parameters

- `stream` (`AsyncIterable<string | Uint8Array> | ReadableStream<string | Uint8Array>`): The stream of HTML content. It can be any object that implements the [Async Iterable protocol](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols), such as a [ReadableStream](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) or a [generator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_generators).
- `options?` (`StreamHtmlOptions`): Options for the `streamHtml` method
  - `position?` (`Range | number`): Where to insert. A `Range` (`{ from, to }`) replaces that range; a `number` inserts at that position. Default: entire document range.
  - `reviewOptions?` ([`ReviewOptions`](https://tiptap.dev/docs/content-ai/capabilities/ai-toolkit/api-reference/review-options.md)): Control preview/review behavior. [See available options](https://tiptap.dev/docs/content-ai/capabilities/ai-toolkit/api-reference/review-options.md).
  - `checkChunkForError?` (`(status: { chunk: string; content: string; range: Range }) => boolean`): A function to check if the chunk contains an error. Returns `true` if the chunk has an error, `false` otherwise. If `true`, the stream will be stopped and a `StreamingChunkCheckFailedError` will be thrown.
  - `onError?` (`(event: { error: unknown; chunk: string; content: string; range: Range }) => void`): A function to handle errors that occur during streaming.
  - `onChunkInserted?` (`(event: { chunk: string; content: string; range: Range }) => void`): A function to handle when a chunk is streamed. Called after the chunk is inserted into the editor.

### Returns

`Promise<void>`: A promise that resolves when the stream is complete.

### Example

```ts
const response = await fetch('/api/generate-html')
// The API endpoint returns a stream of HTML content
const stream = response.body
toolkit.streamHtml(stream)
```
