{"record":{"id":"6b624be05996fdbb","repo":"langgenius/dify","slug":"completion-request-error-6b624b","errorCode":"completion_request_error","errorMessage":"Completion request failed.","messagePattern":"Completion request failed\\.","errorType":"console","errorClass":"CompletionRequestError","httpStatus":400,"severity":"error","filePath":"api/controllers/console/explore/completion.py","lineNumber":140,"sourceCode":"            )\n\n            # response-contract:ignore compact_generate_response\n            return helper.compact_generate_response(response)\n        except services.errors.conversation.ConversationNotExistsError:\n            raise NotFound(\"Conversation Not Exists.\")\n        except services.errors.conversation.ConversationCompletedError:\n            raise ConversationCompletedError()\n        except services.errors.app_model_config.AppModelConfigBrokenError:\n            logger.exception(\"App model config broken.\")\n            raise AppUnavailableError()\n        except ProviderTokenNotInitError as ex:\n            raise ProviderNotInitializeError(ex.description)\n        except QuotaExceededError:\n            raise ProviderQuotaExceededError()\n        except ModelCurrentlyNotSupportError:\n            raise ProviderModelCurrentlyNotSupportError()\n        except InvokeError as e:\n            raise CompletionRequestError(e.description)\n        except ValueError as e:\n            raise e\n        except Exception:\n            logger.exception(\"internal server error.\")\n            raise InternalServerError()\n\n\n@console_ns.route(\n    \"/installed-apps/<uuid:installed_app_id>/completion-messages/<string:task_id>/stop\",\n    endpoint=\"installed_app_stop_completion\",\n)\nclass CompletionStopApi(InstalledAppResource):\n    @console_ns.response(200, \"Success\", console_ns.models[SimpleResultResponse.__name__])\n    @with_current_user_id\n    @with_session(write=False)\n    def post(self, session: Session, current_user_id: str, installed_app: InstalledApp, task_id: str):\n        app_model = installed_app.app_with_session(session=session)\n        if app_model is None:","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/langgenius/dify/blob/ef8544b173fd6cd7a8e71df2cab576e52bebbfbc/api/controllers/console/explore/completion.py#L122-L158","documentation":"Raised by CompletionApi.post (HTTP 400, error_code 'completion_request_error') as a catch for graphon.model_runtime.errors.invoke.InvokeError — the base class for model-runtime invocation failures (network, auth-during-call, context-length, content filter, provider 5xx, etc.). The exception's description is forwarded to the client. This is the generic 'the model call itself failed' bucket after all specific provider errors were ruled out.","triggerScenarios":"POST /console/installed-apps/<id>/completion-messages where AppGenerateService reaches the actual model invocation and the runtime raises InvokeError or a subclass not caught earlier (e.g., InvokeBadRequestError, InvokeConnectionError, InvokeServerAvailableError).","commonSituations":"Upstream provider returned a 4xx/5xx (context length exceeded, invalid request, safety filter); transient network error between Dify and the provider; provider rate-limited at the account level (not the Dify-hosted quota); malformed model-specific parameters; model deprecated mid-flight.","solutions":["Inspect the forwarded description and server logs (logger captures the original InvokeError).","Reduce input size if the error indicates context-length or token-limit problems.","Retry after a brief delay for transient provider/network errors.","Verify the provider account is in good standing and the model is still available.","Check provider-specific status pages for outages."],"exampleFix":"# before: one-shot call with no retry on transient invoke errors\nresp = post('/completion-messages', payload)\n\n# after: retry with backoff for transient invoke failures, surface others\nfor attempt in range(3):\n    try:\n        resp = post('/completion-messages', payload); break\n    except HttpError as e:\n        if e.code == 'completion_request_error' and is_transient(e.message):\n            sleep(2 ** attempt); continue\n        raise","handlingStrategy":"retry","validationCode":"// Heuristically avoid the most common InvokeError causes: token/context limits.\nfunction estimateTokens(text) { return Math.ceil(text.length / 4); }\nif (estimateTokens(query) > modelContextLimit) { trimOrAbort(); }","typeGuard":"function isTransientInvokeError(err) {\n  const msg = (err?.message ?? '').toLowerCase();\n  return /timeout|temporary|temporarily|unavailable|connection|5\\d{2}/.test(msg);\n}","tryCatchPattern":"try {\n  await postCompletion(id, payload);\n} catch (err) {\n  if (err.code === 'completion_request_error' && isTransientInvokeError(err)) {\n    await sleep(2 ** attempt * 1000); // exponential backoff, limited retries\n    await postCompletion(id, payload);\n  } else {\n    // surface the forwarded description for non-transient failures\n    showError(err.message);\n  }\n}","preventionTips":["Trim large inputs to stay under model context limits.","Keep credentials funded and valid to avoid provider-side failures.\n        \"Monitor provider status pages during incidents.","Wrap calls in bounded exponential-backoff retry for transient invoke errors only."],"tags":["explore","completion","model-runtime","invoke","upstream","transient"],"backgroundTag":null,"analyzedSha":"ef8544b173fd6cd7a8e71df2cab576e52bebbfbc","analyzedAt":"2026-08-12T05:15:17.394Z","schemaVersion":2},"datasetVersion":"2026-08-12T12:31:55.035Z"}