{"record":{"id":"bd8e23bd29621eb6","repo":"windmill-labs/windmill","slug":"failed-to-parse-or-apply-json-error-instanceof","errorCode":null,"errorMessage":"Failed to parse or apply JSON: ${error instanceof Error ? error.message : String(error)}","messagePattern":"Failed to parse or apply JSON: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"frontend/src/lib/components/copilot/chat/flow/FlowAIChat.svelte","lineNumber":245,"sourceCode":"\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tconst result = applyFlowJsonUpdate(flowStore.val, inlineScriptSession, {\n\t\t\t\t\tmodules,\n\t\t\t\t\tschema,\n\t\t\t\t\tpreprocessorModule,\n\t\t\t\t\tfailureModule,\n\t\t\t\t\tgroups,\n\t\t\t\t\tnotes,\n\t\t\t\t\tsettings\n\t\t\t\t})\n\n\t\t\t\t// Refresh the state store to update UI\n\t\t\t\trefreshStateStore(flowStore)\n\t\t\t\tawait acceptPendingFlowEditsIfEnabled(true)\n\t\t\t\treturn result\n\t\t\t} catch (error) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`Failed to parse or apply JSON: ${error instanceof Error ? error.message : String(error)}`\n\t\t\t\t)\n\t\t\t}\n\t\t}\n\t}\n\n\t$effect(() => {\n\t\tif (\n\t\t\t!aiChatManager.autoAcceptEditsActive &&\n\t\t\t$currentEditor?.type === 'script' &&\n\t\t\tselectedId &&\n\t\t\tdiffManager?.moduleActions[selectedId]?.pending &&\n\t\t\t$currentEditor.editor.getAiChatEditorHandler()\n\t\t) {\n\t\t\tconst moduleLastSnapshot = diffManager.beforeFlow\n\t\t\t\t? (findModuleInFlow(diffManager.beforeFlow.value, selectedId) ?? undefined)\n\t\t\t\t: undefined\n\t\t\tconst content =","sourceCodeStart":227,"sourceCodeEnd":263,"githubUrl":"https://github.com/windmill-labs/windmill/blob/e474e8803ce2ff5c2df09a58dab51d45f5c922ca/frontend/src/lib/components/copilot/chat/flow/FlowAIChat.svelte#L227-L263","documentation":"This wraps any failure raised while parsing and applying a JSON-based flow edit in FlowAIChat.svelte. The original error's message is appended so the developer sees the underlying cause (JSON.parse failure, schema mismatch, or an error thrown by the apply logic).","triggerScenarios":"The AI tool returns JSON that fails to parse (truncated/malformed output), or the parsed document fails during application (invalid flow structure, store update failure) inside the try block ending at `refreshStateStore(flowStore)` / `acceptPendingFlowEditsIfEnabled(true)`.","commonSituations":"Model emits markdown-fenced or truncated JSON; large flows hit output token limits; the apply step references modules/paths absent from the current flow.","solutions":["Read the wrapped inner message to identify the real failure (parse vs apply)","Retry the generation asking the model for strict raw JSON without code fences","Validate the JSON against the flow schema before applying; add a pre-parse sanitization step"],"exampleFix":"// before\nthrow new Error(`Failed to parse or apply JSON: ${error instanceof Error ? error.message : String(error)}`)\n// after\nconst raw = text.replace(/^```(?:json)?\\n?|\\n```$/g, '')\nlet parsed\ntry { parsed = JSON.parse(raw) } catch (e) {\n  throw new Error(`Invalid JSON from model: ${e.message}`)\n}","handlingStrategy":"try-catch","validationCode":"let parsed\ntry { parsed = JSON.parse(rawText) } catch (e) { throw new Error(`Model returned invalid JSON: ${e.message}`) }\nif (typeof parsed !== 'object' || parsed === null) throw new Error('Model JSON is not an object')","typeGuard":"function isJsonObject(v: unknown): v is Record<string, unknown> { return typeof v === 'object' && v !== null && !Array.isArray(v) }","tryCatchPattern":"try { result = applyFlowJson(raw) } catch (error) { console.error('flow json apply failed', error); sendUserToast(`Failed to parse or apply JSON: ${error.message}`, true) }","preventionTips":["Strip markdown code fences before JSON.parse","Request strict JSON output from the model","Cap flow size to avoid truncated generations"],"tags":["json","parsing","ai-chat"],"backgroundTag":"json-parse-failed","analyzedSha":"e474e8803ce2ff5c2df09a58dab51d45f5c922ca","analyzedAt":"2026-09-03T12:38:19.024Z","contentChangedAt":"2026-09-03T12:38:19.024Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}