# Fix "DecodeError: It is required that you pass in a value for the 'algorithms' argument" in PyJWT

Yes. An agent finds every jwt.decode without algorithms and every .decode() on a token PyJWT 2 returns as str, fixes both, and tests login and a protected route.

[Run this on Rush](https://cloud.getrush.ai/run/fix-jwt-decode-algorithms-argument-required/pyjwt/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

PyJWT 2.0.0 changed two things that a Flask API written against 1.x hits on its next install. `jwt.decode(token, key)` with no `algorithms` raises `jwt.exceptions.DecodeError: It is required that you pass in a value for the "algorithms" argument when calling decode().`, on 2.0.0 and on the current 2.15.1 alike, where 1.7.1 accepted it. And `jwt.encode(...)` returns a `str` instead of `bytes`, so the `token.decode('UTF-8')` that 1.x code used to turn the token into text raises `AttributeError: 'str' object has no attribute 'decode'`. The header a 2.x `encode` writes with no `algorithm` is still `HS256`, so `jwt.decode(token, key, algorithms=["HS256"])` reads tokens issued either way. The current release also warns when an HMAC key is shorter than 32 bytes, with an `InsecureKeyLengthWarning` that names RFC 7518.

hedgarbezerra/Another-FlaskAPI is a Flask REST API with users, posts and comments over MySQL, with token authentication in `app/views/helper.py`. `requirements.txt` pins `PyJWT==1.7.1` beside `Flask==1.0.2`, `Werkzeug==0.15.2` and `SQLAlchemy==1.3.3`, and `runtime.txt` asks for Python 3.7.3, so the pins have been holding it on the old line. Both breaks are in that one file. `token_required` on line 10 reads `token` from the query string and calls `jwt.decode(token, app.config['SECRET_KEY'])` on line 17 inside a bare `except:` that answers `401` with `token is invalid or expired`, so on PyJWT 2 every protected route, including `GET /v1` in `app/routes/routes.py`, refuses every token without a traceback anywhere. `auth()` on line 26 calls `jwt.encode(...)` on line 35 and then `token.decode('UTF-8')` on line 37, so `POST /v1/authenticate` answers a `500` instead. `config.py` line 19 builds `SECRET_KEY` from 12 random characters at each start, which is why tokens stop working on restart and is also short enough to trip the new key-length warning.

The agent reproduces both failures with the app's own code and no database. It imports `helper` with a stubbed `user_by_username`, calls `jwt.encode` the way line 35 does under PyJWT 2 and shows the `AttributeError`, then calls `jwt.decode` the way line 17 does and shows the `DecodeError`, and keeps both tracebacks. The fix is `jwt.decode(token, app.config['SECRET_KEY'], algorithms=['HS256'])` on line 17, an explicit `algorithm='HS256'` on the encode and no `.decode('UTF-8')` on line 37, and it narrows the bare `except:` to `jwt.ExpiredSignatureError` and `jwt.InvalidTokenError` so a programming error stops hiding behind a `401`. `requirements.txt` moves the pin to a 2.x release.

To test it the agent runs the Flask app against SQLite in the sandbox, swapping the `mysql://` URI from `config.py` through an environment variable for the test only, creates a user, posts to `/v1/authenticate` with basic auth, and uses the token on `GET /v1`, asserting the greeting on line 13 of `routes.py` comes back with the user's name. It adds a second case with a tampered token that must get `401`. The `SECRET_KEY` generation is left as it is but called out in the pull request, since a key that changes on every start and is under 32 bytes is a separate decision for the team.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "This Flask API fails after a dependency update: every authenticated route answers 401 'token is invalid or expired' and POST /v1/authenticate answers 500. Read requirements.txt and report the PyJWT pin. In a clean virtual environment install the current PyJWT and reproduce both failures by calling jwt.encode and jwt.decode the way the helper module does, keeping the tracebacks. Find every jwt.decode call without an algorithms argument and every .decode() applied to the result of jwt.encode, with file and line, and fix them: pass algorithms=['HS256'] (or whatever algorithm the encode uses), set the algorithm explicitly on encode, and drop the .decode('UTF-8'). Narrow any bare except around jwt.decode to jwt's own exceptions. Update the PyJWT pin to a 2.x release. Run the app against SQLite in the sandbox by overriding the database URI through an environment variable for the test only, create a user, obtain a token through the authenticate route, call a protected route with it, and add a case with a tampered token that must get 401. Do not change how the secret key is generated, but describe its length and lifetime in the pull request. Open a pull request with both tracebacks, the changed lines, and the test output.",
  "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 three lines in `app/views/helper.py`, the exception clause around them, the pin in `requirements.txt`, and adds a test. The description opens with the two tracebacks captured under PyJWT 2, then a note on why the first break was silent: the bare `except:` on line 19 turned a library error into a `401` that looked like a bad token.

The session's record holds the install, the reproduction, the test run against SQLite with the `authenticate` call, the protected call with the token and the tampered-token case ending in `401`. A reviewer can read the greeting the protected route returned rather than infer it.

Before merging, a reviewer runs the test against the MySQL the app uses, since the sandbox used SQLite, and decides what to do about the `SECRET_KEY` on line 19 of `config.py`, which the pull request describes but does not change: a key that changes on every start invalidates every issued token, and one under 32 bytes trips PyJWT's key-length warning.

## Run it on every 401 spike after a deploy

The first break here is silent, which makes an alert the right trigger. A Grafana contact point can call a Rush `webhook` trigger directly with `Authorization: Bearer <its secret>` as a custom header, so an alert on the `401` rate after a deploy starts one run with the alert's payload after the prompt. Set that trigger call's `Idempotency-Key` to the alert's fingerprint so a repeated notification does not start a second run.

From a deployment script instead, one `POST /o/{org}/sessions` with the repository in `resources` and a `request_id` that is a UUID v5 of the deploy id starts the run; the key's webhook posts `run.completed` or `run.failed` with the session URL. A run an automation starts is not tied to the key and has its own `max_runs_per_hour` cap.

## Why run it on Rush?

A token bug that answers `401` instead of raising looks like a client problem for a long time. The agent reproduces it with the library and the app's own lines in a clean sandbox, which is the quickest way to prove the cause is the dependency and not the clients, and it leaves the two tracebacks beside the fix.

The test needs a running app and a database, and the sandbox gives it both without touching the MySQL the API uses. Several APIs built from the same tutorial, with the same `token_required` decorator, get the same request as separate runs, each with its own record and pull request, as many at once as the key's `max_concurrent` limit allows.

## Frequently asked questions

### Why does the protected route say the token is invalid instead of showing the error?

Because line 17's `jwt.decode` sits in a bare `except:` that answers `401` for any exception. On PyJWT 2 the exception is the library's `DecodeError` about `algorithms`, not a bad token. The pull request narrows the clause to `jwt.ExpiredSignatureError` and `jwt.InvalidTokenError` so the next library error is visible.

### Which algorithm should I pass?

The one the encode uses. With no `algorithm` argument PyJWT writes `HS256` in the token header on both 1.x and 2.x, so `algorithms=['HS256']` reads old and new tokens. The agent reads the header of a token the app issues rather than assuming.

### Why does POST /v1/authenticate return 500?

Line 37 calls `.decode('UTF-8')` on the result of `jwt.encode`, which PyJWT 2 returns as `str`; 1.7.1 returned `bytes`. Dropping the `.decode` call fixes it on 2.x, and the pull request pins 2.x so the two sides match.

### Does the agent need the MySQL database?

No. It runs the app against SQLite in the sandbox by overriding the URI from `config.py` through an environment variable for the test, which is enough to create a user, issue a token and call a protected route. The reviewer runs the same test against MySQL.

### What about the secret key that changes on every start?

`config.py` line 19 builds a 12-character key at import, so every restart invalidates every token, and PyJWT 2.15 warns that an HMAC key under 32 bytes is below the recommended length. The request leaves it unchanged and asks for a description in the pull request, since the fix is a configuration decision.

## More PyJWT jobs

- [Upgrade PyJWT From 1.x to 2.x in a Flask API](https://getrush.ai/run/upgrade-pyjwt/pyjwt/)
- [Add Logout That Revokes a JWT in FastAPI](https://getrush.ai/run/add-token-revocation/pyjwt/)

## More jobs like this: Fix a bug from an alert

- [Fix "failed to fetch https://kubernetes-charts.storage.googleapis.com/index.yaml : 403 Forbidden" in a Helm Chart's Dependencies](https://getrush.ai/run/fix-kubernetes-charts-storage-googleapis-com-403-forbidden/helm/)
- [Fix "# Warning: 'bases' is deprecated. Please use 'resources' instead" Across a Kustomize Repository](https://getrush.ai/run/fix-kustomize-bases-is-deprecated/kustomize/)
- [Fix "TypeError: mfcc() takes 0 positional arguments but 2 were given" in librosa](https://getrush.ai/run/fix-librosa-mfcc-takes-0-positional-arguments/librosa/)
- [Fix "SetuptoolsDeprecationWarning: License classifiers are deprecated" and "project.license as a TOML table is deprecated"](https://getrush.ai/run/fix-license-classifiers-are-deprecated/setuptools/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
