BoundaryML/baml · error · Error

Enum ${name} already exists

Error message

Enum ${name} already exists

What it means

TypeBuilder.addClass(name) throws when the name is already taken by an enum registered on the same TypeBuilder, keeping class and enum namespaces disjoint. Duplicate or shadowing names would make generated BAML ambiguous.

Source

Thrown at engine/language_client_typescript/type_builder.js:72

    }
    classViewer(name, properties) {
        return new ClassViewer(this.tb, name, new Set(properties));
    }
    classBuilder(name, properties) {
        return new ClassBuilder(this.tb, name, new Set(properties));
    }
    enumViewer(name, values) {
        return new EnumViewer(this.tb, name, new Set(values));
    }
    enumBuilder(name, values) {
        return new EnumBuilder(this.tb, name, new Set(values));
    }
    addClass(name) {
        if (this.classes.has(name)) {
            throw new Error(`Class ${name} already exists`);
        }
        if (this.enums.has(name)) {
            throw new Error(`Enum ${name} already exists`);
        }
        this.classes.add(name);
        return new ClassBuilder(this.tb, name);
    }
    addEnum(name) {
        if (this.classes.has(name)) {
            throw new Error(`Class ${name} already exists`);
        }
        if (this.enums.has(name)) {
            throw new Error(`Enum ${name} already exists`);
        }
        this.enums.add(name);
        return new EnumBuilder(this.tb, name);
    }
    addBaml(baml) {
        this.tb.addBaml(baml, this.runtime);
    }
}

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Rename the class or the enum so names are unique within the TypeBuilder
  2. Track registered names in one Set covering both classes and enums before registering
  3. Check with your own registry before calling addClass if the name may collide with enums
  4. Wrap in try/catch and choose a suffixed name on collision

Example fix

// before
tb.addEnum("Status", ["A", "B"]);
tb.addClass("Status"); // throws
// after
tb.addEnum("Status", ["A", "B"]);
tb.addClass("StatusClass");
Defensive patterns

Strategy: validation

Validate before calling

if (enumNames.has(name)) throw new Error(`'${name}' is already an enum`);

Type guard

null

Try / catch

try { tb.addClass(name); } catch (e) {
  if (String(e.message).includes('already exists')) name = name + 'Class';
  else throw e;
}

Prevention

When it happens

Trigger: Calling addClass with a name previously passed to addEnum on the same builder; mixing dynamic class creation with a fixed enum list that shares names.

Common situations: Schema-generation code where enums and classes derive from the same source list of names; migrating code from string-typed enums to classes (or vice versa) without renaming.

Understand the failure class

Background: "already exists" / EEXIST / FileAlreadyExistsException: what the 'file already exists' error means and how to fix it — this error's family across 37 libraries.

Related errors


AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12). Data as JSON: /api/errors/cdf656cac596a3c0. Report an issue: GitHub.