diegosouzapw/OmniRoute · error

Failed to parse stage response as JSON

Error message

Failed to parse stage response as JSON

What it means

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.

Solutions

  1. Log the first ~200 chars of the stage body to identify HTML/truncated/non-JSON output.
  2. Verify the stage executor targets a chat-completions endpoint returning choices[0].message.content.
  3. Add response content-type validation before json() and retry the stage once on parse failure.
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at open-sse/services/autoCombo/pipelineRouter.ts:174 when the library encounters an invalid state.

Common situations: See trigger scenarios.

Understand the failure class


AI-assisted analysis of diegosouzapw/OmniRoute@a179ffed5b (2026-08-25). Data as JSON: /api/errors/0f969b06f65276f7. Report an issue: GitHub.

Appendix: source

Thrown at open-sse/services/autoCombo/pipelineRouter.ts:174

    // Intermediate stage: buffer and extract text
    if (!response.ok) {
      const errorText = await response.text().catch(() => "unknown error");
      log.warn("PIPELINE", `Stage returned ${response.status}: ${errorText.slice(0, 200)}`);
      return { text: "" };
    }

    try {
      const json = await response.json();
      // OpenAI chat completions format: choices[0].message.content
      const content = json?.choices?.[0]?.message?.content;
      if (typeof content === "string") {
        return { text: content };
      }
      // Fallback: try to extract any text field
      return { text: JSON.stringify(json) };
    } catch {
      log.warn("PIPELINE", "Failed to parse stage response as JSON");
      return { text: "" };
    }
  };
}

// ---------------------------------------------------------------------------
// Token estimation
// ---------------------------------------------------------------------------

/**
 * Rough token estimate from message content (4 chars ≈ 1 token).
 */
function estimateTokens(messages: Array<{ role: string; content: unknown }>): number {
  let total = 0;
  for (const msg of messages) {
    if (typeof msg.content === "string") {
      total += Math.ceil(msg.content.length / 4);
    } else if (Array.isArray(msg.content)) {

View on GitHub (pinned to a179ffed5b)