linshenkx/prompt-optimizer · error · ContextError
CONTEXT_ERROR_CODES.IMPORT_FORMAT_ERROR
CONTEXT_ERROR_CODES.IMPORT_FORMAT_ERROR
Error message
Invalid context bundle data
What it means
Last-resort failure in resolvePromptObject: every strategy to obtain a JSON object from the model response failed — direct parse, extraction of embedded JSON, etc. (earlier attempts are swallowed by the catch with 'ignore and fall through'). The model output contained no usable JSON object at all.
Solutions
- Increase maxOutputTokens so the JSON isn't truncated
- Reinforce JSON-only output in the system prompt and keep responseMimeType 'application/json'
- Retry once — LLM JSON compliance is nondeterministic
- Log the raw response to see which extraction strategy failed and adjust the slice logic (e.g. handle fenced ```json blocks)
Example fix
// before const systemPrompt = 'Extract the prompt.' // after const systemPrompt = 'Extract the prompt. Respond with ONLY a single valid JSON object. ' + 'Do not wrap it in markdown. Do not add any explanation text.' // and ensure maxOutputTokens is large enough for the full object
Defensive patterns
Strategy: fallback
Validate before calling
function containsJsonObject(text: string): boolean {
const match = text.match(/\{[\s\S]*}/)
if (!match) return false
try {
return isRecord(JSON.parse(match[0]))
} catch {
return false
}
} Type guard
function isRecord(v: unknown): v is Record<string, unknown> {
return typeof v === 'object' && v !== null && !Array.isArray(v)
} Try / catch
try {
const obj = await extractor.resolvePromptObject(rawResponse)
} catch (error) {
if (error instanceof Error && error.message.includes('missing a usable JSON prompt object')) {
return buildPromptFromPlainText(rawResponse) // degrade to plain-text prompt
}
throw error
} Prevention
- Set a generous maxOutputTokens to prevent truncation
- Instruct the model to output JSON only, no prose or markdown
- Retry once — LLM format compliance is probabilistic
When it happens
Trigger: Calling promptObject / resolvePromptObject when the model returns prose without any JSON, markdown code fences that don't contain a complete object, or truncated output that can't be parsed from any extracted slice.
Common situations: Model ignores JSON mode instructions; output truncated by maxOutputTokens; model answers conversationally ('Sure, here is...') without braces; long responses cut off mid-object.
Related errors
AI-assisted analysis of linshenkx/prompt-optimizer@3e677b1d9f (2026-08-27).
Data as JSON: /api/errors/03248b2c62247e12.
Report an issue: GitHub.
Appendix: source
Thrown at packages/core/src/services/context/electron-proxy.ts:105
}
// === 导入导出 ===
async exportAll(): Promise<ContextBundle> {
return this.api.exportAll();
}
async importAll(bundle: ContextBundle, mode: ImportMode): Promise<ImportResult> {
return this.api.importAll(safeSerializeForIPC(bundle), mode);
}
// === IImportExportable 实现 ===
async exportData(): Promise<ContextBundle> {
return this.exportAll();
}
async importData(data: any): Promise<void> {
if (!(await this.validateData(data))) {
throw new ContextError(CONTEXT_ERROR_CODES.IMPORT_FORMAT_ERROR, 'Invalid context bundle data');
}
await this.importAll(data as ContextBundle, 'replace');
}
async getDataType(): Promise<string> {
return this.api.getDataType ? this.api.getDataType() : Promise.resolve('context-bundle');
}
async validateData(data: any): Promise<boolean> {
return this.api.validateData
? this.api.validateData(safeSerializeForIPC(data))
: Promise.resolve(!!(data?.type && data?.type === 'context-bundle'));
}
}
View on GitHub (pinned to 3e677b1d9f)