langflow-ai/langflow · error · HTTPException

Invalid target folder

Error message

Invalid target folder

What it means

Raised when target_folder_id is supplied but the Folder row does not exist, or exists with a different user_id than the caller. Folder ownership is verified before placing the templated flow; unlike the flow lookup this returns 400 (bad request) rather than 404. Note the strict comparison folder.user_id != user_id — types must compare equal.

Source

Thrown at src/backend/base/langflow/agentic/utils/template_create.py:54

        session: Active async DB session.
        user_id: The owner user id for the new flow.
        template_id: The string id field inside the starter template JSON.
        target_folder_id: Optional folder id to place the flow. If not provided,
            the user's default folder will be used.

    Returns:
        Dict with keys: {"id": str, "link": str}
    """
    # 1) Load template JSON from starter_projects
    template = get_template_by_id(template_id=template_id, fields=None)
    if not template:
        raise HTTPException(status_code=404, detail="Template not found")

    # 2) Resolve target folder
    if target_folder_id:
        folder = await session.get(Folder, target_folder_id)
        if not folder or folder.user_id != user_id:
            raise HTTPException(status_code=400, detail="Invalid target folder")
        folder_id = folder.id
    else:
        default_folder = await get_or_create_default_folder(session, user_id)
        folder_id = default_folder.id

    # 3) Build FlowCreate from template fields (ignore unknowns)
    new_flow = FlowCreate(
        name=template.get("name"),
        description=template.get("description"),
        icon=template.get("icon"),
        icon_bg_color=template.get("icon_bg_color"),
        gradient=template.get("gradient"),
        data=template.get("data"),
        is_component=template.get("is_component", False),
        endpoint_name=template.get("endpoint_name"),
        tags=template.get("tags"),
        mcp_enabled=template.get("mcp_enabled"),
        folder_id=folder_id,

View on GitHub (pinned to 976ec789d2)

Solutions

  1. Omit target_folder_id — the flow goes to your default folder automatically.
  2. Or list your folders first (GET /api/v1/folders/) and pass an id you own from that response.

Example fix

# before
create_flow_from_template(template_id=tid, target_folder_id="<foreign id>")

# after
create_flow_from_template(template_id=tid)  # defaults to caller's default folder
Defensive patterns

Strategy: validation

Validate before calling

folders = (await client.get("/api/v1/folders/")).json()
owned = {f["id"] for f in folders if f.get("user_id") == my_user_id}
if target_folder_id and target_folder_id not in owned:
    target_folder_id = None  # fall back to default folder

Try / catch

try:
    create_flow_from_template(template_id=tid, target_folder_id=fid)
except HTTPError as e:
    if e.response.status_code == 400 and "target folder" in e.response.text:
        create_flow_from_template(template_id=tid)  # default folder
    else:
        raise

Prevention

When it happens

Trigger: Creating a flow from a template with target_folder_id set to a deleted folder, another user's folder, or a non-UUID value that fails session.get resolution.

Common situations: Folder deleted from another tab/client while its id was cached; team workspaces where a folder belongs to a colleague; stale folder ids persisted by an integration after re-login created new user records.

Related errors


AI-assisted analysis of langflow-ai/langflow@976ec789d2 (2026-08-14). Data as JSON: /api/errors/c8f0f4129f1350cf. Report an issue: GitHub.