Skip to content

Fix SolaraViz reset crash on raw Solara components - #3494

Merged
quaquel merged 2 commits into
mesa:mainfrom
Tejasv-Singh:fix-raw-solara-components
Mar 30, 2026
Merged

quaquel merged 2 commits into
mesa:mainfrom
Tejasv-Singh:fix-raw-solara-components

Conversation

@Tejasv-Singh

Copy link
Copy Markdown
Contributor

Summary

Fixes a crash in SolaraViz when raw Solara components are passed in model_params instead of Mesa's wrapper classes. It adds early validation to raise a clear TypeError explaining how to correctly use the components.

Bug / Issue

Fixes #3453

When a raw Solara component (yielding a reacton.core.ValueElement) is passed in the model_params dictionary without Mesa's wrapper classes (e.g., solara.SliderInt() instead of mesa.visualization.Slider), it gets incorrectly treated as a "fixed" dictionary parameter.

During the reset process, the do_reset() function passes this raw UI Component object directly to the model's init kwargs. The model constructor expects a primitive type but gets a ValueElement instead, resulting in a crash with the error: TypeError: 'ValueElement' object cannot be interpreted as an integer.

Implementation

  • Added an early validation check in SolaraViz.__init__() (mesa/visualization/solara_viz.py) that iterates over model_params.
  • If any value passed is an instance of reacton.core.ValueElement, a clear TypeError is immediately raised directing the user to use wrappers from mesa.visualization like Slider, Checkbox, etc.

Testing

  • Added a unit text test_raw_solara_component_in_model_params in tests/visualization/test_solara_viz.py.
  • The test deliberately passes a raw solara.SliderInt() to model_params and asserts that the defensive TypeError is raised.

Additional Notes

This change creates a better Developer Experience (DX) by "failing fast" and explicitly educating the user, rather than failing deep inside the model instantiation step with a cryptic error.

Do let me know your thoughts.

@github-actions

github-actions Bot commented Mar 9, 2026

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +0.9% [+0.2%, +1.6%] 🔵 -0.5% [-0.6%, -0.3%]
BoltzmannWealth large 🔴 +4.2% [+3.3%, +5.1%] 🔵 +3.5% [-1.0%, +8.6%]
Schelling small 🔵 +1.2% [+0.7%, +1.6%] 🔵 -1.2% [-1.5%, -0.9%]
Schelling large 🔵 +1.0% [+0.0%, +1.9%] 🔵 -1.7% [-3.7%, +0.3%]
WolfSheep small 🔵 +1.2% [+0.7%, +1.6%] 🔵 -0.4% [-0.7%, -0.2%]
WolfSheep large 🔴 +5.6% [+4.6%, +6.5%] 🔴 +12.7% [+11.2%, +14.6%]
SugarscapeG1mt small 🔴 +6.6% [+6.1%, +7.0%] 🔵 +0.3% [+0.2%, +0.5%]
SugarscapeG1mt large 🔵 +2.1% [+0.8%, +3.2%] 🔵 -0.9% [-1.9%, +0.1%]
BoidFlockers small 🔵 -2.4% [-2.8%, -2.1%] 🔵 -1.1% [-1.3%, -1.0%]
BoidFlockers large 🔵 -2.6% [-3.0%, -2.2%] 🔵 -1.3% [-1.4%, -1.2%]

@quaquel quaquel added the bug Release notes label label Mar 10, 2026
@EwoutH
EwoutH requested a review from Sahil-Chhoker March 10, 2026 16:10
@Sahil-Chhoker

Copy link
Copy Markdown
Collaborator

@Tejasv-Singh Thanks for this PR. Guardrails are great, but in this case you're handling only ValueElement. I think it would be better to do it the other way around, allow the values defined in mesa to pass while blocking the unwanted ones.

@satharyasin-hub

Copy link
Copy Markdown
Contributor

Thanks for the implementation! I went through the PR and the approach of adding early validation in SolaraViz.__init__() makes sense for improving the developer experience.

One potential improvement could be to avoid checking specifically for ValueElement and instead validate against the supported Mesa wrapper classes (such as Slider, Checkbox, etc.). This could make the validation more future-proof if additional Solara component types appear.

Additionally, it might be useful to add a short note in the error message suggesting the correct wrapper usage (for example mesa.visualization.Slider instead of solara.SliderInt) so users can quickly fix the issue.

Overall the approach looks solid and the added test case is helpful in preventing regressions.

@Tejasv-Singh

Copy link
Copy Markdown
Contributor Author

Thank you so much @Sahil-Chhoker and @satharyasin-hub for the thoughtful feedback, that's a really good point.

You're right that is a much cleaner and more future-proof approach than specifically blocking ValueElement.

I'll update the implementation to validate against Mesa's supported param types (Slider, Checkbox, Choice, NumberInput, StaticText, and primitives like int, float, bool, str) and raise a clear TypeError with a helpful message if anything outside that set is passed. I'll also update the test accordingly.

Will push the updated commits shortly!

@Tejasv-Singh
Tejasv-Singh force-pushed the fix-raw-solara-components branch 5 times, most recently from 0122d46 to 6318e21 Compare March 19, 2026 09:13
@Tejasv-Singh
Tejasv-Singh force-pushed the fix-raw-solara-components branch from 6318e21 to 9ce952f Compare March 19, 2026 09:17
@Tejasv-Singh

Tejasv-Singh commented Mar 19, 2026 •

Copy link
Copy Markdown
Contributor Author

Hey @Sahil-Chhoker, I've pushed the updated implementation addressing all the feedback. Here's a summary of what changed:

Switched to a pure allowlist approach:

  • Replaced the ValueElement blocklist with _SUPPORTED_MODEL_PARAM_TYPES = (UserParam, dict, int, float, bool, str)
  • UserParam covers Slider and all future Mesa wrapper classes automatically, making it fully future-proof
  • No dependency on Solara internals anywhere in the validation logic

Tests updated:

  • test_raw_solara_component_in_model_params, confirms raw Solara components are rejected
  • test_unsupported_type_rejected, confirms the allowlist catches other unsupported types too (list, custom objects, None)
  • test_valid_model_params_types_accepted, confirms all valid Mesa types pass without error

Would love a re-review when you get a chance, thanks so much! 😄 🙌

@Sahil-Chhoker Sahil-Chhoker left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@Tejasv-Singh

Copy link
Copy Markdown
Contributor Author

Hey @EwoutH, the PR has been approved by @Sahil-Chhoker, all checks are passing, and there are no conflicts with the base branch. Could you take a look and merge when you get a chance? Also, would it be possible to remove the GSoC: Not ready label since the requested changes have been addressed? Thank you so much! 🙏

@quaquel
quaquel merged commit 06a3e8d into mesa:main Mar 30, 2026
11 checks passed
codebreaker32 pushed a commit to codebreaker32/mesa that referenced this pull request Apr 1, 2026
EwoutH pushed a commit that referenced this pull request Oct 28, 2026
Co-authored-by: Sahil Chhoker <[email protected]>
(cherry picked from commit 06a3e8d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

5 participants