langgenius/dify · error · DraftWorkflowNotExist

draft_workflow_not_exist

draft_workflow_not_exist

Error message

Draft workflow need to be initialized.

What it means

Returned (HTTP 404, error_code `draft_workflow_not_exist`, message 'Draft workflow need to be initialized.') by GET /rag/pipelines/<pipeline_id>/workflows/draft/environment-variables when no draft workflow exists for the pipeline. Environment variables live on the draft workflow entity, so without one there is nothing to enumerate.

Source

Thrown at api/controllers/console/datasets/rag_pipeline/rag_pipeline_draft_variable.py:360


@console_ns.route("/rag/pipelines/<uuid:pipeline_id>/workflows/draft/environment-variables")
class RagPipelineEnvironmentVariableCollectionApi(Resource):
    @console_ns.response(
        200,
        "Environment variables retrieved successfully",
        console_ns.models[EnvironmentVariableListResponse.__name__],
    )
    @_api_prerequisite
    def get(self, _current_user: Account, pipeline: Pipeline):
        """
        Get draft workflow
        """
        # fetch draft workflow by app_model
        rag_pipeline_service = RagPipelineService(db.session())
        workflow = rag_pipeline_service.get_draft_workflow(pipeline=pipeline)
        if workflow is None:
            raise DraftWorkflowNotExist()

        env_vars = workflow.environment_variables
        env_vars_list = []
        for v in env_vars:
            env_vars_list.append(
                {
                    "id": v.id,
                    "type": "env",
                    "name": v.name,
                    "description": v.description,
                    "selector": v.selector,
                    "value_type": (
                        environment_variable_value_type(v)
                        if isinstance(v, LLMEnvironmentVariable)
                        else v.value_type.value
                    ),
                    "value": v.value,
                    # Do not track edited for env vars.

View on GitHub (pinned to ef8544b173)

Solutions

  1. Call POST /rag/pipelines/<pipeline_id>/workflows/draft with an initial graph to create the draft workflow.
  2. Gate the environment-variables fetch behind a successful get-draft check.
  3. In the UI, show an 'initialize workflow' prompt instead of crashing on 404.
  4. If a published workflow exists, restore it to draft via .../workflows/<id>/restore.

Example fix

// before
const envs = await get(`/rag/pipelines/${pid}/workflows/draft/environment-variables`)
// after
const draft = await get(`/rag/pipelines/${pid}/workflows/draft`).catch(() => null)
if (!draft) {
  await post(`/rag/pipelines/${pid}/workflows/draft`, { graph: initialGraph })
}
const envs = await get(`/rag/pipelines/${pid}/workflows/draft/environment-variables`)
Defensive patterns

Strategy: try-catch

Validate before calling

// ensure draft exists before fetching env vars
const draft = await fetch(`/rag/pipelines/${pid}/workflows/draft`).then(r => r.ok ? r.json() : null)
if (!draft) {
  await post(`/rag/pipelines/${pid}/workflows/draft`, { graph: starterGraph })
}
const envs = await fetch(`/rag/pipelines/${pid}/workflows/draft/environment-variables`).then(r => r.json())

Try / catch

try {
  return await get(`${base}/draft/environment-variables`)
} catch (e) {
  if (e.code === 'draft_workflow_not_exist') {
    // initialize draft then retry once
  } else throw e
}

Prevention

When it happens

Trigger: GET on environment-variables for a freshly-created pipeline whose draft has never been synced; for a pipeline whose draft workflow was deleted; before the first POST /workflows/draft has succeeded.

Common situations: Onboarding flow that loads the variables panel before the editor initializes the draft. Migration that left pipelines without drafts. UI deep-link directly to env vars on a new pipeline.

Related errors


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