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

# to_file

> Constructs a FILE value from a STRUCT expression.

**Experimental feature**

[Advice on use of experimental features](/documents/4.2/maturity/features.md)

Constructs a temporary [FILE](/documents/4.2/sql-reference/data-types/other-data-types/FILE.md) value from a STRUCT expression, such as `named_struct(...)` or a STRUCT column. This function does not access storage or infer metadata.

## Syntax

```sql theme={null}
FILE to_file(STRUCT struct_expr)
```

## Parameters

`struct_expr`: a STRUCT expression that describes the file. The following fields are accepted:

| Field | Accepted input types | FILE field type |
| - | - | - |
| `uri`, `content_type`, `checksum` | CHAR, VARCHAR | VARCHAR |
| `offset`, `size` | TINYINT, SMALLINT, INT, BIGINT | BIGINT |
| `inline` | VARBINARY | VARBINARY |

## Return value

Returns a FILE value. If `struct_expr` is NULL, returns NULL.

## Usage notes

* Field names are case-insensitive and can appear in any order. Unknown or duplicate names cause an analysis error.
* Missing fields and explicit NULL values become NULL. No defaults are filled in, including for `offset` and `size`.
* For each non-NULL STRUCT, exactly one of `uri` and `inline` must be non-NULL. Missing fields count as NULL.
* Integer inputs are widened to BIGINT. Untyped NULL is accepted for every field. Other input types require an explicit CAST.
* For URI references, `offset` and `size` can be NULL; non-NULL values must be greater than or equal to zero. For inline content, both fields must be NULL. A row that violates these rules or the `uri`/`inline` rule causes the query to fail. To return NULL for such rows instead, use [try\_to\_file](/documents/4.2/sql-reference/sql-functions/file-functions/try_to_file.md).
* URI syntax, MIME types, checksums, and the referenced byte range are not validated.
* `inline` contains raw bytes. Empty strings and empty binary values count as non-NULL and are preserved.

When `ai_complete` or `ai_embed` consumes a FILE with a missing or empty `content_type`, the AI function attempts to infer it. See [FILE input support](/documents/4.2/sql-reference/sql-functions/ai-functions/ai_functions.md#file-input).

## Examples

### Example 1: Construct a file reference

```sql theme={null}
SELECT to_file(named_struct('uri', 'https://example.com/a.jpg', 'content_type', 'image/jpeg')) AS f;
```

```plaintext theme={null}
+-------------------------------------------------------------------------------------------------------------------------+
| f                                                                                                                       |
+-------------------------------------------------------------------------------------------------------------------------+
| {"uri":"https://example.com/a.jpg","offset":null,"size":null,"content_type":"image/jpeg","checksum":null,"inline":null} |
+-------------------------------------------------------------------------------------------------------------------------+
```

### Example 2: Construct inline content

```sql theme={null}
SELECT to_file(named_struct('inline', to_binary('610062', 'hex'))) AS f;
```

```plaintext theme={null}
+----------------------------------------------------------------------------------------------+
| f                                                                                            |
+----------------------------------------------------------------------------------------------+
| {"uri":null,"offset":null,"size":null,"content_type":null,"checksum":null,"inline":"610062"} |
+----------------------------------------------------------------------------------------------+
```

### Example 3: NULL argument

```sql theme={null}
SELECT to_file(NULL) AS f;
```

```plaintext theme={null}
+------+
| f    |
+------+
| NULL |
+------+
```

## keyword

TO\_FILE, FILE


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