Skip to content

fix: migrate codebase to Mesa 4.x API (resolves mesa.space removal)#153

Open
abhinavk0220 wants to merge 8 commits intomesa:mainfrom
abhinavk0220:fix/mesa4-compatibility
Open

fix: migrate codebase to Mesa 4.x API (resolves mesa.space removal)#153
abhinavk0220 wants to merge 8 commits intomesa:mainfrom
abhinavk0220:fix/mesa4-compatibility

Conversation

@abhinavk0220
Copy link
Copy Markdown

@abhinavk0220 abhinavk0220 commented Mar 5, 2026

Closes #152

Summary

Mesa 4.x removed mesa.space entirely. This PR migrates the full codebase to the new Mesa 4.x APIs so that mesa-llm works with the current version of Mesa.

Changes

  • Replace MultiGrid/SingleGrid with OrthogonalMooreGrid from mesa.discrete_space
  • Replace old ContinuousSpace(x_max, y_max) with new dimensions-based API
  • Fix agent placement to use cell-based API (agent.cell = grid._cells[pos])
  • Fix neighbor lookup using get_neighborhood() + coordinate set matching
  • Fix model.stepsmodel.step (renamed in Mesa 4.x)
  • Fix Model.__init__(seed=...) kwarg incompatibility with mesa_signals

Testing

All 203 tests passing after migration:

Notes

Tested against Mesa 4.x dev installed from source.


GSoC contributor checklist

Context & motivation

Mesa 4.x removed mesa.space entirely with no deprecation warning
any project depending on it simply breaks at import time. I discovered
this while trying to run mesa-llm examples and getting immediate
ImportError. This PR is the full migration of the codebase to the
new Mesa 4.x spatial API.

What I learned

Mesa 4.x's new discrete space architecture is fundamentally different
from the old API not just renamed imports but a completely different
cell-based model where agents live in cells rather than coordinates.
Understanding this required reading Mesa's source code directly, not
just the migration guide. The most subtle change was the coordinate
convention difference between OrthogonalMooreGrid._cells (x,y) and
the internal connections dict (row,col) a distinction that caused
agents to move in the wrong direction and required a separate fix in
PR #195.

Learning repo

🔗 My learning repo: https://github.com/abhinavk0220/GSoC-learning-space
🔗 Relevant model(s): https://github.com/abhinavk0220/GSoC-learning-space/tree/main/models

