{"record":{"id":"7e61adca1554a66d","repo":"janhq/jan","slug":"mlx-api-request-failed-with-status-response-stat","errorCode":null,"errorMessage":"MLX API request failed with status ${response.status}: ${JSON.stringify(errorData)}","messagePattern":"MLX API request failed with status (.+?): (.+?)","errorType":"http","errorClass":null,"httpStatus":null,"severity":"error","filePath":"extensions/mlx-extension/src/index.ts","lineNumber":433,"sourceCode":"      'Authorization': `Bearer ${sessionInfo.api_key}`,\n    }\n\n    const body = JSON.stringify(opts)\n\n    if (opts.stream) {\n      return this.handleStreamingResponse(url, headers, body, abortController)\n    }\n\n    const response = await fetch(url, {\n      method: 'POST',\n      headers,\n      body,\n      signal: abortController?.signal,\n    })\n\n    if (!response.ok) {\n      const errorData = await response.json().catch(() => null)\n      throw new Error(\n        `MLX API request failed with status ${response.status}: ${JSON.stringify(errorData)}`\n      )\n    }\n\n    const completionResponse = (await response.json()) as chatCompletion\n\n    if (completionResponse.choices?.[0]?.finish_reason === 'length') {\n      throw new Error(OUT_OF_CONTEXT_SIZE)\n    }\n\n    return completionResponse\n  }\n\n  private async *handleStreamingResponse(\n    url: string,\n    headers: HeadersInit,\n    body: string,\n    abortController?: AbortController","sourceCodeStart":415,"sourceCodeEnd":451,"githubUrl":"https://github.com/janhq/jan/blob/7205d770c1e097c3daf35a911176410e93bc5564/extensions/mlx-extension/src/index.ts#L415-L451","documentation":"In the non-streaming chat path, if the MLX server's /v1/chat/completions endpoint returns a non-OK HTTP status, the extension reads the error body (best-effort) and throws an error embedding the status code and response payload. This surfaces upstream server-side failures (bad request, model not loaded, server error).","triggerScenarios":"POST to http://localhost:<port>/v1/chat/completions returns 4xx/5xx — malformed request body, model not loaded in the server, invalid sampling parameters, or internal server error.","commonSituations":"Sending unsupported parameters (temperature/top_p combos the server rejects), calling chat after the model failed to load properly, MLX server version mismatch with the extension's expected OpenAI-compatible API.","solutions":["Read the embedded status and errorData in the message to identify the server-side cause","Verify the model is loaded and healthy before calling chat (hit /health)","Simplify the request (remove exotic parameters) to isolate which field the server rejects","Update the MLX server and extension to matching versions"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"const health = await fetch(`http://127.0.0.1:${sessionInfo.port}/health`)\nif (!health.ok) throw new Error('Model not ready; reload before chat')","typeGuard":"const isChatRequestValid = (r) => Array.isArray(r?.messages) && r.messages.length > 0 && typeof r?.model === 'string'","tryCatchPattern":"try {\n  return await chat(messages, model)\n} catch (e) {\n  const m = /status (\\d+)/.exec(e.message)\n  if (m && Number(m[1]) >= 500) throw e // retryable upstream\n  console.error('MLX rejected request:', e.message)\n  throw e\n}","preventionTips":["Validate request payload (messages, sampling params) before sending","Ensure the model finished loading before the first request","Keep server and extension versions in sync","Log full errorData JSON for diagnosis"],"tags":["mlx","http","api-error","openai-compatible"],"backgroundTag":"http-error-response","analyzedSha":"7205d770c1e097c3daf35a911176410e93bc5564","analyzedAt":"2026-09-17T14:27:30.100Z","contentChangedAt":"2026-09-17T14:27:30.100Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}