---
type: api
title: Perform document extraction inference on the service
description: Extract the content of documents, such as PDFs or images of documents, into a textual format.
url: https://www.elastic.co/elastic/docs-builder/docs/4641/api/doc/elasticsearch/v9/operation/operation-inference-document-extraction
resource: https://www.elastic.co/elastic/docs-builder/docs/4641/api/doc/elasticsearch/v9/operation/operation-inference-document-extraction
products:
  - Elasticsearch
---

# Perform document extraction inference on the service

Authentication: required

Availability: Planned

## Paths

- `POST` `/_inference/document_extraction/{inference_id}`

#### Path parameters

- `inference_id` (string) — required
  The unique identifier for the inference endpoint.

## Description

Extract the content of documents, such as PDFs or images of documents, into a textual format.

## Required authorization

- Cluster privileges: `monitor_inference`

## Query string parameters

- `timeout` (string · default: 120s) — optional
  The amount of time to wait for the inference request to complete.

## Request body

`application/json`

- `input` (union oneOf) — required
  The documents to extract the content from.
Either a `content` object or an array of `content` objects.

`content` object example:
```
"input": {
  "content": {
    "type": "pdf",
    "format": "base64",
    "value": "data:application/pdf;base64,..."
  }
}
```
`content` object array example:
```
"input": [
  {
    "content": {
      "type": "pdf",
      "format": "base64",
      "value": "data:application/pdf;base64,..."
    }
  },
  {
    "content": {
      "type": "image",
      "format": "url",
      "value": "https://example.com/scanned-page.png"
    }
  }
]
```
  `object` or `object[]`
  - `content` (object) — required
    The document to extract the content from.
    - `type` (enum) — required
      The type of the document. Not all services and models support all types.
      Values: `image`, `pdf`
    - `format` (enum) — optional
      The format of the document. If not specified, this defaults to `base64`.
Not all services and models support all formats.
      Values: `base64`, `url`
    - `value` (string) — required
      The value of the document. For the `base64` format, this must be a base64-encoded data URI, that is, "data:content/type;base64,...".
For the `url` format, this must be a URL that points to the document, that is, "https://example.com/document.pdf".
- `task_settings` (object) — optional
  Task settings for the individual inference request.
These settings are specific to the task type you specified and override the task settings specified when initializing the service.

## Response

`application/json`

- `document_extraction` (object[])
  - `content` (string)
    The content extracted from the document.
  - `format` (string)
    The format of the extracted content, for example `markdown`.
  - `metadata` (object)
    Additional information about the document, such as its title. The available fields depend on the service and model.
## Code examples

### Document extraction task

#### Console

```console
POST _inference/document_extraction/my-document-extraction-endpoint
{
  "input": [
    {
      "content": {
        "type": "pdf",
        "format": "base64",
        "value": "data:application/pdf;base64,..."
      }
    }
  ]
}
```

#### JavaScript

```javascript
const response = await client.inference.documentExtraction({
  inference_id: "my-document-extraction-endpoint",
  input: [
    {
      content: {
        type: "pdf",
        format: "base64",
        value: "data:application/pdf;base64,...",
      },
    },
  ],
});
```

#### Python

```python
resp = client.inference.document_extraction(
    inference_id="my-document-extraction-endpoint",
    input=[
        {
            "content": {
                "type": "pdf",
                "format": "base64",
                "value": "data:application/pdf;base64,..."
            }
        }
    ],
)
```

#### curl

```curl
curl -X POST "$ELASTICSEARCH_URL/_inference/document_extraction/my-document-extraction-endpoint" \
  -H "Authorization: ApiKey $ELASTIC_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "input": [
      {
        "content": {
          "type": "pdf",
          "format": "base64",
          "value": "data:application/pdf;base64,..."
        }
      }
    ]
  }'
```

#### PHP

```php
$resp = $client->inference()->documentExtraction([
    "inference_id" => "my-document-extraction-endpoint",
    "body" => [
        "input" => array(
            [
                "content" => [
                    "type" => "pdf",
                    "format" => "base64",
                    "value" => "data:application/pdf;base64,...",
                ],
            ],
        ),
    ],
]);
```

#### Ruby

```ruby
response = client.inference.document_extraction(
  inference_id: "my-document-extraction-endpoint",
  body: {
    "input": [
      {
        "content": {
          "type": "pdf",
          "format": "base64",
          "value": "data:application/pdf;base64,..."
        }
      }
    ]
  }
)
```

### Document extraction task using the url format and task settings

#### Console

```console
POST _inference/document_extraction/my-document-extraction-endpoint
{
  "input": {
    "content": {
      "type": "image",
      "format": "url",
      "value": "https://example.com/scanned-page.png"
    }
  },
  "task_settings": {
    "output_format": "markdown"
  }
}
```

#### JavaScript

```javascript
const response = await client.inference.documentExtraction({
  inference_id: "my-document-extraction-endpoint",
  input: {
    content: {
      type: "image",
      format: "url",
      value: "https://example.com/scanned-page.png",
    },
  },
  task_settings: {
    output_format: "markdown",
  },
});
```

#### Python

```python
resp = client.inference.document_extraction(
    inference_id="my-document-extraction-endpoint",
    input={
        "content": {
            "type": "image",
            "format": "url",
            "value": "https://example.com/scanned-page.png"
        }
    },
    task_settings={
        "output_format": "markdown"
    },
)
```

#### curl

```curl
curl -X POST "$ELASTICSEARCH_URL/_inference/document_extraction/my-document-extraction-endpoint" \
  -H "Authorization: ApiKey $ELASTIC_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "input": {
      "content": {
        "type": "image",
        "format": "url",
        "value": "https://example.com/scanned-page.png"
      }
    },
    "task_settings": {
      "output_format": "markdown"
    }
  }'
```

#### PHP

```php
$resp = $client->inference()->documentExtraction([
    "inference_id" => "my-document-extraction-endpoint",
    "body" => [
        "input" => [
            "content" => [
                "type" => "image",
                "format" => "url",
                "value" => "https://example.com/scanned-page.png",
            ],
        ],
        "task_settings" => [
            "output_format" => "markdown",
        ],
    ],
]);
```

#### Ruby

```ruby
response = client.inference.document_extraction(
  inference_id: "my-document-extraction-endpoint",
  body: {
    "input": {
      "content": {
        "type": "image",
        "format": "url",
        "value": "https://example.com/scanned-page.png"
      }
    },
    "task_settings": {
      "output_format": "markdown"
    }
  }
)
```

## Response examples

#### Document extraction task

`application/json`

An abbreviated response from `POST _inference/document_extraction/my-document-extraction-endpoint`.

```json
{
  "document_extraction": [
    {
      "content": "# Annual Report 2025\n\n...",
      "format": "markdown",
      "metadata": {
        "title": "Annual Report 2025"
      }
    }
  ]
}
```

