linshenkx/prompt-optimizer · warning · DataInvalidFormatError
"data" property is missing or not an object
Error message
"data" property is missing or not an object
What it means
OptimizationError thrown by validateMessageOptimizationRequest when no element of request.messages has an id equal to request.selectedMessageId. The optimizer locates the selected message in the array to know what to rewrite; a mismatch (stale id, different id format) fails here.
Source
Thrown at packages/core/src/services/data/manager.ts:115
let exportData: any;
try {
exportData = JSON.parse(dataString);
} catch (error) {
throw new DataInvalidJsonError(error instanceof Error ? error.message : String(error))
}
if (!exportData || typeof exportData !== 'object' || Array.isArray(exportData)) {
throw new DataInvalidFormatError('Data must be an object')
}
// Support both old and new format for backward compatibility
let dataToImport: Record<string, any>;
// New format: { version: 1, data: { ... } }
if (exportData.version) {
if (!exportData.data || typeof exportData.data !== 'object' || Array.isArray(exportData.data)) {
throw new DataInvalidFormatError('"data" property is missing or not an object')
}
dataToImport = exportData.data;
}
// Old format: direct data object { history: [...], models: [...], ... }
else if (exportData.history || exportData.models || exportData.imageModels || exportData.userTemplates || exportData.userSettings || exportData.contexts) {
dataToImport = exportData;
}
else {
throw new DataInvalidFormatError('Unrecognized data structure')
}
const errors: string[] = [];
// 使用各服务的importData接口
const serviceMap = [
{ service: this.historyManager, dataKey: 'history' },
{ service: this.modelManager, dataKey: 'models' },
...(this.imageModelManager ? [{ service: this.imageModelManager, dataKey: 'imageModels' }] : []),View on GitHub (pinned to 3e677b1d9f)
Solutions
- Re-derive selectedMessageId from the same array you pass: request.messages.find(m => m.id === selectedId) before calling.
- If using temporary local ids, replace them with persisted ids once the conversation syncs.
- Ensure id types match on both sides (always strings, or always numbers).
- If the list may have truncated, refetch full history before optimizing.
Example fix
// before
await promptService.optimizeMessage({ selectedMessageId: selectedId, messages, modelKey });
// after
const exists = messages.some(m => m.id === selectedId);
if (!exists) throw new Error('Selection is stale — message list changed');
await promptService.optimizeMessage({ selectedMessageId: selectedId, messages, modelKey }); Defensive patterns
Strategy: validation
Validate before calling
const found = messages.some(m => m.id === request.selectedMessageId);
if (!found) throw new Error('Selection stale — refetch messages'); Type guard
function selectionExists(messages: {id: string}[], id: string): boolean {
return messages.some(m => m.id === id);
} Try / catch
try { await svc.optimizeMessage(req); } catch (e) { if (e instanceof OptimizationError && /not found in messages/.test(e.message)) { await refreshMessages(); } } Prevention
- Derive selectedMessageId from the same array passed in
- Replace temp ids after persistence
- Keep id types consistent (string vs number)
When it happens
Trigger: Passing an id from a different message list than the one in messages — e.g. messages reloaded/regenerated (new ids) while selectedMessageId still holds the old id, or id type mismatch (number vs string).
Common situations: Chat regenerated after edit so message ids changed, optimistic local ids ('tmp-1') not matching server ids, strict vs loose equality on ids, pagination truncating the array so the selected message falls outside it.
Understand the failure class
Background: "Not found" and "does not exist" errors: why "Task not found", "No such folder", and "Can't find" fire when a lookup comes back empty — this error's family across 14 libraries.
Related errors
- DATA_ERROR_CODES.ELECTRON_API_UNAVAILABLE
- Data must be an object
- Unrecognized data structure
- CONTEXT_ERROR_CODES.STORAGE_ERROR
- CONTEXT_ERROR_CODES.IMPORT_FORMAT_ERROR
AI-assisted analysis of linshenkx/prompt-optimizer@3e677b1d9f (2026-08-27).
Data as JSON: /api/errors/b65287bb59c77480.
Report an issue: GitHub.