{"record":{"id":"f2b9285c24dfdaf5","repo":"Mintplex-Labs/anything-llm","slug":"invalid-response-body-returned-from-deepseek-js","errorCode":null,"errorMessage":"Invalid response body returned from DeepSeek: ${JSON.stringify(result.output)}","messagePattern":"Invalid response body returned from DeepSeek: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"server/utils/AiProviders/deepseek/index.js","lineNumber":119,"sourceCode":"      );\n\n    const result = await LLMPerformanceMonitor.measureAsyncFunction(\n      this.openai.chat.completions\n        .create({\n          model: this.model,\n          messages,\n          temperature,\n        })\n        .catch((e) => {\n          throw new Error(e.message);\n        })\n    );\n\n    if (\n      !result?.output?.hasOwnProperty(\"choices\") ||\n      result?.output?.choices?.length === 0\n    )\n      throw new Error(\n        `Invalid response body returned from DeepSeek: ${JSON.stringify(result.output)}`\n      );\n\n    return {\n      textResponse: this.#parseReasoningFromResponse(result.output.choices[0]),\n      metrics: {\n        prompt_tokens: result.output.usage.prompt_tokens || 0,\n        completion_tokens: result.output.usage.completion_tokens || 0,\n        total_tokens: result.output.usage.total_tokens || 0,\n        outputTps: result.output.usage.completion_tokens / result.duration,\n        duration: result.duration,\n        model: this.model,\n        provider: this.className,\n        timestamp: new Date(),\n      },\n    };\n  }\n","sourceCodeStart":101,"sourceCodeEnd":137,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/526360e320da9d1b36074be5ed64fe76e5bbfbbd/server/utils/AiProviders/deepseek/index.js#L101-L137","documentation":"Thrown after a successful API call when the response object lacks a 'choices' property or has an empty choices array. This indicates DeepSeek returned a 200 response with an unexpected/empty body — not a standard error. The full response is JSON-stringified into the message for debugging. Unlike the Cohere/CometAPI providers which return null in this case, DeepSeek treats it as a hard error.","triggerScenarios":"DeepSeek returns a valid HTTP 200 but with an empty or malformed body (e.g. content-filtered response with no choices); the response shape changes due to a DeepSeek API update; the model returns only an error field without choices; usage is undefined causing downstream issues masked as a choices problem.","commonSituations":"Content policy triggers causing DeepSeek to omit choices; API version mismatch where the response schema shifted; rare server-side bugs returning incomplete payloads; reasoning_content-only responses on some model variants.","solutions":["Inspect the JSON-stringified result.output in the error message — it reveals exactly what DeepSeek returned.","If the body contains an error/filter field, adjust the prompt to avoid triggering content filters.","Check the DeepSeek API changelog for response schema changes.","Consider catching this specific error and returning null (as Cohere/CometAPI do) for graceful degradation."],"exampleFix":"// before\nif (\n  !result?.output?.hasOwnProperty(\"choices\") ||\n  result?.output?.choices?.length === 0\n)\n  throw new Error(\n    `Invalid response body returned from DeepSeek: ${JSON.stringify(result.output)}`\n  );\n\n// after — degrade gracefully like sibling providers\nif (\n  !result?.output?.hasOwnProperty(\"choices\") ||\n  result?.output?.choices?.length === 0\n) {\n  this.log(`Empty choices in DeepSeek response: ${JSON.stringify(result.output)}`);\n  return null;\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"/** Narrows a well-formed DeepSeek/OpenAI completion response. */\nfunction hasValidChoices(output) {\n  return (\n    output != null &&\n    typeof output === 'object' &&\n    Array.isArray(output.choices) &&\n    output.choices.length > 0\n  );\n}","tryCatchPattern":"try {\n  return await deepseek.getChatCompletion(messages, { temperature });\n} catch (e) {\n  if (/Invalid response body returned from DeepSeek/i.test(e.message)) {\n    console.error('DeepSeek returned an unexpected body:', e.message);\n    return null; // degrade gracefully instead of surfacing the error\n  }\n  throw e;\n}","preventionTips":["Treat empty-choices responses as soft failures (return null) rather than hard errors, matching the Cohere/CometAPI pattern.","Log the full response body when choices are empty to diagnose content-filter or schema issues.","Monitor for spikes in this error which may indicate an API contract change."],"tags":["deepseek","invalid-response","chat-completion","empty-choices","response-parsing"],"backgroundTag":null,"analyzedSha":"526360e320da9d1b36074be5ed64fe76e5bbfbbd","analyzedAt":"2026-08-13T01:45:47.170Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}