Skip to main content

A tool to determine the content type of a file with deep learning

Project description

Magika Python Package

image NPM Version image image OpenSSF Best Practices CodeQL Actions status PyPI Monthly Downloads PyPI Downloads

Magika is a novel AI-powered file type detection tool that relies on the recent advance of deep learning to provide accurate detection. Under the hood, Magika employs a custom, highly optimized model that only weighs about a few MBs, and enables precise file identification within milliseconds, even when running on a single CPU. Magika has been trained and evaluated on a dataset of ~100M samples across 200+ content types (covering both binary and textual file formats), and it achieves an average ~99% accuracy on our test set.

Use Magika as a command line client or in your Python code!

You can find more information on which content types are supported, extended documentation, and bindings for other languages on Magika's website at https://securityresearch.google/magika/.

Installing Magika

Magika is available as magika on PyPI:

To install the most recent stable version:

pip install magika

If you intend to use Magika only as a command line, you may want to use pipx install magika instead.

If you want to test out the latest release candidate, you can install it with pip install --pre magika.

Using Magika as a command-line tool

Beginning with version 0.6.0, the magika Python package includes a pre-compiled Rust-based command-line tool, replacing the previous Python version. This binary is distributed as platform-specific wheels for most common architectures. For unsupported platforms, a pure-Python wheel is also available, providing the legacy Python client as a fallback.

$ cd tests_data/basic && magika -r *
asm/code.asm: Assembly (code)
batch/simple.bat: DOS batch file (code)
c/code.c: C source (code)
css/code.css: CSS source (code)
csv/magika_test.csv: CSV document (code)
dockerfile/Dockerfile: Dockerfile (code)
docx/doc.docx: Microsoft Word 2007+ document (document)
epub/doc.epub: EPUB document (document)
epub/magika_test.epub: EPUB document (document)
flac/test.flac: FLAC audio bitstream data (audio)
handlebars/example.handlebars: Handlebars source (code)
html/doc.html: HTML document (code)
ini/doc.ini: INI configuration file (text)
javascript/code.js: JavaScript source (code)
jinja/example.j2: Jinja template (code)
jpeg/magika_test.jpg: JPEG image data (image)
json/doc.json: JSON document (code)
latex/sample.tex: LaTeX document (text)
makefile/simple.Makefile: Makefile source (code)
markdown/README.md: Markdown document (text)
[...]
$ magika ./tests_data/basic/python/code.py --json
[
  {
    "path": "./tests_data/basic/python/code.py",
    "result": {
      "status": "ok",
      "value": {
        "dl": {
          "description": "Python source",
          "extensions": [
            "py",
            "pyi"
          ],
          "group": "code",
          "is_text": true,
          "label": "python",
          "mime_type": "text/x-python"
        },
        "output": {
          "description": "Python source",
          "extensions": [
            "py",
            "pyi"
          ],
          "group": "code",
          "is_text": true,
          "label": "python",
          "mime_type": "text/x-python"
        },
        "score": 0.753000020980835
      }
    }
  }
]
$ cat doc.ini | magika -
-: INI configuration file (text)
$ magika --help
Determines the content type of files with deep-learning

Usage: magika [OPTIONS] [PATH]...

Arguments:
  [PATH]...
          List of paths to the files to analyze.

          Use a dash (-) to read from standard input (can only be used once).

Options:
  -r, --recursive
          Identifies files within directories instead of identifying the directory itself

      --no-dereference
          Identifies symbolic links as is instead of identifying their content by following them

      --colors
          Prints with colors regardless of terminal support

      --no-colors
          Prints without colors regardless of terminal support

  -s, --output-score
          Prints the prediction score in addition to the content type

  -i, --mime-type
          Prints the MIME type instead of the content type description

  -l, --label
          Prints a simple label instead of the content type description

      --json
          Prints in JSON format

      --jsonl
          Prints in JSONL format

      --format <CUSTOM>
          Prints using a custom format (use --help for details).

          The following placeholders are supported:

            %p  The file path
            %l  The unique label identifying the content type
            %d  The description of the content type
            %g  The group of the content type
            %m  The MIME type of the content type
            %e  Possible file extensions for the content type
            %s  The score of the content type for the file
            %S  The score of the content type for the file in percent
            %b  The model output if overruled (empty otherwise)
            %%  A literal %

  -h, --help
          Print help (see a summary with '-h')

  -V, --version
          Print version

Check the Rust CLI docs for more information.

Using Magika as a Python module

Here is a few examples on how to use the Magika Python module:

>>> from magika import Magika
>>> m = Magika()
>>> res = m.identify_bytes(b'function log(msg) {console.log(msg);}')
>>> print(res.output.label)
javascript
>>> from magika import Magika
>>> m = Magika()
>>> res = m.identify_path('./tests_data/basic/ini/doc.ini')
>>> print(res.output.label)
ini
>>> from magika import Magika
>>> m = Magika()
>>> with open('./tests_data/basic/ini/doc.ini', 'rb') as f:
>>>     res = m.identify_stream(f)
>>> print(res.output.label)
ini

