---
title: experimental_extract
description: Extract typed field values from shared state with an extraction model.
---

# `experimental_extract()`

```ts
import { experimental_extract } from 'ai';
```

Extracts a nonempty map of typed fields from one state. See
[Extractions](/docs/ai-sdk-core/extractions) for examples and semantics.

## Parameters

| Parameter               | Type                                                                                           | Description                                                                                                                                    |
| ----------------------- | ---------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
| `model`                 | `Experimental_ExtractionModel`                                                                 | Required experimental v4 extraction model instance, or a string ID resolved by the default provider (the AI Gateway unless one is configured). |
| `state`                 | `Experimental_ExtractionState`                                                                 | Required string, JSON object, or ordered array of text, file, and JSON parts.                                                                  |
| `fields`                | `Record<string, Experimental_ExtractionField>`                                                 | Required nonempty field map.                                                                                                                   |
| `referenceDate`         | `string`                                                                                       | Optional `YYYY-MM-DD` date that anchors relative dates.                                                                                        |
| `maxOutputTokens`       | `number`                                                                                       | Optional maximum output tokens, an integer >= 1.                                                                                               |
| `reasoning`             | `'provider-default' \| 'none' \| 'minimal' \| 'low' \| 'medium' \| 'high' \| 'xhigh' \| 'max'` | Optional reasoning effort for supported models.                                                                                                |
| `maxRetries`            | `number`                                                                                       | Nonnegative integer; defaults to 2.                                                                                                            |
| `abortSignal`           | `AbortSignal`                                                                                  | Cancels the extraction request.                                                                                                                |
| `experimental_download` | `Experimental_DownloadFunction`                                                                | Required for remote file URLs in state. Use `createDownload({ fetch })` with a DNS-protected transport for untrusted URLs.                     |
| `headers`               | `Record<string, string>`                                                                       | Additional HTTP headers.                                                                                                                       |
| `providerOptions`       | `ProviderOptions`                                                                              | Provider-specific options.                                                                                                                     |
| `telemetry`             | `TelemetryOptions`                                                                             | Telemetry configuration, including per-call integrations, input/output recording, and a function ID.                                           |
| `runtimeContext`        | `Record<string, unknown>`                                                                      | Context available to lifecycle callbacks and selectively included in telemetry with `telemetry.includeRuntimeContext`.                         |
| `onStart`               | `(event: Experimental_ExtractStartEvent) => void`                                              | Called when the extraction operation begins.                                                                                                   |
| `onEnd`                 | `(event: Experimental_ExtractEndEvent) => void`                                                | Called when the extraction operation completes successfully.                                                                                   |

Remote file URLs are rejected with `DownloadError` unless `experimental_download`
is supplied. The model's `fetch` option does not apply to these downloads. See
[Secure URL Fetching](/docs/advanced/secure-url-fetching#core-file-downloads).

See [Lifecycle Callbacks](/docs/ai-sdk-core/lifecycle-callbacks#experimental_extract)
for the complete `onStart` and `onEnd` event fields.

## Fields

| Type       | Options                                                                                           |
| ---------- | ------------------------------------------------------------------------------------------------- |
| `text`     | None                                                                                              |
| `number`   | `unit`, `integer`, `decimals` (0 to 6)                                                            |
| `date`     | None                                                                                              |
| `time`     | None                                                                                              |
| `datetime` | None                                                                                              |
| `duration` | `unit` (`seconds`, `minutes`, `hours`, `days`, `weeks`, `months`, `years`), `integer`, `decimals` |
| `list`     | Required `items` (`text`, `number`, or `date`), `integer`, `decimals`                             |

Every field accepts optional `instructions` as a string, JSON object, or JSON
array. See [field types](/docs/ai-sdk-core/extractions#field-types).

## Result

Returns `Promise<Experimental_ExtractionResult<FIELDS>>`:

- `answers`: One typed answer per field ID, with `value` typed from the field.
- `usage`: `inputTokens`, `outputTokens`, and `totalTokens`, each possibly undefined.
- `warnings`: Provider warnings, also passed to the SDK warning logger.
- `providerMetadata`: Optional provider-specific metadata.
- `response`: Timestamp, model ID, and optional response ID, headers, and body.

## Errors

Unsupported field types throw
[`Experimental_ExtractionUnsupportedFieldTypeError`](/docs/reference/ai-sdk-errors/ai-extraction-unsupported-field-type-error),
before provider I/O or, for AI Gateway models, after the gateway rejects the
field. Invalid inputs throw `InvalidArgumentError`; malformed answers throw
`InvalidResponseDataError`. Fields the model refuses throw
[`Experimental_ExtractionRefusalError`](/docs/reference/ai-sdk-errors/ai-extraction-refusal-error). Model instances must implement v4; other
versions throw `UnsupportedModelVersionError`.
