> ## Documentation Index
> Fetch the complete documentation index at: https://botpress-ak-docs-20-document-updating-variables-from-outsid.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Zai reference

> Complete reference for the Zai package.

## `Zai` class

The package exports one class: `Zai`. This class gives you access to the library's methods.

To create a new `Zai` instance using your Botpress client:

```ts theme={null}
const client = new Client({ botId: 'YOUR_BOT_ID', token: 'YOUR_TOKEN' })
const zai = new Zai({ client })
```

**Parameters**

<ResponseField name="options" type="ZaiConfig" required>
  <Expandable>
    <ResponseField name="client" type="Client" required>
      A `Client` object created using the [official Botpress client](https://www.npmjs.com/package/@botpress/client).
    </ResponseField>

    <ResponseField name="userId" type="string">
      The ID of the user consuming the API. If not provided, requests will be made without user attribution.
    </ResponseField>

    <ResponseField name="modelId" type="string" default="best">
      The ID of the LLM you want to use.

      Available options:

      * `best`: The best available model
      * `fast`: The fastest available model
      * Custom model ID in format `provider:model-name`. For example: `openai:gpt-4`
    </ResponseField>

    <ResponseField name="activeLearning" type="ActiveLearning">
      <Expandable>
        <ResponseField name="enable" type="boolean" required default={false}>
          Enables active learning.
        </ResponseField>

        <ResponseField name="tableName" type="string" required default="ActiveLearningTable">
          Name of the table to store active learning tasks in.
        </ResponseField>

        <ResponseField name="taskId" type="string" default="default">
          The ID of the task.
        </ResponseField>
      </Expandable>
    </ResponseField>

    <ResponseField name="namespace" type="string" default="zai">
      Namespace for organizing tasks and data.
    </ResponseField>
  </Expandable>
</ResponseField>

## Methods

Here's a reference for all methods available with the `Zai` object.

<Note>
  All of the methods below return a `Response` object. If you just await the method's result, it will return simplest form of the result. However, the `Response` object also has [its own methods](#response-methods) for accessing the result, event handling, and request control.
</Note>

### `check()`

Checks whether a condition is true or false for the given input.

```ts theme={null}
const result = await zai.check(input, condition, options)
```

**Parameters**

<ResponseField name="input" type="unknown" required>
  The input data to check the condition against.
</ResponseField>

<ResponseField name="condition" type="string" required>
  The condition to check against the input.
</ResponseField>

<ResponseField name="options" type="CheckOptions">
  <Expandable>
    <ResponseField name="examples" type="Array<Example>" default="[]">
      Examples to check the condition against.

      <Expandable>
        <ResponseField name="input" type="unknown" required>
          The input for the example.
        </ResponseField>

        <ResponseField name="check" type="boolean" required>
          Whether the condition is true for this example.
        </ResponseField>

        <ResponseField name="reason" type="string">
          The reason for the decision.
        </ResponseField>

        <ResponseField name="condition" type="string">
          The condition for this specific example.
        </ResponseField>
      </Expandable>
    </ResponseField>
  </Expandable>
</ResponseField>

**Returns**

<ResponseField name="Response" type="Response<CheckResult, boolean>">
  <Expandable>
    <ResponseField name="value" type="boolean">
      Whether the condition is true or not.
    </ResponseField>

    <ResponseField name="explanation" type="string">
      The explanation of the decision.
    </ResponseField>
  </Expandable>
</ResponseField>

***

### `extract()`

Extracts one or many elements from an arbitrary input using a schema.

```ts theme={null}
const result = await zai.extract(input, schema, options)
```

**Parameters**

<ResponseField name="input" type="unknown" required>
  The input data to extract elements from.
</ResponseField>

<ResponseField name="schema" type="ZodSchema" required>
  The Zod schema defining the structure of the data to extract.
</ResponseField>

<ResponseField name="options" type="ExtractOptions">
  <Expandable>
    <ResponseField name="instructions" type="string">
      Instructions to guide the user on how to extract the data.
    </ResponseField>

    <ResponseField name="chunkLength" type="number" default="16000">
      The maximum number of tokens per chunk (100-100,000).
    </ResponseField>

    <ResponseField name="strict" type="boolean" default="true">
      Whether to strictly follow the schema or not.
    </ResponseField>
  </Expandable>
</ResponseField>

**Returns**

<ResponseField name="Response" type="Response<T>">
  The extracted data matching the provided schema.
</ResponseField>

***

### `filter()`

Filters elements of an array against a condition.

```ts theme={null}
const result = await zai.filter(input, condition, options)
```

**Parameters**

<ResponseField name="input" type="array" required>
  The array of elements to filter.
</ResponseField>

<ResponseField name="condition" type="string" required>
  The condition to filter elements against.
</ResponseField>

<ResponseField name="options" type="FilterOptions">
  <Expandable>
    <ResponseField name="tokensPerItem" type="number" default="250">
      The maximum number of tokens per item (1-100,000).
    </ResponseField>

    <ResponseField name="examples" type="Array<FilterExample>" default="[]">
      Examples to filter the condition against.

      <Expandable>
        <ResponseField name="input" type="unknown" required>
          The input for the example.
        </ResponseField>

        <ResponseField name="filter" type="boolean" required>
          Whether this item should be filtered (kept).
        </ResponseField>

        <ResponseField name="reason" type="string">
          The reason for the filtering decision.
        </ResponseField>
      </Expandable>
    </ResponseField>
  </Expandable>
</ResponseField>

**Returns**

<ResponseField name="Response" type="Response<array>">
  The filtered array containing only elements that match the condition.
</ResponseField>

***

### `label()`

Tags the provided input with a list of predefined labels.

```ts theme={null}
const result = await zai.label(input, labels, options)
```

**Parameters**

<ResponseField name="input" type="unknown" required>
  The input data to label.
</ResponseField>

<ResponseField name="labels" type="Record<string, string>" required>
  A mapping of label keys to their descriptions/questions.
</ResponseField>

<ResponseField name="options" type="LabelOptions">
  <Expandable>
    <ResponseField name="examples" type="Array<LabelExample>" default="[]">
      Examples to help make labeling decisions.

      <Expandable>
        <ResponseField name="input" type="unknown" required>
          The input for the example.
        </ResponseField>

        <ResponseField name="labels" type="Record<string, LabelResult>" required>
          The labels for this example.

          <Expandable>
            <ResponseField name="label" type="'ABSOLUTELY_NOT' | 'PROBABLY_NOT' | 'AMBIGUOUS' | 'PROBABLY_YES' | 'ABSOLUTELY_YES'" required>
              The confidence level for this label.
            </ResponseField>

            <ResponseField name="explanation" type="string">
              The explanation for this label decision.
            </ResponseField>
          </Expandable>
        </ResponseField>
      </Expandable>
    </ResponseField>

    <ResponseField name="instructions" type="string">
      Instructions to guide the labeling process.
    </ResponseField>

    <ResponseField name="chunkLength" type="number" default="16000">
      The maximum number of tokens per chunk (100-100,000).
    </ResponseField>
  </Expandable>
</ResponseField>

**Returns**

<ResponseField name="Response" type="Response<LabelResults, BooleanLabels>">
  <Expandable>
    <ResponseField name="[labelKey]" type="LabelResult">
      For each label key provided:

      <Expandable>
        <ResponseField name="explanation" type="string">
          The explanation for this label decision.
        </ResponseField>

        <ResponseField name="value" type="boolean">
          Whether this label applies to the input.
        </ResponseField>

        <ResponseField name="confidence" type="number">
          The confidence level (0-1) for this decision.
        </ResponseField>
      </Expandable>
    </ResponseField>
  </Expandable>
</ResponseField>

***

### `rewrite()`

Rewrites a string according to the provided prompt.

```ts theme={null}
const result = await zai.rewrite(original, prompt, options)
```

**Parameters**

<ResponseField name="original" type="string" required>
  The original text to rewrite.
</ResponseField>

<ResponseField name="prompt" type="string" required>
  The prompt describing how to rewrite the text.
</ResponseField>

<ResponseField name="options" type="RewriteOptions">
  <Expandable>
    <ResponseField name="examples" type="Array<RewriteExample>" default="[]">
      Examples to guide the rewriting.

      <Expandable>
        <ResponseField name="input" type="string" required>
          The input text for the example.
        </ResponseField>

        <ResponseField name="output" type="string" required>
          The expected output for the example.
        </ResponseField>

        <ResponseField name="instructions" type="string">
          Specific instructions for this example.
        </ResponseField>
      </Expandable>
    </ResponseField>

    <ResponseField name="length" type="number">
      The maximum number of tokens to generate (10-16,000).
    </ResponseField>
  </Expandable>
</ResponseField>

**Returns**

<ResponseField name="Response" type="Response<string>">
  The rewritten text according to the prompt.
</ResponseField>

***

### `summarize()`

Summarizes a text of any length to a summary of the desired length.

```ts theme={null}
const result = await zai.summarize(original, options)
```

**Parameters**

<ResponseField name="original" type="string" required>
  The original text to summarize.
</ResponseField>

<ResponseField name="options" type="SummarizeOptions">
  <Expandable>
    <ResponseField name="prompt" type="string" default="New information, concepts and ideas that are deemed important">
      What should the text be summarized to?
    </ResponseField>

    <ResponseField name="format" type="string" default="A normal text with multiple sentences and paragraphs...">
      How to format the summary text.
    </ResponseField>

    <ResponseField name="length" type="number" default="250">
      The length of the summary in tokens (10-100,000).
    </ResponseField>

    <ResponseField name="intermediateFactor" type="number" default="4">
      How many times longer (than final length) are the intermediate summaries generated (1-10).
    </ResponseField>

    <ResponseField name="maxIterations" type="number" default="100">
      The maximum number of iterations to perform.
    </ResponseField>

    <ResponseField name="sliding" type="SlidingOptions" default="{ window: 50000, overlap: 250 }">
      Sliding window options.

      <Expandable>
        <ResponseField name="window" type="number">
          The window size for sliding window processing (10-100,000).
        </ResponseField>

        <ResponseField name="overlap" type="number">
          The overlap size between windows (0-100,000).
        </ResponseField>
      </Expandable>
    </ResponseField>
  </Expandable>
</ResponseField>

**Returns**

<ResponseField name="Response" type="Response<string>">
  The summarized text according to the specified options.
</ResponseField>

***

### `text()`

Generates a text of the desired length according to the prompt.

```ts theme={null}
const result = await zai.text(prompt, options)
```

**Parameters**

<ResponseField name="prompt" type="string" required>
  The prompt describing what text to generate.
</ResponseField>

<ResponseField name="options" type="TextOptions">
  <Expandable>
    <ResponseField name="length" type="number">
      The maximum number of tokens to generate (1-100,000).
    </ResponseField>
  </Expandable>
</ResponseField>

**Returns**

<ResponseField name="Response" type="Response<string>">
  The generated text according to the prompt.
</ResponseField>

***

### `sort()`

Sorts array items based on natural language sorting criteria.

```ts theme={null}
const result = await zai.sort(input, instructions, options)
```

**Parameters**

<ResponseField name="input" type="array" required>
  The array of items to sort.
</ResponseField>

<ResponseField name="instructions" type="string" required>
  Natural language description of how to sort (e.g., "by priority", "newest first", "from least expensive to most expensive").
</ResponseField>

<ResponseField name="options" type="SortOptions">
  <Expandable>
    <ResponseField name="tokensPerItem" type="number" default="250">
      The maximum number of tokens per item (1-100,000).
    </ResponseField>
  </Expandable>
</ResponseField>

**Returns**

<ResponseField name="Response" type="Response<array>">
  The sorted array according to the specified instructions.
</ResponseField>

***

### `rate()`

Rates array items on a 1-5 scale based on single or multiple criteria.

```ts theme={null}
const result = await zai.rate(input, instructions, options)
```

**Parameters**

<ResponseField name="input" type="array" required>
  The array of items to rate.
</ResponseField>

<ResponseField name="instructions" type="string | Record<string, string>" required>
  Single criterion (string) or multiple criteria (object mapping criterion names to descriptions).

  Ratings scale: 1 = Very Bad, 2 = Bad, 3 = Average, 4 = Good, 5 = Very Good
</ResponseField>

<ResponseField name="options" type="RateOptions">
  <Expandable>
    <ResponseField name="tokensPerItem" type="number" default="250">
      The maximum number of tokens per item (1-100,000).
    </ResponseField>

    <ResponseField name="maxItemsPerChunk" type="number" default="50">
      The maximum number of items to rate per chunk (1-100).
    </ResponseField>
  </Expandable>
</ResponseField>

**Returns**

<ResponseField name="Response" type="Response<RatingResult[], SimplifiedRatingResult[]>">
  For single criterion (string instructions), returns array of numbers (total scores).

  For multiple criteria (object instructions), returns array of objects with scores per criterion and total.

  <Expandable>
    <ResponseField name="[criterionName]" type="number">
      Score for this criterion (1-5).
    </ResponseField>

    <ResponseField name="total" type="number">
      Sum of all criterion scores.
    </ResponseField>
  </Expandable>
</ResponseField>

***

### `group()`

Groups array items into categories based on semantic similarity or criteria.

```ts theme={null}
const result = await zai.group(input, options)
```

**Parameters**

<ResponseField name="input" s type="Array<T>" required>
  The array of items to group.
</ResponseField>

<ResponseField name="options" type="GroupOptions">
  <Expandable>
    <ResponseField name="instructions" type="string">
      Instructions for how to group the items (e.g., "Group by type of customer issue", "Categorize by technology").
    </ResponseField>

    <ResponseField name="tokensPerElement" type="number" default="250">
      The maximum number of tokens per element (1-100,000).
    </ResponseField>

    <ResponseField name="chunkLength" type="number" default="16000">
      The maximum number of tokens per chunk (100-100,000).
    </ResponseField>

    <ResponseField name="initialGroups" type="Array<InitialGroup>" default="[]">
      Predefined categories to use for grouping.

      <Expandable>
        <ResponseField name="id" type="string" required>
          Unique identifier for the group.
        </ResponseField>

        <ResponseField name="label" type="string" required>
          Display name for the group.
        </ResponseField>

        <ResponseField name="elements" type="array">
          Optional initial elements in this group.
        </ResponseField>
      </Expandable>
    </ResponseField>
  </Expandable>
</ResponseField>

**Returns**

<ResponseField name="Response" type="Response<Group<T>[], Record<string, T[]>>">
  Simplified form: Object mapping group labels to arrays of items.

  Full form: Array of group objects with id, label, and elements.

  <Expandable>
    <ResponseField name="id" type="string">
      Unique identifier for the group.
    </ResponseField>

    <ResponseField name="label" type="string">
      Display name for the group.
    </ResponseField>

    <ResponseField name="elements" type="array">
      Items that belong to this group.
    </ResponseField>
  </Expandable>
</ResponseField>

***

### `answer()`

Answers questions from documents with citations and intelligent handling of edge cases.

```ts theme={null}
const result = await zai.answer(documents, question, options)
```

**Parameters**

<ResponseField name="documents" type="array" required>
  Array of documents to search (strings, objects, or any type).
</ResponseField>

<ResponseField name="question" type="string" required>
  The question to answer.
</ResponseField>

<ResponseField name="options" type="AnswerOptions">
  <Expandable>
    <ResponseField name="examples" type="Array<AnswerExample>" default="[]">
      Examples to help guide answer generation.
    </ResponseField>

    <ResponseField name="instructions" type="string">
      Additional instructions for answer generation.
    </ResponseField>

    <ResponseField name="chunkLength" type="number" default="16000">
      Maximum number of tokens per document chunk (250-100,000).
    </ResponseField>

    <ResponseField name="maxRefinementPasses" type="number" default="3">
      Maximum number of refinement iterations when merging chunked results (1-10).
    </ResponseField>
  </Expandable>
</ResponseField>

**Returns**

<ResponseField name="Response" type="Response<AnswerResult>">
  The result can be one of five types:

  <Expandable title="answer response attributes">
    <ResponseField name="type" type="'answer'">
      Indicates this is a successful answer with citations.
    </ResponseField>

    <ResponseField name="answer" type="string">
      The answer text.
    </ResponseField>

    <ResponseField name="citations" type="array">
      Citations mapping answer text to source documents.

      <Expandable>
        <ResponseField name="offset" type="number">
          Character offset where this citation appears in the answer.
        </ResponseField>

        <ResponseField name="item" type="any">
          The source document item.
        </ResponseField>

        <ResponseField name="snippet" type="string">
          The relevant text snippet from the document.
        </ResponseField>
      </Expandable>
    </ResponseField>
  </Expandable>

  <Expandable title="ambiguous response attributes">
    <ResponseField name="type" type="'ambiguous'">
      Indicates the question has multiple valid interpretations.
    </ResponseField>

    <ResponseField name="ambiguity" type="string">
      Explanation of what is ambiguous.
    </ResponseField>

    <ResponseField name="follow_up" type="string">
      A follow-up question to clarify.
    </ResponseField>

    <ResponseField name="answers" type="array">
      Possible answers for different interpretations (2-3 answers).
    </ResponseField>
  </Expandable>

  <Expandable title="out of topic response attributes">
    <ResponseField name="type" type="'out_of_topic'">
      Indicates the question is unrelated to the documents.
    </ResponseField>

    <ResponseField name="reason" type="string">
      Why the question is considered out of topic.
    </ResponseField>
  </Expandable>

  <Expandable title="invalid question response attributes">
    <ResponseField name="type" type="'invalid_question'">
      Indicates the question is invalid or malformed.
    </ResponseField>

    <ResponseField name="reason" type="string">
      What makes this an invalid question.
    </ResponseField>
  </Expandable>

  <Expandable title="missing knowledge response attributes">
    <ResponseField name="type" type="'missing_knowledge'">
      Indicates insufficient information to answer.
    </ResponseField>

    <ResponseField name="reason" type="string">
      What knowledge is missing.
    </ResponseField>
  </Expandable>
</ResponseField>

***

### `patch()`

Patches files based on natural language instructions using the micropatch protocol.

```ts theme={null}
const result = await zai.patch(files, instructions, options)
```

**Parameters**

<ResponseField name="files" type="Array<File>" required>
  Array of files to patch.

  <Expandable>
    <ResponseField name="path" type="string" required>
      The file path (e.g., 'src/components/Button.tsx').
    </ResponseField>

    <ResponseField name="name" type="string" required>
      The file name (e.g., 'Button.tsx').
    </ResponseField>

    <ResponseField name="content" type="string" required>
      The file content.
    </ResponseField>
  </Expandable>
</ResponseField>

<ResponseField name="instructions" type="string" required>
  Natural language instructions describing what changes to make.
</ResponseField>

<ResponseField name="options" type="PatchOptions">
  <Expandable>
    <ResponseField name="maxTokensPerChunk" type="number">
      Maximum tokens per chunk when processing large files. If not specified, all files must fit in a single prompt.
    </ResponseField>
  </Expandable>
</ResponseField>

**Returns**

<ResponseField name="Response" type="Response<Array<File>>">
  Array of patched files with the same structure as input, plus:

  <Expandable>
    <ResponseField name="patch" type="string">
      The micropatch operations that were applied.
    </ResponseField>
  </Expandable>
</ResponseField>

***

### `with()`

Creates a new Zai instance with modified configuration options.

```ts theme={null}
const newZai = zai.with(options)
```

**Parameters**

<ResponseField name="options" type="Partial<ZaiConfig>" required>
  Configuration options to override. Can include any of the `ZaiConfig` properties: `client`, `userId`, `modelId`, `activeLearning`, or `namespace`.
</ResponseField>

**Returns**

<ResponseField name="Zai" type="Zai">
  A new Zai instance with the updated configuration.
</ResponseField>

***

### `learn()`

Creates a new Zai instance with active learning enabled for the specified task ID.

```ts theme={null}
const learningZai = zai.learn(taskId)
```

**Parameters**

<ResponseField name="taskId" type="string" required>
  The ID of the task for active learning. This will be used to organize and retrieve examples for improving future responses.
</ResponseField>

**Returns**

<ResponseField name="Zai" type="Zai">
  A new Zai instance with active learning enabled for the specified task.
</ResponseField>

## Response methods

All Zai operations return a `Response` object that implements promise-like behavior while providing additional functionality for event handling and request control.

You can call any of the following methods on the `Response` object:

### `result()`

Returns the complete result including output, usage statistics, and elapsed time.

```ts theme={null}
const { output, usage, elapsed } = await response.result()
```

**Returns**

<ResponseField name="Promise" type="Promise<ResultData>">
  <Expandable>
    <ResponseField name="output" type="T">
      The actual result of the operation.
    </ResponseField>

    <ResponseField name="usage" type="Usage">
      Usage statistics for the operation.

      <Expandable>
        <ResponseField name="requests" type="RequestStats">
          Request-related statistics.

          <Expandable>
            <ResponseField name="requests" type="number">
              Total number of requests made.
            </ResponseField>

            <ResponseField name="errors" type="number">
              Number of requests that resulted in errors.
            </ResponseField>

            <ResponseField name="responses" type="number">
              Number of successful responses received.
            </ResponseField>

            <ResponseField name="cached" type="number">
              Number of cached responses used.
            </ResponseField>

            <ResponseField name="percentage" type="number">
              Completion percentage of requests.
            </ResponseField>
          </Expandable>
        </ResponseField>

        <ResponseField name="cost" type="CostStats">
          Cost-related statistics.

          <Expandable>
            <ResponseField name="input" type="number">
              Cost for input tokens.
            </ResponseField>

            <ResponseField name="output" type="number">
              Cost for output tokens.
            </ResponseField>

            <ResponseField name="total" type="number">
              Total cost for the operation.
            </ResponseField>
          </Expandable>
        </ResponseField>

        <ResponseField name="tokens" type="TokenStats">
          Token usage statistics.

          <Expandable>
            <ResponseField name="input" type="number">
              Number of input tokens used.
            </ResponseField>

            <ResponseField name="output" type="number">
              Number of output tokens generated.
            </ResponseField>

            <ResponseField name="total" type="number">
              Total number of tokens processed.
            </ResponseField>
          </Expandable>
        </ResponseField>
      </Expandable>
    </ResponseField>

    <ResponseField name="elapsed" type="number">
      Time elapsed in milliseconds for the operation.
    </ResponseField>
  </Expandable>
</ResponseField>

***

### `on()`

Registers an event listener for the specified event type.

```ts theme={null}
response.on('progress', (usage) => {
  console.log('Request progress:', usage)
})
```

**Parameters**

<ResponseField name="type" type="'progress' | 'complete' | 'error'" required>
  The event type to listen for:

  * `progress`: Emitted during request processing with usage statistics
  * `complete`: Emitted when the operation completes successfully
  * `error`: Emitted when an error occurs
</ResponseField>

<ResponseField name="listener" type="(event: EventData) => void" required>
  The callback function to execute when the event is emitted.
</ResponseField>

**Returns**

<ResponseField name="Response" type="Response">
  The same Response instance for method chaining.
</ResponseField>

***

### `off()`

Removes an event listener for the specified event type.

```ts theme={null}
const listener = (usage) => console.log(usage)
response.on('progress', listener)
response.off('progress', listener)
```

**Parameters**

<ResponseField name="type" type="'progress' | 'complete' | 'error'" required>
  The event type to remove the listener from.
</ResponseField>

<ResponseField name="listener" type="(event: EventData) => void" required>
  The specific listener function to remove.
</ResponseField>

**Returns**

<ResponseField name="Response" type="Response">
  The same Response instance for method chaining.
</ResponseField>

***

### `once()`

Registers an event listener that will be called only once.

```ts theme={null}
response.once('complete', (result) => {
  console.log('Operation completed:', result)
})
```

**Parameters**

<ResponseField name="type" type="'progress' | 'complete' | 'error'" required>
  The event type to listen for.
</ResponseField>

<ResponseField name="listener" type="(event: EventData) => void" required>
  The callback function to execute when the event is emitted.
</ResponseField>

**Returns**

<ResponseField name="Response" type="Response">
  The same Response instance for method chaining.
</ResponseField>

***

### `bindSignal()`

Binds an AbortSignal to the response for cancellation control.

```ts theme={null}
const controller = new AbortController()
response.bindSignal(controller.signal)

// Cancel the operation
controller.abort('User cancelled')
```

**Parameters**

<ResponseField name="signal" type="AbortSignal" required>
  The AbortSignal to bind to this response.
</ResponseField>

**Returns**

<ResponseField name="Response" type="Response">
  The same Response instance for method chaining.
</ResponseField>

***

### `abort()`

Aborts the ongoing operation.

```ts theme={null}
response.abort('Operation cancelled by user')
```

**Parameters**

<ResponseField name="reason" type="string | Error">
  Optional reason for the abortion.
</ResponseField>

**Returns**

<ResponseField name="void" type="void">
  No return value.
</ResponseField>

***

### `then()`

Attaches callbacks for the resolution and/or rejection of the response.

```ts theme={null}
response
  .then(result => console.log('Success:', result))
  .catch(error => console.error('Error:', error))
```

**Parameters**

<ResponseField name="onfulfilled" type="(value: T) => TResult1 | PromiseLike<TResult1>">
  Callback to execute when the response resolves successfully.
</ResponseField>

<ResponseField name="onrejected" type="(reason: any) => TResult2 | PromiseLike<TResult2>">
  Callback to execute when the response rejects.
</ResponseField>

**Returns**

<ResponseField name="PromiseLike" type="PromiseLike<TResult1 | TResult2>">
  A promise-like object for further chaining.
</ResponseField>

***

### `catch()`

Attaches a callback for handling rejection of the response.

```ts theme={null}
response.catch(error => {
  console.error('Operation failed:', error)
})
```

**Parameters**

<ResponseField name="onrejected" type="(reason: any) => TResult | PromiseLike<TResult>">
  Callback to execute when the response rejects.
</ResponseField>

**Returns**

<ResponseField name="PromiseLike" type="PromiseLike<T | TResult>">
  A promise-like object for further chaining.
</ResponseField>
