langgenius/dify · error · ValueError

patched environment variable ids must be unique

Error message

patched environment variable ids must be unique

What it means

Raised by SyncEnvironmentVariablePatchPayload.validate_patch when the upsert list contains two variables with the same 'id' (api/controllers/console/app/workflow.py:129-130). The merge logic keys on id, so duplicate ids would silently overwrite one another; validation rejects the patch up front. Surfaced as a Pydantic ValidationError (400).

Source

Thrown at api/controllers/console/app/workflow.py:130

    value_type: str
    id: NotRequired[str]
    name: NotRequired[str]
    value: NotRequired[Any]
    description: NotRequired[str | None]


class SyncEnvironmentVariablePatchPayload(BaseModel):
    environment_variables: list[dict[str, Any]] = Field(default_factory=list)
    deleted_environment_variable_ids: list[str] = Field(default_factory=list)

    @model_validator(mode="after")
    def validate_patch(self) -> Self:
        """Require stable, disjoint IDs so the service can merge the patch deterministically."""
        upsert_ids = [variable.get("id") for variable in self.environment_variables]
        if any(not isinstance(variable_id, str) or not variable_id for variable_id in upsert_ids):
            raise ValueError("patched environment variables require an id")
        if len(set(upsert_ids)) != len(upsert_ids):
            raise ValueError("patched environment variable ids must be unique")
        if any(not variable_id for variable_id in self.deleted_environment_variable_ids):
            raise ValueError("deleted environment variable ids must not be empty")
        if len(set(self.deleted_environment_variable_ids)) != len(self.deleted_environment_variable_ids):
            raise ValueError("deleted environment variable ids must be unique")
        if set(upsert_ids).intersection(self.deleted_environment_variable_ids):
            raise ValueError("an environment variable cannot be upserted and deleted in the same patch")
        return self


class SyncDraftWorkflowPayload(BaseModel):
    model_config = ConfigDict(extra="forbid")

    graph: dict[str, Any]
    features: dict[str, Any]
    hash: str | None = None
    is_collaborative: bool = Field(default=False, alias="_is_collaborative")
    environment_variable_patch: SyncEnvironmentVariablePatchPayload | None = None
    conversation_variables: list[dict[str, Any]] = Field(

View on GitHub (pinned to ef8544b173)

Solutions

  1. Ensure each upserted variable has a unique id — deduplicate the list before sending.
  2. If editing one variable, include it once; do not re-send the same id twice.
  3. Add a client-side uniqueness check on ids before submit.

Example fix

// before: duplicate id
[{id:'a', name:'X', value:'1'}, {id:'a', name:'Y', value:'2'}]
// after: unique ids
[{id:crypto.randomUUID(), name:'X', value:'1'}, {id:crypto.randomUUID(), name:'Y', value:'2'}]
Defensive patterns

Strategy: validation

Validate before calling

// Deduplicate upsert ids before submit
const seen = new Set()
payload.environment_variables = payload.environment_variables.filter(v =>
  seen.has(v.id) ? false : (seen.add(v.id), true))

Type guard

function uniqueIds(vars) {
  const ids = vars.map(v => v.id)
  return new Set(ids).size === ids.length
}

Prevention

When it happens

Trigger: Two entries in environment_variable_patch.environment_variables share the same 'id' value. Common when copy-pasting variable definitions or when a front-end bug double-adds an entry.

Common situations: Copy-paste of a variable block without regenerating the id; front-end state desync adding the same variable twice; import payload with colliding ids; test fixture reuse without id rotation.

Related errors


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