All jobs

Fix "AttributeError: 'Graph' object has no attribute 'node'" in NetworkX

Yes. An agent renames G.node to G.nodes, replays a seeded run on NetworkX 1.11 and the current release, and reports the other 1.x calls that will break next.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The first error has a one-word fix, and the agent treats it as the visible end of a larger migration. kentwait/nxsim, a package for simulating agents on a graph with SimPy, was written for NetworkX 1.9 and lists 'networkx' in setup.py with no version. On a current install the README's own example stops at line 90 of nxsim/simulation.py, self.env.G.node[i]['agent'] = ..., with the error in the title. The same attribute is read in four more places in nxsim/agents.py, lines 75, 77, 78 and 94. Python 3.12 appends a hint to the message: Did you mean: '_node'? The agent does not take it. G._node is the private dictionary behind the public view; it would make the error disappear and tie the code to an internal name.

The public replacement is G.nodes[i], five edited lines in two files. To show the edit changes nothing else, the agent replays the README's zombie outbreak under a fixed random seed in two environments: Python 3.7 with NetworkX 1.11, where the original code runs, and a current Python with the newest NetworkX and the edited code. Ten nodes, thirty time steps, one trial. Both runs record nine bites, the same count of infected agents at every step, and the same hash over the logged state history that BaseLoggingAgent pickles to disk. That comparison is the proof the pull request leads with.

It then establishes which versions the edit is valid for, because the rename is not backward compatible. On 1.11 G.nodes is a method, and the edited line fails with 'method' object is not subscriptable. G.nodes[i] works from 2.0, the old G.node still works on 2.0 and 2.3 without any warning, and it is gone in 2.4. So the agent sets networkx>=2.0 in setup.py; code that stays on the 1.x line keeps the old spelling, and nothing can install the new spelling beside a release that rejects it.

Last, it looks for what the example does not exercise, by calling the package's other public methods on a small graph. Three more things changed underneath this code. get_neighboring_nodes() is documented as returning a list and now returns an iterator with no len(), because Graph.neighbors changed in 2.0. BaseEnvironmentAgent.add_node passes the attribute dictionary as a second positional argument and fails with Graph.add_node() takes 2 positional arguments but 3 were given. utils.create_copy_without_data calls nodes_iter(), which no longer exists, and loops over the empty graph it has only just created, so it never copied anything on any version. The agent fixes add_node, wraps the neighbour call in list() to keep the documented return type, and reports the copy helper for a maintainer to decide, since renaming the call would not make it copy anything.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "This package fails with \"AttributeError: 'Graph' object has no attribute 'node'\" on current NetworkX. It was written for NetworkX 1.x. Do not use the private G._node that the error hint suggests. First find a runnable example in the README, docs or tests, and make it deterministic with a fixed random seed. Build two environments: one with the newest NetworkX 1.x on a Python that can run it, and one with current releases. Run the example unchanged in the old environment and save its printed output and a hash of any data it writes. Replace G.node with G.nodes, run the example in the new environment, and compare output and hash with the old run. Then find by testing the oldest NetworkX release your edit works on and the first release where the original code fails, and set the lower bound in the package metadata accordingly. After that, go beyond the example: search for other NetworkX 1.x calls (nodes_iter, edges_iter, G.edge, G.adj used as a dict of dicts, add_node or add_edge with a positional attribute dictionary, neighbors() or nodes() used as a list, connected_component_subgraphs, selfloop_edges as a method) and call every public function of the package on a small graph to see which fail. Fix the mechanical ones, keep each function's documented return type, and add a test per fix. Report, without changing it, anything that looks wrong on every version. Open a pull request with the comparison and the list of what you found.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

A pull request that edits nxsim/simulation.py, nxsim/agents.py and setup.py, and adds a test file with the seeded example and one test per additional fix. The description starts with the two runs side by side, the bite count, the per-step counts and the hash from each environment, and then the version table: where G.nodes[i] starts working, where G.node stops. Each further finding has the call that was made, the message it produced and the change, and the copy helper is listed separately as a question.

A reviewer confirms the hashes match and reads the lower bound, since moving a package from an unbounded dependency to >=2.0 is a decision for anyone still on 1.x. They check that functions documented as returning lists still do. If the old environment cannot be built, or the example cannot be made deterministic, the pull request says the comparison was not possible and what was run instead; it does not claim equivalence it did not measure.

Run it on every scheduled dependency check

An unpinned dependency breaks a package on the upstream project's schedule, not yours. An automation with a schedule trigger runs a prompt like this one on a cadence you choose, for example every set to 1d, which fires at midnight in the trigger's time zone. Word the saved prompt generally: install the package with the newest releases of everything it depends on, run the examples and tests, and fix or report what fails. Most nights the run reports that nothing changed.

When the package has users, the same failure also reaches you as an issue with a traceback. A github trigger on issues.labeled, limited to a label a maintainer applies, starts a run with the issue as event text after the prompt. Either way one run handles the repository, so a low max runs per hour is enough.

The use case this job follows, with its automation and trigger: Upgrade dependencies weekly.

Why run it on Rush?

Showing that a rename preserved behaviour needs the old library, and NetworkX 1.11 predates every current Python, so the baseline runs on Python 3.7. Building that environment next to a current one, seeding the example and hashing its output is slow work on a laptop and a few commands in a sandbox that starts empty. A Rush run keeps both environments, the exact commands and their output in the session, so the comparison in the pull request can be traced back to what was executed.

The second reason is coverage. A person who hits this error fixes the line in the traceback, sees the example pass and stops. The agent has been asked to call every public function, and that is how the positional add_node and the iterator returned by neighbors are found before a user reports them one at a time. Each of those findings comes with the call that exposed it.

Frequently asked questions

What replaces G.node in NetworkX?

G.nodes. G.nodes[n] gives the attribute dictionary of node n, and you can assign into it as before. It is available from NetworkX 2.0, and the old G.node was removed in 2.4.

Should I use G._node as the error message suggests?

No. That hint comes from Python, which lists similar attribute names, not from NetworkX. _node is a private attribute, and code that uses it can break in any release without notice. The public G.nodes view does the same job.

Can one codebase support NetworkX 1.x and 2.x?

Not with the same spelling. On 1.11 G.nodes is a method, so G.nodes[n] raises a TypeError, while G.node[n] works on 1.x and up to 2.3. A package has to choose a floor, which is why the agent tests the boundary releases and writes the bound into the metadata.

What else breaks when moving from NetworkX 1.x?

The iterator methods such as nodes_iter and edges_iter are gone, neighbors returns an iterator, nodes() and edges() return views, and add_node no longer accepts an attribute dictionary as a positional argument. The prompt lists these so the agent searches for them and tests each public function instead of stopping at the first error.

More NetworkX jobs

More jobs like this: Upgrade dependencies weekly

Keep reading