mesa icon indicating copy to clipboard operation
mesa copied to clipboard

SolaraViz DX Improvements: Reset bug, Class Instantiation errors, and Warning spam

Open Tejasv-Singh opened this issue 8 months ago • 3 comments

SolaraViz Reset button crashes when model_params contains raw Solara components

Bug Summary

Clicking the "Reset" button in SolaraViz causes a crash with TypeError: 'ValueElement' object cannot be interpreted as an integer when model_params contains raw Solara components (like solara.SliderInt()) instead of Mesa's wrapper classes.

Root Cause

In mesa/visualization/solara_viz.py, the do_reset() function attempts to re-instantiate the model by extracting user parameters. When a raw Solara component (yielding a reacton.core.ValueElement) is passed in the model_params dictionary without Mesa's Slider wrapper, the functions split_model_params() and check_param_is_fixed() incorrectly treat it as a "fixed" dictionary parameter.

During reset, do_reset() then passes this raw UI Component object directly to the model's __init__ kwargs instead of extracting its .value, causing the crash when the model constructor expects a primitive type (int, float, etc.).

Steps to Reproduce

from mesa import Model
from mesa.visualization import SolaraViz
import solara

class MyModel(Model):
    def __init__(self, num_agents):
        super().__init__()
        self.num_agents = num_agents  # Expects an integer

# Problematic: passing raw Solara component
model_params = {
    "num_agents": solara.SliderInt(value=10, min=1, max=100)  # Should use mesa.visualization.Slider
}

model = MyModel(10)
page = SolaraViz(model, components=[], model_params=model_params)
# Initial load works fine, but clicking "Reset" button crashes

Expected Behavior

One of the following:

  1. Defensive extraction: do_reset() should detect ValueElement objects and extract their .value property before passing to the model constructor
  2. Early validation: Raise a clear error during SolaraViz initialization if model_params contains unsupported types, with a helpful message directing users to use Mesa's Slider, Checkbox, etc. wrapper classes

Actual Behavior

TypeError: 'ValueElement' object cannot be interpreted as an integer

The error occurs deep in the model instantiation, making it unclear that the issue is with the parameter extraction logic.

Environment

  • Mesa version: 4.0.0.dev0
  • Python version: 3.14.3

Proposed Fix

I'm willing to submit a PR to fix this. The fix would involve:

  • Adding type checking in split_model_params()/do_reset() to extract .value from ValueElement objects
  • OR adding validation in SolaraViz.__init__() to reject raw Solara components with a clear error message

Which approach would the maintainers prefer?


Note: This is part 1 of a series of SolaraViz DX improvements.

Tejasv-Singh avatar Mar 06 '26 06:03 Tejasv-Singh

@Tejasv-Singh can i look into this are u going to open a pr

Rishav23av avatar Mar 06 '26 16:03 Rishav23av

@Rishav23av Thanks for offering to help out! I had already finished working on a fix for this back then and have now opened a PR for the same here: #3494.

However, I would genuinely love if you could review the PR and tell me what all could be improved. 🙌

Tejasv-Singh avatar Mar 09 '26 23:03 Tejasv-Singh

Hi! I’m interested in working on this issue. If no one is currently assigned, I’d be happy to investigate and submit a PR.

Please let me know if that would be helpful.

satharyasin-hub avatar Mar 14 '26 03:03 satharyasin-hub