Skip to content

daft.functions.guess_mime_type#

guess_mime_type #

guess_mime_type(bytes_expr: Expression) -> Expression

Guess the MIME type of binary data by inspecting magic bytes.

Detects common file formats including: PNG, JPEG, GIF, WEBP, PDF, ZIP, MP3, WAV, OGG, MP4, MPEG, HDF5, and HTML.

Note: HDF5 detection follows the registered media type and signature documented by IANA: https://www.iana.org/assignments/media-types/application/vnd.hdfgroup.hdf5.

Returns None if the format cannot be determined.

Parameters:

Name Type Description Default
bytes_expr Expression

Binary expression containing raw bytes.

required

Returns:

Name Type Description
Expression Expression

String expression containing MIME type (e.g., "image/png") or None.

Example

import daft from daft.functions import guess_mime_type df = daft.from_pydict({"data": [b"\x89PNG\r\n\x1a\n", b"not a known format"]}) df = df.with_column("mime", guess_mime_type(df["data"])) df.select("mime").show() ╭───────────╮ │ mime │ │ --- │ │ String │ ╞═══════════╡ │ image/png │ ├╌╌╌╌╌╌╌╌╌╌╌┤ │ None │ ╰───────────╯ (Showing first 2 of 2 rows)

Source code in daft/functions/file_.py
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
def guess_mime_type(bytes_expr: Expression) -> Expression:
    r"""Guess the MIME type of binary data by inspecting magic bytes.

    Detects common file formats including: PNG, JPEG, GIF, WEBP, PDF, ZIP,
    MP3, WAV, OGG, MP4, MPEG, HDF5, and HTML.

    Note: HDF5 detection follows the registered media type and signature documented by IANA:
    https://www.iana.org/assignments/media-types/application/vnd.hdfgroup.hdf5.

    Returns None if the format cannot be determined.

    Args:
        bytes_expr: Binary expression containing raw bytes.

    Returns:
        Expression: String expression containing MIME type (e.g., "image/png") or None.

    Example:
        >>> import daft
        >>> from daft.functions import guess_mime_type
        >>> df = daft.from_pydict({"data": [b"\x89PNG\r\n\x1a\n", b"not a known format"]})
        >>> df = df.with_column("mime", guess_mime_type(df["data"]))
        >>> df.select("mime").show()
        ╭───────────╮
        │ mime      │
        │ ---       │
        │ String    │
        ╞═══════════╡
        │ image/png │
        ├╌╌╌╌╌╌╌╌╌╌╌┤
        │ None      │
        ╰───────────╯
        <BLANKLINE>
        (Showing first 2 of 2 rows)

    """
    return bytes_expr._eval_expressions("guess_mime_type")