{"record":{"id":"0ebc9089431e5d12","repo":"apify/crawlee","slug":"could-not-derive-the-default-values-of-the-custom","errorCode":null,"errorMessage":"Could not derive the default values of the custom statistics fields from `stateExtension.deserialize` - give every field a default, or declare `stateExtension.defaultState` explicitly.","messagePattern":"Could not derive the default values of the custom statistics fields from `stateExtension\\.deserialize` - give every field a default, or declare `stateExtension\\.defaultState` explicitly\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/core/src/crawlers/statistics.ts","lineNumber":438,"sourceCode":"        const { defaultState, deserialize } = options;\n\n        if (typeof defaultState === 'function') {\n            return defaultState;\n        }\n\n        if (defaultState !== undefined) {\n            return () => structuredClone(defaultState);\n        }\n\n        if (deserialize === undefined) {\n            return () => ({}) as StateExtension;\n        }\n\n        return () => {\n            try {\n                return convertStateSync(deserialize, {}, this.#persistStateKey);\n            } catch (error) {\n                throw new Error(\n                    'Could not derive the default values of the custom statistics fields from `stateExtension.deserialize` - ' +\n                        'give every field a default, or declare `stateExtension.defaultState` explicitly.',\n                    { cause: error },\n                );\n            }\n        };\n    }\n\n    /** The built-in half of {@apilink Statistics.defaultState}, before any custom fields are merged over it. */\n    #builtInDefaultState(): StatisticState {\n        return {\n            requestsFinished: 0,\n            requestsFailed: 0,\n            requestsRetries: 0,\n            requestsFailedPerMinute: 0,\n            requestsFinishedPerMinute: 0,\n            requestMinDurationMillis: Infinity,\n            requestMaxDurationMillis: 0,","sourceCodeStart":420,"sourceCodeEnd":456,"githubUrl":"https://github.com/apify/crawlee/blob/dbe57fb09ca607ad59dcf998f3925ef9ac3bb26c/packages/core/src/crawlers/statistics.ts#L420-L456","documentation":"Statistics must know the default values of every custom stateExtension field to initialize its state. When only a deserialize function is provided and it cannot be invoked synchronously to derive defaults on an empty object, the library throws this error. Provide per-field defaults or an explicit defaultState.","triggerScenarios":"Passing stateExtension with a deserialize that fails or is async/ambiguous when called on {} (no defaultState provided), during Statistics construction.","commonSituations":"Writing deserialize that assumes pre-populated input; providing an async deserializer; forgetting defaultState after refactoring the extension shape.","solutions":["Declare `stateExtension.defaultState` explicitly with a value for every field","Make deserialize return defaults when given an empty object (e.g. spread defaults)","Check the `cause` on this error to see why convertStateSync failed","Avoid async deserializers in stateExtension"],"exampleFix":"// before\nstateExtension: { deserialize: async (s) => JSON.parse(s) }\n// after\nstateExtension: { defaultState: { myCounter: 0 }, deserialize: (s) => JSON.parse(s) }","handlingStrategy":"validation","validationCode":"if (!stateExtension.defaultState && isAsyncLike(stateExtension.deserialize)) throw new Error('provide stateExtension.defaultState');","typeGuard":null,"tryCatchPattern":"try { new Statistics({ stateExtension }); } catch (e) { if (String(e).includes('Could not derive the default values')) addExplicitDefaultState(); else throw e; }","preventionTips":["Always provide explicit defaultState for custom extensions","Keep deserialize synchronous","Test Statistics construction with your extension in unit tests"],"tags":["statistics","configuration","deserialization"],"backgroundTag":"missing-default-state","analyzedSha":"dbe57fb09ca607ad59dcf998f3925ef9ac3bb26c","analyzedAt":"2026-08-30T22:22:28.328Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}