DayuanJiang/next-ai-draw-io · error · Error

Unexpected response format: ${contentType}

Error message

Unexpected response format: ${contentType}

What it means

After a successful ModelScope HTTP 200, the validate-model route requires the response content-type to be text/event-stream or application/json; anything else (HTML error page, empty content-type) triggers this error.

Source

Thrown at app/api/validate-model/route.ts:324

                        },
                    )

                    if (!response.ok) {
                        const errorText = await response.text()
                        throw new Error(
                            `ModelScope API error (${response.status}): ${errorText}`,
                        )
                    }

                    const contentType =
                        response.headers.get("content-type") || ""
                    const isValidStreamingResponse =
                        response.status === 200 &&
                        (contentType.includes("text/event-stream") ||
                            contentType.includes("application/json"))

                    if (!isValidStreamingResponse) {
                        throw new Error(
                            `Unexpected response format: ${contentType}`,
                        )
                    }

                    const responseTime = Date.now() - startTime

                    if (response.body) {
                        response.body.cancel().catch(() => {
                            /* Ignore cancellation errors */
                        })
                    }

                    return NextResponse.json({
                        valid: true,
                        responseTime,
                        note: "ModelScope model validated (using streaming API)",
                    })
                } catch (error) {

View on GitHub (pinned to 155ef4f7ac)

Solutions

  1. Log the full contentType and response body to see what was actually returned
  2. Verify the request URL is the ModelScope inference API endpoint, not the website
  3. Check for proxies/middleware that rewrite the response
  4. If ModelScope changed its response format, update isValidStreamingResponse to accept it

Example fix

// before
if (!isValidStreamingResponse) {
  throw new Error(`Unexpected response format: ${contentType}`)
}

// after
if (!isValidStreamingResponse) {
  const body = await response.text().catch(() => '')
  throw new Error(
    `Unexpected response format: ${contentType}; body: ${body.slice(0, 200)}`,
  )
}
Defensive patterns

Strategy: validation

Validate before calling

const ACCEPTED = ['text/event-stream', 'application/json']
const ct = (await fetch(url)).headers.get('content-type') || ''
if (!ACCEPTED.some((t) => ct.includes(t))) {
  // don't attempt to stream; surface a config error
}

Type guard

const isStreamableContent = (ct: string): boolean =>
  ct.includes('text/event-stream') || ct.includes('application/json')

Try / catch

try { /* consume stream */ } catch (e) {
  if ((e as Error).message.startsWith('Unexpected response format')) {
    logContentType(); checkEndpointUrl()
  }
}

Prevention

When it happens

Trigger: ModelScope returns 200 with text/html (login/captcha page), text/plain, or an empty content-type instead of the expected SSE stream or JSON.

Common situations: Gateway/CDN intercepting the request and returning HTML, wrong endpoint URL that serves a web page, or ModelScope API contract changes.

Related errors


AI-assisted analysis of DayuanJiang/next-ai-draw-io@155ef4f7ac (2026-08-27). Data as JSON: /api/errors/8914688b0602c16e. Report an issue: GitHub.