---
title: "extractWithModels"
version: "0.9.0"
url: "https://docs.laminapdf.com/reference/sdk/functions/extractwithmodels/"
---

# extractWithModels

```ts
function extractWithModels<F>(
   doc, 
   options, 
   models, 
   __namedParameters?
): Promise<ExtractResult[F]>;
```

Defined in: [packages/sdk/src/extract-job.ts:221](https://github.com/laudebugs/laminapdf/blob/v0.9.0/packages/sdk/src/extract-job.ts#L221)

Runs an extraction whose models the host runs asynchronously: steps a job, and for each
request awaits the matching function of `models`, then hands its result back.

## Type Parameters

### F

`F` *extends* `ExtractFormat`

## Parameters

### doc

[`Document`](/reference/sdk/classes/document.md)

### options

[`ExtractJobOptions`](/reference/sdk/type-aliases/extractjoboptions.md)\<`F`\>

### models

[`HostModels`](/reference/sdk/interfaces/hostmodels.md)

### \_\_namedParameters?

#### budget?

`number` = `16`

#### signal?

`AbortSignal`

## Returns

`Promise`\<[`ExtractResult`](/reference/sdk/interfaces/extractresult.md)\[`F`\]\>

## Example

```ts
const json = await extractWithModels(
  doc,
  { format: "json", models: { layout: { name: "my-layout", version: "1", sha256, licence: "Apache-2.0" } } },
  { layout: async ({ page, dpi }) => myModel.detect(await render(page, dpi)) },
);
```
On a document opened with `Document.openRange`, a step that needs bytes not yet fetched ends
the job with `data-pending`, as every error ends a job; fetch the file first (`await
doc.ensure(() => doc.pages)` reads only the page tree, so read the whole file for a long
extraction), or wrap the call in `doc.ensure`, which runs it again, models included.

## Throws

[LaminaPdfError](/reference/sdk/classes/laminapdferror.md) as [ExtractJob.step](/reference/sdk/classes/extractjob.md#step); `invalid-argument` when
`options.models` names a model `models` has no function for; `cancelled` once `signal`
aborts, also when the model rejects because of it (the model's error is the `cause`).

## Since

0.2.0
