What Information Is Included in the MagikaResult Object in Google Magika

The MagikaResult object contains three core attributes: path (the file location), status (an enum indicating scan success or specific failure modes), and prediction (a nested MagikaPrediction object containing the deep learning output, final content-type determination, and confidence score).

When you scan a file or byte sequence using the google/magika library, the API returns a structured container known as a MagikaResult object. This high-level result bundles all metadata about the scan operation, including whether the file was successfully processed and what content type was detected. Understanding the structure of this object is essential for integrating Magika's content detection into file processing pipelines.

Core Attributes of the MagikaResult Object

The path Attribute

The path field stores the location of the processed file. In the Python implementation at python/src/magika/types/magika_result.py (lines 31-33), this is a pathlib.Path object. In the JavaScript/TypeScript implementation at js/src/magika-result.ts (lines 18-20), it is returned as a standard string.

The status Attribute

The status field is an enumeration indicating whether the scan succeeded or failed. According to the source code in python/src/magika/types/status.py and js/src/status.ts, possible values include OK for successful scans, or error states such as FILE_NOT_FOUND_ERROR and IO_ERROR.

The prediction Attribute

The prediction field contains a MagikaPrediction object with detailed inference results. As defined in python/src/magika/types/magika_prediction.py and js/src/magika-prediction.ts, this nested object includes:

  • dl: The raw deep-learning model's ContentTypeInfo.
  • output: The final consolidated content-type (potentially overridden from the raw DL output).
  • score: A confidence float ranging from 0 to 1.

Python Convenience Properties

The Python implementation provides shortcut properties on the MagikaResult class (defined around lines 101-138 in python/src/magika/types/magika_result.py) that forward directly to the nested prediction object:

  • dl: Equivalent to prediction.dl.
  • output: Equivalent to prediction.output.
  • score: Equivalent to prediction.score.
  • asdict(): Serializes the result to a plain dictionary, converting the path to a string and including the full prediction when status == OK.

Accessing MagikaResult Data in Python

from pathlib import Path
from magika import Magika

magika = Magika()
result = magika.identify_path(Path("example.pdf"))

print("Path:", result.path)                  # pathlib.Path object

print("Status:", result.status.name)         # "OK"

print("Detected type:", result.output.label) # "application/pdf"

print("Score:", result.score)                # 0.98

print("Dict export:", result.asdict())

Accessing MagikaResult Data in JavaScript

import { Magika } from "./magika.js";

async function analyzeFile() {
  const magika = new Magika();
  const result = await magika.identifyPath("example.pdf");
  
  console.log("Path:", result.path);                    // "example.pdf"
  console.log("Status:", result.status);                // "OK"
  console.log("Type:", result.prediction.output.label); // "application/pdf"
  console.log("Score:", result.prediction.score);      // 0.98
}

Summary

  • The MagikaResult object encapsulates file scan results through three primary fields: path, status, and prediction.
  • Path representation varies by language: pathlib.Path in Python (lines 31-33 of magika_result.py) and string in JavaScript (js/src/magika-result.ts).
  • Status indicates success (OK) or specific error conditions like FILE_NOT_FOUND_ERROR.
  • Prediction contains the deep learning output (dl), final content-type (output), and confidence score.
  • Python users can access prediction data directly via convenience properties (result.output, result.score) and serialize results using asdict().

Frequently Asked Questions

What is the difference between prediction.dl and prediction.output in a MagikaResult object?

The dl field contains the raw content-type prediction directly from the deep learning model, while the output field represents the final content-type after any potential overrides or post-processing logic applied by the Magika engine. Both are ContentTypeInfo objects defined in python/src/magika/types/content_type_info.py.

How do I check if a file scan failed using the MagikaResult object?

Check the status attribute against the Status enum. If result.status.name (Python) or result.status (JavaScript) is not "OK", the scan encountered an error such as FILE_NOT_FOUND_ERROR or IO_ERROR, and the prediction field may be null or undefined.

Can I serialize the MagikaResult object to JSON in Python?

Yes. The Python implementation includes an asdict() method (implemented around lines 101-138 in python/src/magika/types/magika_result.py) that converts the result to a dictionary suitable for JSON serialization, converting the pathlib.Path to a string and including the full prediction data when the status is OK.

What data type is the score field in a MagikaResult object?

The score field is a floating-point number between 0 and 1 representing the model's confidence in the prediction. In Python, you can access it via result.score (shortcut) or result.prediction.score, while in JavaScript you access it through result.prediction.score.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →