{"record":{"id":"515dcbd34d29afbd","repo":"BoundaryML/baml","slug":"could-not-add-a-type-to-the-database-as-the-name-0-is","errorCode":null,"errorMessage":"Could not add a type to the database as the name `{0}` is already present","messagePattern":"Could not add a type to the database as the name `(.+?)` is already present","errorType":"error_code","errorClass":"ConvertError","httpStatus":null,"severity":"error","filePath":"baml_language/crates/bex_sap/src/sap_model/convert.rs","lineNumber":40,"sourceCode":"#[derive(thiserror::Error, Debug)]\npub enum ConvertError {\n    #[error(\"Failed to parse float: {0}\")]\n    ParseFloat(#[from] std::num::ParseFloatError),\n    #[error(\"Unknown media kind\")]\n    UnknownMediaKind,\n    #[error(\"Float literals cannot be parsed\")]\n    FloatLiteral,\n    #[error(\"Non-parsable type: {0:?}\")]\n    NonParsableType(Box<SapTy>),\n    #[error(\"Unknown class: {0}\")]\n    UnknownClass(DefKey),\n    #[error(\"Unknown enum: {0}\")]\n    UnknownEnum(DefKey),\n    #[error(\"Unknown type alias: {0}\")]\n    UnknownTypeAlias(DefKey),\n    #[error(\"Unknown name (could not determine if it was a class, enum, or type alias): {0}\")]\n    UnknownName(DefKey),\n    #[error(\"Could not add a type to the database as the name `{0}` is already present\")]\n    AlreadyPresent(DefKey),\n    #[error(\"Recursion depth exceeded for {0}\")]\n    RecursionDepthExceeded(&'static str),\n    #[error(\"Unions must be flattened\")]\n    UnflattenedUnion,\n    /// Something like `type A = B; type B = A;` is invalid.\n    #[error(\"Recursive type alias without indirection: {0}\")]\n    DirectRecursiveTypeAlias(DefKey),\n    #[error(\"Internal error (please report): {0}\")]\n    InternalError(&'static str),\n}\n\nconst MAX_RECURSION_DEPTH: usize = 16;\n\n/// Contains stuff from [`sys_types::SysOpContext`] that we need for converting to the sap model.\n///\n/// ## Representation\n/// - Unions should be flattened:","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/baml_language/crates/bex_sap/src/sap_model/convert.rs#L22-L58","documentation":"ConvertError::AlreadyPresent(DefKey) is raised in TypeCtx::build_db when TypeRefDb::try_add_inner rejects an insert because a type with the same DefKey was already added. Since classes, enums, and aliases are iterated from distinct maps, this indicates a duplicate name collision in the type database.","triggerScenarios":"Calling build_db when two definitions share the same DefKey — e.g. a class and an alias (or two classes) registered under identical keys in the SysOpContext.","commonSituations":"Duplicate `class Foo` / `type Foo` declarations across loaded modules; merging two contexts that both define the same type name; generated code re-declaring a type that also exists in sources.","solutions":["Search your BAML sources for a second declaration of the name in the error and remove or rename one.","Check for the same type name defined in multiple included modules and disambiguate with module-qualified names.","If contexts are merged programmatically, deduplicate definition maps by DefKey before constructing TypeCtx."],"exampleFix":"// before\nclass User { id: int }\ntype User = string // collision\n// after\nclass User { id: int }\ntype UserName = string","handlingStrategy":"validation","validationCode":"// Detect duplicate DefKeys across class, enum, and alias maps before build_db\nfn assert_no_duplicates(ctx: &TypeCtx) -> Result<(), String> {\n    let mut seen = std::collections::HashSet::new();\n    for k in ctx.class_definitions.keys()\n        .chain(ctx.enum_definitions.keys())\n        .chain(ctx.type_alias_definitions.keys()) {\n        if !seen.insert(k.clone()) {\n            return Err(format!(\"duplicate type name: {k:?}\"));\n        }\n    }\n    Ok(())\n}","typeGuard":null,"tryCatchPattern":"match result {\n    Err(ConvertError::AlreadyPresent(key)) => eprintln!(\"remove the duplicate declaration of {key:?}\"),\n    other => other,\n}","preventionTips":["Enforce unique type names across all included modules.","Deduplicate merged context maps by DefKey before constructing TypeCtx.","Never mix generated and hand-written definitions of the same name."],"tags":["sap","baml","duplicate-definition","name-collision"],"backgroundTag":"file-already-exists","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}