What the agent does
In Python 2, json.loads(s, encoding='utf-8') told the decoder how to read a byte string. Python 3 kept the keyword and ignored it. Python 3.8 said so with DeprecationWarning: 'encoding' is ignored and deprecated. It will be removed in Python 3.9, and 3.9 removed it, so the call now fails with the TypeError in the title. On 3.9 itself the message starts with a bare __init__(). Deleting the argument is always the right edit and never changes a result, because the argument had no effect. What the agent adds is the answer to the question the argument was hiding: for each call, what does decode the bytes, and is that the encoding the author believed they had chosen?
dallascard/scholar, the code for a topic-model paper, routes all its JSON through one module. file_handling.py defines read_json, which calls json.load(input_file, encoding='utf-8'), and read_jsonlist, which calls json.loads(line, encoding='utf-8') for each line. Both open the file with codecs.open(input_filename, 'r', encoding='utf-8'), so the text is already decoded as UTF-8 before the parser sees it. Here the keyword was redundant and the fix is two deletions. The agent confirms that by reading the open call next to each json call, and does not infer it from the keyword's value.
It then follows the callers to see how far the failure reaches. read_jsonlist is used by preprocess_data.py, preprocess_data_old.py and split_data.py, and read_json by run_scholar.py, run_scholar_tf.py and compute_npmi.py. The README's walkthrough runs python download_imdb.py, which only writes files, and then python preprocess_data.py data/imdb/train.jsonlist ..., which reads one. A new user on a current Python therefore gets through the download and fails on the second command. The README's install line is conda create -n scholar python=3, with no minor version, which is how a setup that worked when it was written ends up on an interpreter the code never ran on. The agent proposes a version in that line and reports that the README also lists pytorch 0.4, without attempting that upgrade.
The risky cases are the ones this repository does not have, and the prompt makes the agent look for them anywhere. A call that passes encoding='latin-1' with a file opened by a bare open(path) was never reading Latin-1: the file was decoded with the platform's default encoding, and the keyword was decoration. A call that passes bytes straight to json.loads gets automatic detection of UTF-8, UTF-16 or UTF-32 and nothing else, so bytes in another encoding raise UnicodeDecodeError whatever the keyword said. In both, removing the argument is still correct, and the agent moves the stated encoding to where it works: the open call, or an explicit .decode(). Another script in scholar, import_congress_press.py, opens its input with encoding='Windows-1252', so mixed encodings are a real possibility in this kind of code. The agent adds a round-trip test with non-ASCII text for each reader it touched.
Start it with one API call
POST https://api.phoenix.so/o/{org}/sessions with this body:
{
"agent": "claude",
"input": "This project fails on Python 3.9 or later with \"TypeError: JSONDecoder.__init__() got an unexpected keyword argument 'encoding'\" (on 3.9 the message is \"__init__() got an unexpected keyword argument 'encoding'\"). Find every call to json.load, json.loads, json.JSONDecoder, and to simplejson equivalents if used, that passes an encoding argument, and every json.dump or json.dumps that passes one. Remove the argument in each. Then, for every call you changed, determine what actually decodes the input: if the input is a file object, find the open, io.open or codecs.open call that created it and record its mode and encoding; if it is bytes, say where the bytes come from; if it is str, say where it was decoded. Build a table with the encoding the removed keyword named and the encoding really in effect. Where they disagree, or where a text file is opened with no encoding so the platform default applies, move the intended encoding to the open call or to an explicit decode, and flag the row, because that change can alter what is read. Never change an encoding that the table shows was already in effect. List the scripts and entry points that reach each changed function, and identify the first documented command a new user would run that fails. Add a test for each reader that writes non-ASCII text to a temporary file and reads it back. If the README or environment file leaves the Python version open, propose a concrete supported range. Run the tests on the oldest and newest Python the project can support and include the commands and output. Open a pull request with the table.",
"resources": [
{
"url": "https://github.com/your-org/your-app"
}
]
}- 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.
- 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.
- 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 whose description is built around one table: each call, the encoding its keyword claimed, the encoding actually in effect and where that is set. For scholar the table has two rows, both UTF-8 on both sides, and the diff removes two keyword arguments and adds a test file. The description also names the first documented command that fails and lists the six scripts that depend on the two readers.
A reviewer goes straight to any row where the two encodings differ. Those are the only rows where the pull request changes behaviour, and each one asks a question about the data: which encoding the files really use. The agent answers it when it can, by reading sample files in the repository, and otherwise leaves the row flagged with both options. Rows that agree need no thought, which is the point of the table. The proposed Python range in the README is a separate small commit that the reviewer can take or drop.
Run it on every Grafana alert firing
Data jobs that read JSON usually fail in a scheduler, and the first sign is an alert on their error logs. A Grafana contact point of type webhook can send a custom Authorization header, so it can call a Rush webhook trigger directly: create the trigger on an automation that holds the prompt above, store its secret when the console shows it, and set the contact point's URL and Authorization: Bearer header to match. The alert's JSON body becomes the event text after the prompt.
Grafana repeats notifications while an alert keeps firing. Keep the automation's max runs per hour at 1 so a long outage does not start a run per repeat, and route only the alert rule that matches this TypeError to the contact point. A resolved notification also posts to the URL; add a line to the saved prompt telling the agent to answer in one sentence and change nothing when the event's status is resolved.
The use case this job follows, with its automation and trigger: Fix a bug from an alert.
Why run it on Rush?
The edit takes a minute, and the evidence that it is safe takes longer. Showing that the keyword did nothing means running the readers on an interpreter that still accepts it and on one that does not, with files containing non-ASCII text, and comparing what comes back. The agent has both interpreters in its sandbox, writes the files, and the comparison is kept in the session record. The table's two encoding columns are filled from what it read and ran.
The sandbox also gives a neutral platform default. Whether a bare open(path) reads a file correctly depends on the machine's locale, so a developer's laptop and the production container can disagree, and neither is evidence about the other. The agent can run the same reader under more than one locale setting and show which calls depend on it, which is the one finding in this job that can change real output.
Frequently asked questions
What do I replace json.loads(s, encoding='utf-8') with?
json.loads(s). The keyword has been ignored for the whole of Python 3, so removing it changes nothing. If s is bytes, the function detects UTF-8, UTF-16 or UTF-32 by itself. For any other encoding, decode first: json.loads(data.decode('latin-1')).
And json.load(f, encoding=...)?
json.load(f), with the encoding set where the file is opened: open(path, encoding='utf-8'). The parser receives text that the file object has already decoded, so the open call is the only place an encoding has any effect.
Did the encoding argument ever work in Python 3?
No. It was accepted for compatibility with Python 2 code and ignored. That is why some projects have a keyword naming one encoding and a file opened in another, with nobody noticing until the keyword became an error.
The error comes from a library, not my code. What then?
The traceback's last frame inside your dependencies names the package that still passes the keyword. The agent reports the package and version, checks whether a newer release has dropped the argument, and raises the pin if your tests pass with it. It does not patch files inside installed packages.
More jobs like this: Fix a bug from an alert
- Fix "DecodeError: It is required that you pass in a value for the 'algorithms' argument" in PyJWT (PyJWT)
- Fix "failed to fetch https://kubernetes-charts.storage.googleapis.com/index.yaml : 403 Forbidden" in a Helm Chart's Dependencies (Helm)
- Fix "# Warning: 'bases' is deprecated. Please use 'resources' instead" Across a Kustomize Repository (Kustomize)
- Fix "TypeError: mfcc() takes 0 positional arguments but 2 were given" in librosa (librosa)
- Fix "SetuptoolsDeprecationWarning: License classifiers are deprecated" and "project.license as a TOML table is deprecated" (setuptools)
- Fix "AttributeError: module 'locale' has no attribute 'format'" on Python 3.12 (locale)