BoundaryML/baml · error · anyhow::Error
Failed to find in union
Error message
Failed to find {type_to_find} in union What it means
selected_type_index scans the options of a union TypeGeneric looking for an entry equal to type_to_find. If none of the union's options match, it returns this error. It means a lookup assumed a given type is one of the union's members but it is not.
Solutions
- Print the union's options and confirm type_to_find exactly matches one of them (including modifiers/metadata).
- Regenerate/refresh code after schema changes so lookups use current variants.
- Handle the Err case by falling back to a search by name or by reporting a schema mismatch instead of assuming membership.
Example fix
// before
let idx = union.selected_type_index(&expected)?;
// after
let idx = match union.selected_type_index(&expected) {
Ok(r) => r.index,
Err(_) => anyhow::bail!("type {:?} is not a member of union {:?}", expected, union),
}; Defensive patterns
Strategy: validation
Validate before calling
if !union.options().iter().any(|t| *t == type_to_find) { return Err(anyhow!("type not a union member")); } Type guard
fn in_union(union: &TypeGeneric<Meta>, t: &TypeGeneric<Meta>) -> bool { union.options().iter().any(|o| *o == *t) } Try / catch
match union.selected_type_index(&expected) { Ok(r) => r.index, Err(_) => fallback_lookup(&expected) } Prevention
- Regenerate clients after schema changes
- Compare types including modifiers/metadata
- Handle union membership failure explicitly rather than unwrapping
When it happens
Trigger: Calling selected_type_index with a type_to_find that is not one of the union's variants (e.g. after a schema edit removed/changed that variant, or the types differ by generic metadata parameters).
Common situations: Streaming/partial-response code mapping a runtime-selected type back to its union index after the BAML schema was changed; comparing types that differ by alias or modifiers so equality fails.
Understand the failure class
Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.
Related errors
AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12).
Data as JSON: /api/errors/dca5d9ebcf723f97.
Report an issue: GitHub.
Appendix: source
Thrown at engine/baml-lib/baml-types/src/ir_type/mod.rs:490
pub fn selected_type_index(
&self,
type_to_find: &TypeGeneric<T>,
lookup: &impl TypeLookupsMeta<T>,
) -> Result<SelectedTypeIndexResult<'_, T>, anyhow::Error> {
let options = self.iter_include_null();
for (i, t) in options.iter().enumerate() {
if match t {
TypeGeneric::RecursiveTypeAlias { name, .. } => {
&TypeLookupsMeta::expand_recursive_type(lookup, name.as_str())? == type_to_find
}
_ => *t == type_to_find,
} {
return Ok(SelectedTypeIndexResult { index: i, options });
}
}
Err(anyhow::anyhow!("Failed to find {type_to_find} in union"))
}
}
pub trait HasType<T> {
fn field_type(&self) -> &TypeGeneric<T>;
}
impl HasType<type_meta::IR> for TypeIR {
fn field_type(&self) -> &TypeIR {
self
}
}
impl HasType<type_meta::NonStreaming> for TypeNonStreaming {
fn field_type(&self) -> &TypeGeneric<type_meta::NonStreaming> {
self
}
}View on GitHub (pinned to bd85ce9dee)