bevyengine/bevy · error

Attempted to insert invalid value of type {}.

Error message

Attempted to insert invalid value of type {}.

What it means

The impl_list!-family macro implements List::insert(index, Box<dyn PartialReflect>) for reflected list types (e.g. arrays, VecDeque-like containers). The element is recovered with value.try_take::<T>() and, if that fails, a second chance is given via T::from_reflect(&*value) so dynamic values can convert. Only when both paths fail does this panic fire, printing the value's type path.

Source

Thrown at crates/bevy_reflect/src/impls/macros/list.rs:18

macro_rules! impl_reflect_for_veclike {
    ($ty:ty, $insert:expr, $remove:expr, $push:expr, $pop:expr, $sub:ty) => {
        const _: () = {
            impl<T: $crate::from_reflect::FromReflect + $crate::info::MaybeTyped + $crate::type_path::TypePath + $crate::type_registry::GetTypeRegistration> $crate::list::List for $ty {
                #[inline]
                fn get(&self, index: usize) -> Option<&dyn $crate::reflect::PartialReflect> {
                    <$sub>::get(self, index).map(|value| value as &dyn $crate::reflect::PartialReflect)
                }

                #[inline]
                fn get_mut(&mut self, index: usize) -> Option<&mut dyn $crate::reflect::PartialReflect> {
                    <$sub>::get_mut(self, index).map(|value| value as &mut dyn $crate::reflect::PartialReflect)
                }

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

                fn remove(&mut self, index: usize) -> bevy_platform::prelude::Box<dyn $crate::reflect::PartialReflect> {
                    bevy_platform::prelude::Box::new($remove(self, index))
                }

                fn push(&mut self, value: bevy_platform::prelude::Box<dyn $crate::reflect::PartialReflect>) {
                    let value = T::take_from_reflect(value).unwrap_or_else(|value| {
                        panic!(
                            "Attempted to push invalid value of type {}.",
                            value.reflect_type_path()
                        )

View on GitHub (pinned to 396ca72708)

Solutions

  1. Make the inserted value's type equal the list's element type T (log reflect_type_path() on the boxed value to compare).
  2. Convert dynamic values first: T::from_reflect(&*value) and insert only on Some.
  3. Prefer typed access: downcast to &mut Vec<T> (or the concrete list) and call the ordinary insert.
  4. If the value genuinely has another type, convert it with TryFrom/From or bevy's reflect conversion utilities before boxing it.

Example fix

// before
list.insert(0, Box::new(2_u8)); // Vec<f32> -> panics

// after
list.insert(0, Box::new(2.0_f32));
Defensive patterns

Strategy: type-guard

Validate before calling

if list.get_represented_type_info().map(|i| i.kind() == bevy_reflect::ReflectKind::List).unwrap_or(false) { /* then insert is the only remaining risk: check the element */ }

Type guard

use bevy_reflect::{FromReflect, PartialReflect, List};

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

// guard every insert:
assert!(element_ok::<f32>(boxed.as_ref()), "bad element for this list");

Prevention

When it happens

Trigger: Calling List::insert on a reflected list whose element type is T with a boxed value that is neither T nor FromReflect-convertible — e.g. list.insert(0, Box::new(1u8)) on a Vec<f32>, or inserting a DynamicUsize-like dynamic value into a Vec<String>.

Common situations: Scene deserialization after an element type changed; editor/inspector inserting property values chosen by users; generic code that moves elements between different lists via PartialReflect without matching element types.

Related errors


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