Core Concepts

To get the most out of Magika, it's worth learning about its core concepts. You can read about how Magika works, the models, the supported content types, the prediction modes, and notes on how to understand all parts of the output in the Core Concepts section of Magika's website.

API documentation

The Python API is documented in the Python bindings section of Magika's website, and via docstrings in the Magika module source code.

Research Paper and Citation

We describe how we developed Magika and the choices we made in our research paper, which was accepted at the International Conference on Software Engineering (ICSE) 2025. You can find a copy of the paper here.

If you use this software for your research, please cite it as:

@InProceedings{fratantonio25:magika,
  author = {Yanick Fratantonio and Luca Invernizzi and Loua Farah and Kurt Thomas and Marina Zhang and Ange Albertini and Francois Galilee and Giancarlo Metitieri and Julien Cretin and Alexandre Petit-Bianco and David Tao and Elie Bursztein},
  title = {{Magika: AI-Powered Content-Type Detection}},
  booktitle = {Proceedings of the International Conference on Software Engineering (ICSE)},
  month = {April},
  year = {2025}
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

magika-1.0.2.tar.gz (3.0 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

magika-1.0.2-py3-none-win_amd64.whl (13.1 MB view details)

Uploaded Python 3Windows x86-64

magika-1.0.2-py3-none-manylinux_2_28_x86_64.whl (15.9 MB view details)

Uploaded Python 3manylinux: glibc 2.28+ x86-64

magika-1.0.2-py3-none-macosx_11_0_arm64.whl (13.8 MB view details)

Uploaded Python 3macOS 11.0+ ARM64

magika-1.0.2-py3-none-any.whl (3.0 MB view details)

Uploaded Python 3

File details

Details for the file magika-1.0.2.tar.gz.

File metadata

  • Download URL: magika-1.0.2.tar.gz
  • Upload date:
  • Size: 3.0 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for magika-1.0.2.tar.gz
Algorithm Hash digest
SHA256 8ed912d8f14d044f43fdbd17d6bd2cbdd6e8b8246e89be49f6cd547053636677
MD5 a0b025e3e9b4e0691cfaca359170179c
BLAKE2b-256 79cadfb30534be5ad84363e0e8ce08bc6e990ce0430aec1eaafb0633b4bb3f7f

See more details on using hashes here.

Provenance

The following attestation bundles were made for magika-1.0.2.tar.gz:

Publisher: python-build-and-release-package.yml on google/magika

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file magika-1.0.2-py3-none-win_amd64.whl.

File metadata

  • Download URL: magika-1.0.2-py3-none-win_amd64.whl
  • Upload date:
  • Size: 13.1 MB
  • Tags: Python 3, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for magika-1.0.2-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 4937e876d55642423d6416e5db4e5ca7523ab7f855cbc5389efdeac1d149df04
MD5 176f938da02994ae5e69f55c953887a1
BLAKE2b-256 1a12185a8822994a2f7b5e7d88d19a88d80637917bbb0a6f3f59a2564aabc125

See more details on using hashes here.

Provenance

The following attestation bundles were made for magika-1.0.2-py3-none-win_amd64.whl:

Publisher: python-build-and-release-package.yml on google/magika

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file magika-1.0.2-py3-none-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for magika-1.0.2-py3-none-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 df4706c18153431548b1d36c8ca11c8a8a415197dcc741281846c61ebfc94a5b
MD5 7596e39bfc1a5294e5d79ced60cbdf1f
BLAKE2b-256 ba9e429608833917b7d4c4f7071a270bbca96821fb592e275d85bc9eae5a94c8

See more details on using hashes here.

Provenance

The following attestation bundles were made for magika-1.0.2-py3-none-manylinux_2_28_x86_64.whl:

Publisher: python-build-and-release-package.yml on google/magika

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file magika-1.0.2-py3-none-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for magika-1.0.2-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1db8e2d57556e7244f5fce9cfd023aa0da05d204ea7313f3c75b32feab2bcd6d
MD5 8f79a836c2f72860c48efab2db389614
BLAKE2b-256 38f3a65650c36a472fed1ca1c4868e567cf015c14c73a6bb5fa4a808932e0944

See more details on using hashes here.

Provenance

The following attestation bundles were made for magika-1.0.2-py3-none-macosx_11_0_arm64.whl:

Publisher: python-build-and-release-package.yml on google/magika

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file magika-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: magika-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 3.0 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for magika-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c50be7a6a7132ef1a92956694401aaf911bda8fc5e2a591092e0dac5b5865a8a
MD5 8e078f294e5a0705a4dac032573dcd28
BLAKE2b-256 1246b8180a34c64470e2f40a3676ef3284a32efd2b3598aa99946ee319eb66e8

See more details on using hashes here.

Provenance

The following attestation bundles were made for magika-1.0.2-py3-none-any.whl:

Publisher: python-build-and-release-package.yml on google/magika

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page