Readiness checks

  • This PR addresses an agreed-upon problem (linked issue or discussion with maintainer approval), or is a small/trivial fix
  • I have read the contributing guide and deprecation policy
  • I have performed a self-review: I reviewed my own PR as if I were a reviewer and left comments on anything that needs explanation
  • Another GSoC contributor has reviewed this PR:
  • Tests pass locally (`pytest --cov=mesa tests/)
  • Code is formatted (ruff check . --fix)
  • If applicable: documentation, examples, and/or migration guide are updated

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 5, 2026

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 53104782-8bd4-4f75-afc9-c8102aea04d1

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Tip

Flake8 can be used to improve the quality of Python code reviews.

Flake8 is a Python linter that wraps PyFlakes, pycodestyle and Ned Batchelder's McCabe script.

To configure Flake8, add a '.flake8' or 'setup.cfg' file to your project root.

See Flake8 Documentation for more details.

abhinavk0220 pushed a commit to abhinavk0220/mesa-examples that referenced this pull request Mar 6, 2026
…Schelling's (1971) classic segregation model using LLM agentsinstead of a fixed tolerance threshold.Each agent reasons in natural language about its neighborhood compositionand decides whether to stay ('happy') or relocate ('unhappy'). Thisproduces richer segregation dynamics than the classical threshold rule.Includes:- SchellingAgent extending LLMAgent with CoT reasoning- LLMSchellingModel on OrthogonalMooreGrid with torus=True- Segregation index metric tracked over time- SolaraViz with grid plot, happiness chart, and segregation index- README with comparison table vs classical Schelling modelReference: Schelling, T.C. (1971). Dynamic models of segregation.Journal of Mathematical Sociology, 1(2), 143-186.Related: mesa/mesa-llm#153
@Jay-Lokhande
Copy link
Copy Markdown

Hey this looks cool,
actually i have cloned your branch but not getting how did you tested against Mesa 4.x would please help me out for this?

@colinfrisch
Copy link
Copy Markdown
Member

I'm not fully up to date with the latest mesa 4.x changes... @wang-boyu what do you think about this ?

@abhinavk0220
Copy link
Copy Markdown
Author

Hey Jay! To test against Mesa 4.x, install Mesa directly from
source:

git clone https://github.com/mesa/mesa.git
cd mesa
pip install -e .

Then install mesa-llm dependencies:

cd ../mesa-llm
pip install -e .

Then run the test suite:

python -m pytest tests/ -q

The key Mesa 4.x changes that affect mesa-llm are:

  • mesa.space is removed → use mesa.discrete_space and
    mesa.experimental.continuous_space
  • Model(seed=42) constructor changed
  • model._time replaces model.steps as step counter

Happy to help if you run into issues! 🙂

@abhinavk0220
Copy link
Copy Markdown
Author

abhinavk0220 commented Mar 10, 2026

Hi Colin! Happy to provide more context for wang-boyu's review.

The key Mesa 4.x breaking changes this PR addresses:

  1. mesa.space removed entirely → migrated to mesa.discrete_space
    (OrthogonalMooreGrid) and mesa.experimental.continuous_space

  2. ContinuousSpace API changed → old positional args replaced
    with dimensions=[[0,x],[0,y]] keyword API

  3. model.steps → model._time as the internal step counter

  4. Model(seed=42) constructor incompatibility with mesa_signals

All 203 tests passing after migration. Happy to answer any
specific questions wang-boyu has!

@Jay-Lokhande
Copy link
Copy Markdown

I was trying to understand changes you made I am not getting that 'hell' named file you have created or might generated.

OrthogonalMooreGrid,
OrthogonalVonNeumannGrid,
)
from mesa.space import (
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Replaced mesa.space imports with mesa.discrete_space and
mesa.experimental.continuous_space throughout. The ContinuousSpace
API also changed old positional args (x_max, y_max) replaced with
dimensions=[[0,x],[0,y]] keyword API. Agent placement migrated from
grid.place_agent() to cell-based API: cell.add_agent(agent) +
agent.cell = cell.

@AdityaChauhanX07
Copy link
Copy Markdown

A few things worth noting for the review:

  1. This was tested against Mesa 4.x from source, not the official pre-release (pip install --pre mesa). Worth verifying against the pre-release since that's what most users will actually install.

  2. There are merge conflicts that will need resolving before this lands.

abhinavKumar0206 and others added 5 commits March 18, 2026 23:04
…rely, breaking the full test suite.This commit migrates all affected files to the new APIs.Changes:- Replace MultiGrid/SingleGrid imports with OrthogonalMooreGrid from mesa.discrete_space- Replace old ContinuousSpace(x_max, y_max) with new dimensions-based API- Fix agent placement to use cell-based API (agent.cell = grid._cells[pos])- Fix neighbor lookup using get_neighborhood + coordinate set matching- Fix model.steps -> model.step (renamed in Mesa 4.x)- Remove seed= kwarg from Model.__init__ calls (mesa_signals compat fix)- Fix record_model.py self.steps -> self.stepAll 203 tests now passing.
@abhinavk0220 abhinavk0220 force-pushed the fix/mesa4-compatibility branch from 3bdc711 to 97e24b5 Compare March 18, 2026 17:35
@abhinavk0220
Copy link
Copy Markdown
Author

Thanks for the review, @AdityaChauhanX07!

On testing against the pre-release:
Good catch. The tests were run against Mesa 4.x installed from source (pip install -e .). I'll verify against the pip pre-release (pip install --pre mesa) as well — the APIs should be identical but it's worth confirming. If any divergence exists I'll update the PR accordingly.

On merge conflicts:
Agreed, there are conflicts that accumulated while other PRs landed on main. I'll rebase against the current main and resolve them before requesting a final review.

Appreciate you flagging both issues — this kind of review feedback is exactly what helps get a PR merge-ready.

abhinavKumar0206 and others added 2 commits March 21, 2026 14:54
…conflicts

- Remove Mesa 3.x SingleGrid/MultiGrid code path from move_one_step()
  since those classes don't exist in Mesa 4.x; keep only OrthogonalMooreGrid
  and ContinuousSpace paths
- Fix unresolved merge conflict in test_llm_agent.py (HEAD vs pre-commit.ci)
- Replace MultiGrid/SingleGrid with OrthogonalMooreGrid throughout all tests
- Fix corrupted merged lines in test_llm_agent.py and conftest.py
- Update ContinuousSpace tests to use Mesa 4.x API (dimensions= instead of x_max=)
- Fix corrupted import line in test_inbuilt_tools.py (mesa.space removed in 4.x)
- Remove Mesa 3.x-only test cases (SingleGrid/MultiGrid boundary tests)
- Remove grid setup from vision=-1 tests (uses all model.agents, no grid needed)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: codebase incompatible with Mesa 4.x (mesa.space removed)

5 participants