{"record":{"id":"03248b2c62247e12","repo":"linshenkx/prompt-optimizer","slug":"context-error-codes-import-format-error","errorCode":"CONTEXT_ERROR_CODES.IMPORT_FORMAT_ERROR","errorMessage":"Invalid context bundle data","messagePattern":"Invalid context bundle data","errorType":"error_code","errorClass":"ContextError","httpStatus":null,"severity":"error","filePath":"packages/core/src/services/context/electron-proxy.ts","lineNumber":105,"sourceCode":"  }\n\n  // === 导入导出 ===\n  async exportAll(): Promise<ContextBundle> {\n    return this.api.exportAll();\n  }\n\n  async importAll(bundle: ContextBundle, mode: ImportMode): Promise<ImportResult> {\n    return this.api.importAll(safeSerializeForIPC(bundle), mode);\n  }\n\n  // === IImportExportable 实现 ===\n  async exportData(): Promise<ContextBundle> {\n    return this.exportAll();\n  }\n\n  async importData(data: any): Promise<void> {\n    if (!(await this.validateData(data))) {\n      throw new ContextError(CONTEXT_ERROR_CODES.IMPORT_FORMAT_ERROR, 'Invalid context bundle data');\n    }\n    await this.importAll(data as ContextBundle, 'replace');\n  }\n\n  async getDataType(): Promise<string> {\n    return this.api.getDataType ? this.api.getDataType() : Promise.resolve('context-bundle');\n  }\n\n  async validateData(data: any): Promise<boolean> {\n    return this.api.validateData \n      ? this.api.validateData(safeSerializeForIPC(data))\n      : Promise.resolve(!!(data?.type && data?.type === 'context-bundle'));\n  }\n}\n","sourceCodeStart":87,"sourceCodeEnd":120,"githubUrl":"https://github.com/linshenkx/prompt-optimizer/blob/3e677b1d9f7e0493c142c175560531e7ae786dce/packages/core/src/services/context/electron-proxy.ts#L87-L120","documentation":"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.","triggerScenarios":"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.","commonSituations":"Model ignores JSON mode instructions; output truncated by maxOutputTokens; model answers conversationally ('Sure, here is...') without braces; long responses cut off mid-object.","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)"],"exampleFix":"// before\nconst systemPrompt = 'Extract the prompt.'\n\n// after\nconst systemPrompt =\n  'Extract the prompt. Respond with ONLY a single valid JSON object. ' +\n  'Do not wrap it in markdown. Do not add any explanation text.'\n// and ensure maxOutputTokens is large enough for the full object","handlingStrategy":"fallback","validationCode":"function containsJsonObject(text: string): boolean {\n  const match = text.match(/\\{[\\s\\S]*}/)\n  if (!match) return false\n  try {\n    return isRecord(JSON.parse(match[0]))\n  } catch {\n    return false\n  }\n}","typeGuard":"function isRecord(v: unknown): v is Record<string, unknown> {\n  return typeof v === 'object' && v !== null && !Array.isArray(v)\n}","tryCatchPattern":"try {\n  const obj = await extractor.resolvePromptObject(rawResponse)\n} catch (error) {\n  if (error instanceof Error && error.message.includes('missing a usable JSON prompt object')) {\n    return buildPromptFromPlainText(rawResponse) // degrade to plain-text prompt\n  }\n  throw error\n}","preventionTips":["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"],"tags":["structured-output","json-parsing","truncated-response","ai-model"],"backgroundTag":"invalid-model-json-output","analyzedSha":"3e677b1d9f7e0493c142c175560531e7ae786dce","analyzedAt":"2026-08-27T21:29:16.709Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}