{"record":{"id":"e472c427cc057130","repo":"BabylonJS/Babylon.js","slug":"gltfloaderunexpectedmagicerror","errorCode":"GLTFLoaderUnexpectedMagicError","errorMessage":"Unexpected magic: ","messagePattern":"Unexpected magic: ","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"packages/dev/loaders/src/glTF/glTFFileLoader.pure.ts","lineNumber":1263,"sourceCode":"        this._startPerformanceCounter(\"Parse JSON\");\r\n        this._log(`JSON length: ${json.length}`);\r\n        const parsed = JSON.parse(json);\r\n        this._endPerformanceCounter(\"Parse JSON\");\r\n        return parsed;\r\n    }\r\n\r\n    private _unpackBinaryAsync(dataReader: DataReader): Promise<IGLTFLoaderData> {\r\n        this._startPerformanceCounter(\"Unpack Binary\");\r\n\r\n        // Read magic + version + length + json length + json format\r\n        return dataReader.loadAsync(20).then(() => {\r\n            const Binary = {\r\n                Magic: 0x46546c67,\r\n            };\r\n\r\n            const magic = dataReader.readUint32();\r\n            if (magic !== Binary.Magic) {\r\n                throw new RuntimeError(\"Unexpected magic: \" + magic, ErrorCodes.GLTFLoaderUnexpectedMagicError);\r\n            }\r\n\r\n            const version = dataReader.readUint32();\r\n\r\n            if (this.loggingEnabled) {\r\n                this._log(`Binary version: ${version}`);\r\n            }\r\n\r\n            const length = dataReader.readUint32();\r\n            if (!this.useRangeRequests && length !== dataReader.buffer.byteLength) {\r\n                Logger.Warn(`Length in header does not match actual data length: ${length} != ${dataReader.buffer.byteLength}`);\r\n            }\r\n\r\n            let unpacked: Promise<IGLTFLoaderData>;\r\n            switch (version) {\r\n                case 1: {\r\n                    unpacked = this._unpackBinaryV1Async(dataReader, length);\r\n                    break;\r","sourceCodeStart":1245,"sourceCodeEnd":1281,"githubUrl":"https://github.com/BabylonJS/Babylon.js/blob/0592b347b8a4ee0236089ea86a749cacfdb266d8/packages/dev/loaders/src/glTF/glTFFileLoader.pure.ts#L1245-L1281","documentation":"Thrown as a RuntimeError (code GLTFLoaderUnexpectedMagicError) when the first 4 bytes of a binary glTF file do not equal the magic number 0x46546C67 ('glTF'). The loader uses this constant to confirm the stream is a binary glTF container.","triggerScenarios":"Calling a load path that expects binary glTF (.glb) but the fetched bytes are JSON text, HTML (e.g. a 404 page), or a truncated file.","commonSituations":"Server returning an HTML error page with 200 status; wrong file extension/URL; serving .gltf JSON to a .glb path; gzip/CDN mangling the bytes.","solutions":["Verify the URL returns an actual .glb file (check first bytes are 'glTF')","Check the HTTP response status/content-type and fix server routing so errors aren't served as 200","Re-download/re-export the file — it may be corrupted or truncated"],"exampleFix":"// before\nscene.importMeshesFromMeshesAsync(\"model.gltf.url/that/404s.glb\"); // server returns HTML 200\n// after\nconst res = await fetch(url);\nif (!res.ok || !(await res.arrayBuffer()).byteLength) throw new Error(\"bad asset\");\nawait scene.importMeshesFromMeshesAsync(url);","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Add a download integrity check (size + magic bytes) before handing buffers to the loader"],"tags":["gltf","binary","file-format"],"backgroundTag":"invalid-file-magic","analyzedSha":"0592b347b8a4ee0236089ea86a749cacfdb266d8","analyzedAt":"2026-08-30T15:11:20.442Z","schemaVersion":2},"datasetVersion":"2026-08-30T18:17:15.746Z"}