bevyengine/bevy · error

Expected value to be of type {} but received {}

Error message

Expected value to be of type {} but received {}

What it means

Panic in ReflectSerializeWithRegistry's serialize closure (impl CreateTypeData): the dyn Reflect value could not be downcast to the concrete serializable type T — i.e. serialization type data was invoked with a value of the wrong concrete type (expected vs received paths in message). This indicates corrupted type data dispatch.

Source

Thrown at crates/bevy_reflect/src/serde/ser/serialize_with_registry.rs:80

    pub fn serialize<S>(
        &self,
        value: &dyn Reflect,
        serializer: S,
        registry: &TypeRegistry,
    ) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        ((self.serialize)(value, registry)).serialize(serializer)
    }
}

impl<T: Reflect + SerializeWithRegistry> CreateTypeData<T> for ReflectSerializeWithRegistry {
    fn create_type_data(_input: ()) -> Self {
        Self {
            serialize: |value: &dyn Reflect, registry| {
                let value = value.downcast_ref::<T>().unwrap_or_else(|| {
                    panic!(
                        "Expected value to be of type {} but received {}",
                        core::any::type_name::<T>(),
                        value.reflect_type_path()
                    )
                });
                Box::new(SerializableWithRegistry { value, registry })
            },
        }
    }
}

struct SerializableWithRegistry<'a, T: SerializeWithRegistry> {
    value: &'a T,
    registry: &'a TypeRegistry,
}

impl<'a, T: SerializeWithRegistry> Serialize for SerializableWithRegistry<'a, T> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>

View on GitHub (pinned to 396ca72708)

Solutions

  1. Serialize values of the type the serializer was created for
  2. Check the type registration/dispatch that selected this serializer
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at crates/bevy_reflect/src/serde/ser/serialize_with_registry.rs:80 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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