bevyengine/bevy · error

Attempted to insert invalid value of type {}.

Error message

Attempted to insert invalid value of type {}.

What it means

bevy_reflect implements List for smallvec::SmallVec<T> where T: SmallArray. List::insert(index, Box<dyn PartialReflect>) recovers the element with value.try_take::<T::Item>() and, failing that, <T as SmallArray>::Item::from_reflect(&*value). If neither an owning downcast nor a FromReflect conversion succeeds, this panic fires with the value's type path.

Source

Thrown at crates/bevy_reflect/src/impls/smallvec.rs:37

        if index < SmallVec::len(self) {
            Some(&self[index] as &dyn PartialReflect)
        } else {
            None
        }
    }

    fn get_mut(&mut self, index: usize) -> Option<&mut dyn PartialReflect> {
        if index < SmallVec::len(self) {
            Some(&mut self[index] as &mut dyn PartialReflect)
        } else {
            None
        }
    }

    fn insert(&mut self, index: usize, value: Box<dyn PartialReflect>) {
        let value = value.try_take::<T::Item>().unwrap_or_else(|value| {
            <T as SmallArray>::Item::from_reflect(&*value).unwrap_or_else(|| {
                panic!(
                    "Attempted to insert invalid value of type {}.",
                    value.reflect_type_path()
                )
            })
        });
        SmallVec::insert(self, index, value);
    }

    fn remove(&mut self, index: usize) -> Box<dyn PartialReflect> {
        Box::new(self.remove(index))
    }

    fn push(&mut self, value: Box<dyn PartialReflect>) {
        let value = value.try_take::<T::Item>().unwrap_or_else(|value| {
            <T as SmallArray>::Item::from_reflect(&*value).unwrap_or_else(|| {
                panic!(
                    "Attempted to push invalid value of type {}.",
                    value.reflect_type_path()

View on GitHub (pinned to 396ca72708)

Solutions

  1. Insert values whose type equals the SmallVec's Item type (log reflect_type_path() to confirm).
  2. Convert dynamic/wrong-typed values explicitly with <T as SmallArray>::Item::from_reflect(&*value) before inserting.
  3. Downcast to &mut SmallVec<T> and use the typed SmallVec::insert.
  4. Check the value against the registered ListInfo/TypeInfo for the SmallVec before inserting.

Example fix

// before
sv.insert(0, Box::new(9_u8)); // SmallVec<[f32; 4]> -> panics

// after
sv.insert(0, Box::new(9.0_f32));
Defensive patterns

Strategy: type-guard

Type guard

use bevy_reflect::{FromReflect, PartialReflect};

fn item_ok<I: FromReflect>(value: &dyn PartialReflect) -> bool {
    value.try_downcast_ref::<I>().is_some() || I::from_reflect(value).is_some()
}

// before List::insert on a SmallVec<[f32; 4]>:
if item_ok::<f32>(boxed.as_ref()) {
    sv.insert(0, boxed);
}

Prevention

When it happens

Trigger: Calling List::insert on a reflected SmallVec with an element that is not the array's Item type and cannot convert, e.g. smallvec.insert(0, Box::new('c')) on a SmallVec<[f32; 8]>.

Common situations: Component fields declared as SmallVec (positions, ids, small buffers) being patched via scenes or editors; generic reflection code that does not special-case SmallVec element types; version drift between serialized element types and current Item.

Related errors


AI-assisted analysis of bevyengine/bevy@396ca72708 (2026-08-20). Data as JSON: /api/errors/eba707d7e56b71b6. Report an issue: GitHub.