langgenius/dify · error · CompletionRequestError

completion_request_error

completion_request_error

Error message

Completion request failed.

What it means

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.

Source

Thrown at api/controllers/console/explore/completion.py:140

            )

            # response-contract:ignore compact_generate_response
            return helper.compact_generate_response(response)
        except services.errors.conversation.ConversationNotExistsError:
            raise NotFound("Conversation Not Exists.")
        except services.errors.conversation.ConversationCompletedError:
            raise ConversationCompletedError()
        except services.errors.app_model_config.AppModelConfigBrokenError:
            logger.exception("App model config broken.")
            raise AppUnavailableError()
        except ProviderTokenNotInitError as ex:
            raise ProviderNotInitializeError(ex.description)
        except QuotaExceededError:
            raise ProviderQuotaExceededError()
        except ModelCurrentlyNotSupportError:
            raise ProviderModelCurrentlyNotSupportError()
        except InvokeError as e:
            raise CompletionRequestError(e.description)
        except ValueError as e:
            raise e
        except Exception:
            logger.exception("internal server error.")
            raise InternalServerError()


@console_ns.route(
    "/installed-apps/<uuid:installed_app_id>/completion-messages/<string:task_id>/stop",
    endpoint="installed_app_stop_completion",
)
class CompletionStopApi(InstalledAppResource):
    @console_ns.response(200, "Success", console_ns.models[SimpleResultResponse.__name__])
    @with_current_user_id
    @with_session(write=False)
    def post(self, session: Session, current_user_id: str, installed_app: InstalledApp, task_id: str):
        app_model = installed_app.app_with_session(session=session)
        if app_model is None:

View on GitHub (pinned to ef8544b173)

Solutions

  1. Inspect the forwarded description and server logs (logger captures the original InvokeError).
  2. Reduce input size if the error indicates context-length or token-limit problems.
  3. Retry after a brief delay for transient provider/network errors.
  4. Verify the provider account is in good standing and the model is still available.
  5. Check provider-specific status pages for outages.

Example fix

# before: one-shot call with no retry on transient invoke errors
resp = post('/completion-messages', payload)

# after: retry with backoff for transient invoke failures, surface others
for attempt in range(3):
    try:
        resp = post('/completion-messages', payload); break
    except HttpError as e:
        if e.code == 'completion_request_error' and is_transient(e.message):
            sleep(2 ** attempt); continue
        raise
Defensive patterns

Strategy: retry

Validate before calling

// Heuristically avoid the most common InvokeError causes: token/context limits.
function estimateTokens(text) { return Math.ceil(text.length / 4); }
if (estimateTokens(query) > modelContextLimit) { trimOrAbort(); }

Type guard

function isTransientInvokeError(err) {
  const msg = (err?.message ?? '').toLowerCase();
  return /timeout|temporary|temporarily|unavailable|connection|5\d{2}/.test(msg);
}

Try / catch

try {
  await postCompletion(id, payload);
} catch (err) {
  if (err.code === 'completion_request_error' && isTransientInvokeError(err)) {
    await sleep(2 ** attempt * 1000); // exponential backoff, limited retries
    await postCompletion(id, payload);
  } else {
    // surface the forwarded description for non-transient failures
    showError(err.message);
  }
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of langgenius/dify@ef8544b173 (2026-08-12). Data as JSON: /api/errors/6b624be05996fdbb. Report an issue: GitHub.