All jobs

Fix ImportError: Cannot Import Name 'json' From 'itsdangerous' in a Flask 1 App

Yes. An agent reproduces the import error from the unpinned itsdangerous, pins the release Flask 1.1 still imports, checks the app starts and opens a PR.

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 clones the repository into its own cloud sandbox and reads the pins first. In an API like blabla1337/skf-flask, the OWASP Security Knowledge Framework, requirements.txt pins Flask==1.1.2, Jinja2==2.11.3, MarkupSafe==1.1.1, Werkzeug==0.16.1, Click==7.1.2, flask-restplus==0.13.0, Flask-SQLAlchemy==2.5.1 and SQLAlchemy==1.3.23, and no line for itsdangerous; setup.py reads that file into install_requires. skf/app.py builds the app in create_app and takes SQLALCHEMY_DATABASE_URI from skf/settings.py, which defaults to sqlite:///db/db.sqlite unless SKF_DB_URL is set, wsgi.py exposes app, tests/test_skf.py drives it through app.test_client(), and Docker/alpine-cloud/api/Dockerfile starts FROM python:3.7-slim-stretch.

Then it reproduces the failure. On a current Python the pinned set does not even install, because elastic-apm==6.0.0 fails to build without pkg_resources; on Python 3.9 it installs and pip resolves the one unpinned package to the newest itsdangerous. python -c "import flask" then stops at flask/json/__init__.py line 15, from itsdangerous import json as _json, with ImportError: cannot import name 'json' from 'itsdangerous'. Flask 1.1.2 has no upper bound on itsdangerous, and 2.1 dropped the json shim Flask 1 imported. With itsdangerous<2.1 installed, import flask and import skf.app both succeed on the same pins.

The fix the agent proposes is the smallest one that matches the file's own style: add itsdangerous==2.0.1 to requirements.txt beside the other pins, so the resolver stops drifting. It does not move to Flask 1.1.4, whose bounds would pull itsdangerous 1.x, because the file pins every other Pallets package to an exact release and the pin is the pattern. It does not jump to Flask 2 either, and says why: Werkzeug is held at 0.16.1 next to flask-restplus 0.13.0, so the real upgrade is a flask-restx migration that this one-line fix only buys time for.

To show the app starts, the agent runs python -c "import skf.app" and the test module's import on the installed set, and reads the Dockerfile's Python 3.7 base to note that the image installs the same requirements file and gets the same pin. The pull request quotes the ImportError from before and the clean imports after, and lists the Flask 2 and flask-restx path as the follow-up, with the pins that block it.

Start it with one API call

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

{
  "agent": "claude",
  "input": "This Flask app fails to start with \"ImportError: cannot import name 'json' from 'itsdangerous'\". Read requirements.txt, setup.py, any Dockerfile and the app's entry module first, and list which Flask-related packages are pinned and which are not. Create a virtual environment on the Python version the Dockerfile uses, install the requirements, and reproduce the error with python -c \"import flask\", quoting the traceback. Find the newest itsdangerous release that imports with the pinned Flask, add it as an exact pin in the same style as the file, reinstall, and show import flask and the app's entry module import cleanly. Do not upgrade Flask or any other pin in this change; say which pins block a Flask 2 upgrade and why. Open a pull request that quotes the error before and the clean import after.",
  "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

What comes back is a pull request touching requirements.txt only: one added line, itsdangerous==2.0.1, in the alphabetical place the file keeps. The description quotes the traceback ending at flask/json/__init__.py with the ImportError, the pip resolution that put itsdangerous past 2.1, the clean import flask and import skf.app after the pin, and the Python version it used because the pinned set would not build on a newer one.

A reviewer checks that the pin is the newest release that still carries the json module Flask 1.1 imports, that setup.py picks it up since it reads the same file, and reads the note on what holds the app on Flask 1: the Werkzeug pin and flask-restplus. The run's record keeps the install and import output, so the resolution and the traceback can be read as they happened.

Run it on every failed deploy

Add an automation to the Project with this prompt and a webhook trigger. In the pipeline that builds the image or installs the requirements, add a step on failure that sends the job's name and the last lines of its log to the trigger's URL with Authorization: Bearer and the secret Rush showed when the trigger was made. One run starts per failed job, reads the log as data after the prompt, and opens one pull request on its own branch; a run that cannot make the import clean ends needs_review with what it tried attached.

From your own code, send this body to POST /o/{org}/sessions with an API key and a request_id that is a UUID v5 of the pipeline run id, so a retried delivery returns the run already started rather than a second branch.

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

Why run it on Rush?

This is a failure that appears without a commit. The requirements file did not change; a transitive release did, and the first anyone hears of it is a deploy that cannot import Flask. The agent reproduces that drift in its own sandbox from the same file, on the Python the Dockerfile names, and keeps the resolution and the traceback in the run's record, so the pull request pins on evidence rather than on a forum post about which version broke it.

It also refuses to turn a one-line pin into a surprise migration. The pull request fixes the import, states in plain words which pins keep the app on Flask 1 and what the real upgrade is, and leaves that for a run you start on purpose. What comes back is a deployable fix with the before and after attached, started by the pipeline that failed.

Frequently asked questions

Why does itsdangerous break Flask when nothing in my file changed?

Flask 1.1.2 declares itsdangerous>=0.24 with no upper bound, and itsdangerous 2.1 removed the json module that Flask 1's flask.json imported. A fresh install picks the newest itsdangerous, so an environment built last year and one built today get different code from the same requirements file. The pin makes the two the same.

Why pin instead of upgrading Flask?

In this repository Werkzeug is held at 0.16.1 because flask-restplus 0.13.0 imports things later Werkzeug releases removed, and Flask 2 needs Werkzeug 2. Upgrading Flask therefore means replacing flask-restplus with flask-restx across the API modules, which is its own change. The pin restores the deploy today; the pull request names that migration as next.

Is 2.0.1 the right itsdangerous version?

It is the newest release that still ships the json module Flask 1.1 imports; 2.1 removed it. The agent checks it by installing and importing rather than by reading a changelog, and quotes the import. If you prefer the 1.x line that Flask 1.1.4 would select, say so in the prompt.

Will the Docker image get the fix?

Yes, as long as it installs from the same requirements.txt, which this one does. The agent reads the Dockerfile, notes its Python 3.7 base, and runs the reproduction on a Python the pinned set can build on, so the image and the pull request agree. A Dockerfile that installs packages by name instead would be listed as a second place to pin.

More jobs like this: Fix a bug from an alert

Keep reading