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, and HTML. 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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, and HTML. 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")