tracel-ai/burn · error

Cannot add single item when attributes are set.

Error message

Cannot add single item when attributes are set.

What it means

Mirror guard of the `add`-side check: `add_single` panics when named attributes were already added via `add`, because a Content serializes either a named-attribute list or one single item. The panic reports a caller bug in composing display content for a module, not a runtime data problem.

Source

Thrown at crates/burn-core/src/module/display.rs:469

            name: name.to_owned(),
            value: DisplayAdapter(value).format(self.display_settings.clone()),
            ty: any::type_name::<T>().to_string(),
        });
        self
    }

    /// Adds a single item.
    ///
    /// # Arguments
    ///
    /// * `value` - Rendered string of the single item.
    ///
    /// # Returns
    ///
    /// Updated `Content` instance with the single item added.
    pub fn add_single<T: ModuleDisplay + ?Sized>(mut self, value: &T) -> Self {
        if !self.attributes.is_empty() {
            panic!("Cannot add single item when attributes are set.");
        }

        self.single_item = Some(value.format(self.display_settings.clone()));

        self
    }

    /// Adds a single item.
    ///
    /// # Arguments
    ///
    /// * `value` - Formatted display value.
    ///
    /// # Returns
    ///
    /// Updated `Content` instance with the formatted single item added.
    pub fn add_formatted<T: Display>(mut self, value: &T) -> Self {
        if !self.attributes.is_empty() {

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Call `add_single` on a fresh Content before any `add` calls, or drop the `add` calls if a single item is intended.
  2. Convert the single item into a named attribute (`add("item", value)`) when both kinds of content are needed.
  3. Separate the content into two Content instances with different display roles.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/burn-core/src/module/display.rs:469 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05). Data as JSON: /api/errors/03b6a064423d3b1c. Report an issue: GitHub.