All jobs

Fix "LegacyAPIWarning: The Query.get() method is considered legacy as of the 1.x series of SQLAlchemy"

Yes. An agent replaces Model.query.get() with db.session.get(), shows which commands surface the warning, and drives the route with a test client before and after.

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

What the agent does

The agent reads the app and how it is installed, because this warning only appears under some commands and the fix should be checked under the one that raised it. In a small Flask app like yymm/flask-vuejs, requirements.txt lists Flask, Flask-SQLAlchemy and gunicorn with no versions, the Procfile runs gunicorn app:app, and db.cfg points SQLALCHEMY_DATABASE_URI at sqlite:///test.db. app.py declares one model, Todo on the todos table with a todo_id primary key, title, done and pub_date, and four JSON routes; /sqlalchemy/update at line 96 fetches the row with Todo.query.get(request.json['id']) before setting done and title and committing.

Then it reproduces, which takes some care. A clean install today resolves Flask 3.1.3, Flask-SQLAlchemy 3.1.1 and SQLAlchemy 2.1.8. Under a plain python, a test client that creates the table, adds a row and posts to /sqlalchemy/update prints nothing, because LegacyAPIWarning descends from DeprecationWarning and Python hides those outside __main__. With python -W always the line appears: app.py:96: LegacyAPIWarning: The Query.get() method is considered legacy as of the 1.x series of SQLAlchemy and becomes a legacy construct in 2.0. The method is now available as Session.get() (deprecated since: 2.0) (Background on SQLAlchemy 2.0 at: https://sqlalche.me/e/b8d9). Under pytest the same line lands in the warnings summary of an otherwise passing test, and with -W error::DeprecationWarning the request fails with the warning as an exception.

The fix is db.session.get(Todo, request.json['id']), the Session.get() the warning names, reached through Flask-SQLAlchemy's scoped session. The other three routes use Todo.query.order_by(...).all() and db.session.add, which the warning does not cover, so they stay. The agent also notes the /sqlalchemy route's bare except: that creates the database on first failure, and leaves it alone as unrelated.

Proof is the same test client run twice. After the change, GET /sqlalchemy/get lists the row, POST /sqlalchemy/update returns {"status": "ok"} and the next GET shows done true with the new title, and python -W always prints no LegacyAPIWarning. The pytest run passes with an empty warnings summary.

Start it with one API call

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

{
  "agent": "claude",
  "input": "Our test run reports \"LegacyAPIWarning: The Query.get() method is considered legacy as of the 1.x series of SQLAlchemy\". Find every .query.get( call in the app, say which Flask, Flask-SQLAlchemy and SQLAlchemy versions a clean install resolves, and show under which commands the warning is visible (plain python, python -W always, pytest, -W error). Replace each call with db.session.get(Model, id), then drive the affected routes with Flask's test client against a fresh SQLite file before and after, and open a pull request with the version list, the warning as printed and both transcripts.",
  "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 changes one line in app.py: Todo.query.get(...) becomes db.session.get(Todo, ...). The description names the three resolved versions, quotes the warning with its file and line, explains why a plain python run hides it and pytest shows it, and includes the before-and-after test client transcript with the updated row.

A reviewer checks that the lookup is by primary key as before, that the commit still happens, and that no other query.get remains. They see that Todo.query.order_by(...).all() and db.session.add were left as they are because the warning does not name them.

The description lists the two things the agent saw and did not change: the bare except: that creates the database on first request, and the unpinned requirements.txt.

Run it on every pytest run with a warnings summary

The warning appears in CI test logs long before it fails anything, so the test job is the trigger. A step in the workflow posts the pytest output to a Rush webhook trigger with Authorization: Bearer and the trigger's secret when the warnings summary contains LegacyAPIWarning, and the log follows the prompt as data; the run opens the pull request with the replacement calls.

For a set of Flask services that share the same pattern, your server sends this request once per repository, each with its own UUID as request_id so a retry returns the run already started, and reads each run.completed webhook; a key starts at most max_concurrent runs at once and queues the rest.

The use case this job follows, with its automation and trigger: Fix a bug from an alert.

Why run it on Rush?

A warning that only some runners show is easy to dismiss as noise, until a -W error flag or a pytest configuration turns it into a failure. In a Rush sandbox the agent runs the app under each command, keeps every transcript, and names the resolved versions, so the pull request explains why the warning comes and goes rather than only silencing it.

The change is one line, but it touches a route that writes to the database. The agent drives the route with a test client against a fresh SQLite file before and after and shows the row updated, so the reviewer reads a diff with the behaviour proven, not assumed.

Frequently asked questions

Why do I see this under pytest but not when running the app?

Because LegacyAPIWarning is a DeprecationWarning, which Python hides by default outside the main module. pytest shows deprecation warnings in its summary, and python -W always or -W error::DeprecationWarning make them visible or fatal.

What is the Flask-SQLAlchemy way to write Session.get()?

db.session.get(Model, primary_key). It returns the instance or None, exactly like Model.query.get() did, so the surrounding code that sets fields and commits does not change.

Do Model.query.all() and order_by() warn too?

Not in this run: the three other routes use Todo.query.order_by(...).all() and the warning names only Query.get(). The agent reports what the run printed and changes only the call that warned.

Should the requirements be pinned at the same time?

That is a separate decision. The pull request records the versions the clean install resolved, Flask 3.1.3, Flask-SQLAlchemy 3.1.1 and SQLAlchemy 2.1.8, so the maintainers can pin them if they want the same result on the next machine.

More Flask-SQLAlchemy jobs

More jobs like this: Fix a bug from an alert

Keep reading