> ## Documentation Index
> Fetch the complete documentation index at: https://docs.dreep.cloud/llms.txt
> Use this file to discover all available pages before exploring further.

# Extract Text (OCR)

> Run OCR over an image or PDF with client.extract_text(), or read a stored asset's text with client.fetch_text().

## extract\_text

Runs OCR over an image or PDF you upload.

```python theme={null}
result = client.extract_text(Path("invoice.pdf"))
print(result.text)
```

```python theme={null}
client.extract_text(
    file,
    *,
    filename=None,
    folder=None,
    folder_id=None,
    auto_create_folders=None,
    include_blocks=None,
    timeout=NOT_GIVEN,
) -> OCRResult
```

| Parameter | Type | Description |
| :- | :- | :- |
| `file` | `bytes \| Path \| BinaryIO` | **Required.** Image or PDF to read |
| `filename` | `str` | Name for the source, needed when passing `bytes` |
| `folder` | `str` | Store the extracted text as a `.txt` asset in this folder |
| `folder_id` | `str` | UUID of the destination folder |
| `auto_create_folders` | `bool` | `False` to require the path already exists |
| `include_blocks` | `bool` | Also return line-level blocks with bounding boxes |

### Returns

```python theme={null}
result.text          # "INVOICE\nAcme Ltd\n…"
result.blocks        # [] unless include_blocks=True
result.saved_asset   # SavedTextAsset when a folder was given, else None
```

With `include_blocks=True`, each block carries the recognised line and where it sits on the
page:

```python theme={null}
result = client.extract_text(Path("invoice.pdf"), include_blocks=True)

for block in result.blocks:
    print(block)  # line text with its bounding box
```

Passing a folder also stores the text as a `.txt` asset, so you can serve or re-read it
later without running OCR again:

```python theme={null}
result = client.extract_text(Path("invoice.pdf"), folder="invoices/text")
result.saved_asset.id
```

## fetch\_text

For an asset **already in Dreep**, read its text instead of re-uploading the file. The
result is cached after the first read, so repeat calls don't re-run OCR.

```python theme={null}
text = client.fetch_text(asset)
```

```python theme={null}
client.fetch_text(asset, *, expires_in=None, timeout=NOT_GIVEN) -> str
```

| Parameter | Type | Description |
| :- | :- | :- |
| `asset` | `AssetLike` | **Required.** The stored image or PDF |
| `expires_in` | `int \| timedelta` | Needed for an asset in a signed folder — the request is then signed with your signing secret |

Under the hood this is just the asset's delivery URL with `format=txt`, which is why it
costs no upload.

```python theme={null}
# An asset in a signed folder
text = client.fetch_text(asset, expires_in=300)
```

### Still processing

OCR on a long PDF can outlive the request. The API answers `202` while it finishes, which
the SDK raises as `DreepProcessingError`:

```python theme={null}
from dreep import DreepProcessingError

try:
    text = client.fetch_text(asset)
except DreepProcessingError:
    # not ready yet — try again shortly
    ...
```

## Errors

| Error | Cause |
| :- | :- |
| `DreepValidationError` | Unsupported file type — OCR takes images and PDFs |
| `DreepLimitError` | The account has no credit left for OCR |
| `DreepProcessingError` | `fetch_text()` while extraction is still running |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.