---
title: ai_extract
description: "ai\\_extract function in PySpark: Extracts structured data from a document column using AI/LLM."
canonical_url: https://docs.databricks.com/gcp/en/pyspark/reference/functions/ai_extract
---

# ai\_extract

Extracts structured data from a document column using AI/LLM.

For the corresponding Databricks SQL function, see [`ai_extract` function](https://docs.databricks.com/gcp/en/sql/language-manual/functions/ai_extract.md).

## Syntax

```python
from pyspark.sql import functions as dbf

dbf.ai_extract(col=<col>, schema=<schema>, options=<options>)
```

## Parameters

| Parameter | Type                          | Description                                                                                                                                  |
| --------- | ----------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------- |
| `col`     | `pyspark.sql.Column` or `str` | A column containing the document content to extract from.                                                                                    |
| `schema`  | `dict` or `list`              | A Python dict (field name to `{"type": ..., "description": ...}`) or list of field-name strings. Serialized to a JSON literal automatically. |
| `options` | `dict`, optional              | A dictionary of options to control extraction behavior.                                                                                      |

## Returns

`pyspark.sql.Column`: A new column of VariantType containing the extracted fields.

## Examples

```python
df.select(ai_extract("text", {"name": {"type": "string", "description": "Name"}}))
df.select(ai_extract("text", ["name", "age"]))
```

## <a id="related-resources" />Related resources

* [ai\_extract SQL language manual](https://docs.databricks.com/gcp/en/sql/language-manual/functions/ai_extract.md)
