---
title: FileType
description: "FileType and FileRef in PySpark: use the Databricks FILE type as a parameter or return type in Python UDFs and read file metadata and bytes."
canonical_url: https://docs.databricks.com/aws/en/pyspark/reference/file-type
---

# FileType

**Applies to:** ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks Runtime 18 LTS and above

> [!NOTE]
> **Beta**
>
> This feature is in [Beta](https://docs.databricks.com/aws/en/release-notes/release-types.md).

`FileType` is the PySpark type for the SQL `FILE` type, a reference to an unstructured file and its metadata. Use it to declare `FILE` parameters and return types in Python user-defined functions (UDFs). In Python, a `FILE` value is a `FileRef` object, which holds the file's metadata and reads its bytes.

For the SQL type reference and conceptual overview, see [`FILE` type](https://docs.databricks.com/aws/en/sql/language-manual/data-types/file-type.md) and [FILE type and unstructured data](https://docs.databricks.com/aws/en/unstructured/file.md). For file-processing UDF examples, see [Process files with UDFs](https://docs.databricks.com/aws/en/unstructured/file-udfs.md).

> [!NOTE]
>
> `FILE` type requires [environment version 6](https://docs.databricks.com/aws/en/release-notes/serverless/environment-version/six.md) or above on serverless notebooks. To choose the environment version for a notebook, see [ Select a base environment](https://docs.databricks.com/aws/en/compute/serverless/dependencies.md#environment-version).

## Import

```python
from pyspark.sql.types import FileType, FileRef
```

## FileType

`FileType` is a subclass of `pyspark.sql.types.DataType`. Use it as the parameter or return type of a UDF, or as a field type in a schema.

`FileType` doesn't specify `MANAGED` or `EXTERNAL`. Those qualifiers apply only to a `FILE` table column, where the column determines whether a reference is stored as managed or external. A UDF passes and returns `FILE` references, and the target column decides how each reference is stored when you write the result to a table.

You can use `FileType` in the following ways, which also apply to SQL and Scala UDFs and to SQL stored procedures:

* As a top-level type.
* Nested inside a `StructType` or an `ArrayType`.
* As the value type of a `MapType`, but not as a `MapType` key.
* Inside a `VariantType`, but only for external files.

When a query returns a `FILE` column to Python, either inside a UDF or through `DataFrame.collect()`, each value is a [`FileRef`](https://docs.databricks.com/aws/en/pyspark/reference/file-type.md#fileref).

## FileRef

A `FileRef` is the Python value for a `FILE`. It holds the file's metadata and has methods to read the file's bytes and to create new references.

### Attributes

| Attribute      | Type  | Description                                                                                                                                                                                     |
| -------------- | ----- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `uri`          | `str` | The URI of the file. Always set.                                                                                                                                                                |
| `offset`       | `int` | An offset into the file, in bytes.                                                                                                                                                              |
| `size`         | `int` | The size of the file in bytes.                                                                                                                                                                  |
| `content_type` | `str` | The MIME type of the file, when known.                                                                                                                                                          |
| `checksum`     | `str` | An integrity token for the file's bytes, of the form `<algorithm>:<digest>`. For the recognized algorithms, see [Checksums](https://docs.databricks.com/aws/en/unstructured/file.md#checksums). |

### Methods

| Method                                                      | Description                                                                                                                                                                                                                                                                                                                                                          |
| ----------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `as_local_file()`                                           | Returns a `pathlib.Path` to the file. Pass the result to any library that accepts a path. Available on Databricks compute (notebooks and UDF workers). Not available on a [Databricks Connect](https://docs.databricks.com/aws/en/dev-tools/databricks-connect/index.md) client, such as an IDE or local application that runs your code outside Databricks compute. |
| `open()`                                                    | Opens the file for binary reading and returns a file object. The caller closes it. Has the same compute requirement as `as_local_file()`.                                                                                                                                                                                                                            |
| `from_bytes(content, path=None, content_type=None)`         | Class method. Uploads `content` (a `bytes` value) to the Unity Catalog volume path given by `path` and returns a `FileRef`. Fails if a file already exists at the target path. Supported only inside a UDF.                                                                                                                                                          |
| `from_local_file(local_file, path=None, content_type=None)` | Class method. Uploads a local file to a Unity Catalog volume and returns a `FileRef`. The `path` and `content_type` parameters behave as in `from_bytes`. Supported only inside a UDF.                                                                                                                                                                               |

## Example

In the following code, a scalar UDF receives a `FILE` value as a `FileRef`, opens the image, and returns its dimensions:

```python
from pyspark.sql.functions import col, udf
from pyspark.sql.types import FileRef, StringType
from PIL import Image

@udf(returnType=StringType())
def image_resolution(file: FileRef) -> str:
    with Image.open(file.as_local_file()) as img:
        return f"{img.width}x{img.height}"

spark.read.table("images").select(image_resolution(col("photo"))).display()
```

For more file-processing UDF examples, including generating files with a UDTF, see [Process files with UDFs](https://docs.databricks.com/aws/en/unstructured/file-udfs.md). For general UDF authoring, see [Python scalar user-defined functions (UDFs)](https://docs.databricks.com/aws/en/udf/python.md) and [Python user-defined table functions (UDTFs)](https://docs.databricks.com/aws/en/udf/python-udtf.md).

## Limitations

Using `FileType` in PySpark has the following limitations:

* PySpark doesn't support declaring `FILE MANAGED` or `FILE EXTERNAL` table columns or ingesting files in bulk. Use SQL for those operations. PySpark only supports `FILE`, as `FileType`, in UDFs and file reads.
* `as_local_file()` and `open()` require cluster-side access, so they aren't available on a [Databricks Connect](https://docs.databricks.com/aws/en/dev-tools/databricks-connect/index.md) client. Call them in a UDF or on cluster compute instead.
* For `from_bytes` and `from_local_file`, Python UDFs infer `content_type` from the path extension, whereas Scala UDFs infer it from the file's magic bytes.
* Returning a `FileRef` from a UDF that writes to a `FILE MANAGED` column isn't supported.
* A UDF registered in Unity Catalog (`CREATE FUNCTION`) can read a `FILE`'s metadata, but not its contents, and it can't create files. Use a session-scoped UDF to read a file's contents (`open`, `as_local_file`) or create one (`from_bytes`, `from_local_file`).

## Related resources

* [`FILE` type](https://docs.databricks.com/aws/en/sql/language-manual/data-types/file-type.md)
* [FILE type and unstructured data](https://docs.databricks.com/aws/en/unstructured/file.md)
* [Process files with UDFs](https://docs.databricks.com/aws/en/unstructured/file-udfs.md)
* [Python scalar user-defined functions (UDFs)](https://docs.databricks.com/aws/en/udf/python.md)
* [Python user-defined table functions (UDTFs)](https://docs.databricks.com/aws/en/udf/python-udtf.md)
* [Ingest files as the FILE type](https://docs.databricks.com/aws/en/ingestion/file.md)
