{"record":{"id":"0f969b06f65276f7","repo":"diegosouzapw/OmniRoute","slug":"failed-to-parse-stage-response-as-json","errorCode":null,"errorMessage":"Failed to parse stage response as JSON","messagePattern":"Failed to parse stage response as JSON","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"open-sse/services/autoCombo/pipelineRouter.ts","lineNumber":174,"sourceCode":"\n    // Intermediate stage: buffer and extract text\n    if (!response.ok) {\n      const errorText = await response.text().catch(() => \"unknown error\");\n      log.warn(\"PIPELINE\", `Stage returned ${response.status}: ${errorText.slice(0, 200)}`);\n      return { text: \"\" };\n    }\n\n    try {\n      const json = await response.json();\n      // OpenAI chat completions format: choices[0].message.content\n      const content = json?.choices?.[0]?.message?.content;\n      if (typeof content === \"string\") {\n        return { text: content };\n      }\n      // Fallback: try to extract any text field\n      return { text: JSON.stringify(json) };\n    } catch {\n      log.warn(\"PIPELINE\", \"Failed to parse stage response as JSON\");\n      return { text: \"\" };\n    }\n  };\n}\n\n// ---------------------------------------------------------------------------\n// Token estimation\n// ---------------------------------------------------------------------------\n\n/**\n * Rough token estimate from message content (4 chars ≈ 1 token).\n */\nfunction estimateTokens(messages: Array<{ role: string; content: unknown }>): number {\n  let total = 0;\n  for (const msg of messages) {\n    if (typeof msg.content === \"string\") {\n      total += Math.ceil(msg.content.length / 4);\n    } else if (Array.isArray(msg.content)) {","sourceCodeStart":156,"sourceCodeEnd":192,"githubUrl":"https://github.com/diegosouzapw/OmniRoute/blob/a179ffed5bb2e0b883b9ae7214ce8717b2a94c4d/open-sse/services/autoCombo/pipelineRouter.ts#L156-L192","documentation":"Pipeline-router warning: an intermediate pipeline stage returned HTTP 200 but its body could not be parsed as JSON (or carried no extractable text), so the stage contributes empty text to the next stage. Typically caused by an upstream returning HTML error pages, truncated SSE, or a non-chat-completions payload shape.","triggerScenarios":"Thrown at open-sse/services/autoCombo/pipelineRouter.ts:174 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Log the first ~200 chars of the stage body to identify HTML/truncated/non-JSON output.","Verify the stage executor targets a chat-completions endpoint returning choices[0].message.content.","Add response content-type validation before json() and retry the stage once on parse failure."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"a179ffed5bb2e0b883b9ae7214ce8717b2a94c4d","analyzedAt":"2026-08-25T18:35:09.898Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}