What the agent does
base64.encodestring and decodestring were deprecated aliases for years and Python 3.9 removed them. The error names one function, and the usual advice is to rename it to encodebytes. In code that builds an HTTP Basic Authorization header, that rename is rarely the whole fix. DocketAlarm/pacer-api shows why. Line 85 of api/client.py reads auth = base64.encodestring('%s:%s' % (username, password)).strip(), inside a call() function that otherwise uses urllib.request. The argument is a str. On Python 3.8 that line already raised TypeError: expected bytes-like object, not str, so the branch never worked on Python 3 at all. The removal in 3.9 only changed which exception it raises.
The agent reproduces the failure before editing. The repository has no setup.py, no requirements file and no test runner, and api_test.py is an interactive sample that prompts for a user name, so there is nothing to run as it stands. It reads call() to find when line 85 executes: only when a caller passes both username and password to an endpoint other than login or subaccount. The client's own helpers pass credentials on lines 125, 272 and 320, always to login, which sends them as form fields and skips the header. That explains how a line that cannot run survived. A small script that replaces urllib.request.urlopen with a stub and calls call("searchpacer", username=..., password=...) raises the error in the title on 3.9 and later.
Then it fixes the line for what it is meant to produce, a header value. The replacement is base64.b64encode(('%s:%s' % (username, password)).encode('utf-8')).decode('ascii'). Each part answers a specific trap. Encoding the input fixes the TypeError. Decoding the output matters because "Basic %s" % b'dTpw' formats as Basic b'dTpw', a header no server accepts and no exception reports. Choosing b64encode over encodebytes matters because encodebytes inserts a newline after every 76 characters of output, .strip() removes only the last one, and credentials longer than 57 bytes then put a newline inside the header, which urllib refuses with ValueError: Invalid header value.
Last, the agent adds the test that was missing. It stubs urlopen, captures the Request, and asserts the exact Authorization value for a short credential pair, for a long pair that would have wrapped, and for a password with a non-ASCII character. It checks that login still sends no header. It runs the file on the oldest and newest interpreters it can install and searches the rest of the tree for decodestring, the other alias removed in the same release.
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 \"AttributeError: module 'base64' has no attribute 'encodestring'\". Find every use of base64.encodestring and base64.decodestring in the source, tests and scripts. For each one, work out what the result is used for (an HTTP Authorization header, a file, a comparison) and what types go in and come out, and check whether the call was already broken on Python 3 because it passes str where bytes are required. Reproduce the failure with a script or test that reaches the line without real network access; stub the HTTP call if you need to. Do not just rename the function. For a header or any single-line value use base64.b64encode, encode str input to bytes explicitly, and decode the result to str before formatting it into text. Use encodebytes only where the output really should be wrapped in 76-character lines. Add tests that assert the exact value produced, including an input long enough that the old function would have inserted a newline, and one with a non-ASCII character. Run the tests on the oldest Python version the project claims to support and on a current one, and include the output of both. Say whether the fixed code path could ever have worked before, and how you know. Open a pull request.",
"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 that changes one line of the client and adds one test file. The description quotes the old line, the new line, and the three values that tell them apart: the TypeError the old call raised on Python 3.8, the AttributeError it raises from 3.9, and the header the new call builds. It includes the test output from each interpreter the agent used.
It also carries a finding the error message does not hint at. In a client like this one, the description says that Basic authentication on non-login calls has never worked on Python 3, names the callers that avoided it by using a login token, and points out that merging the fix turns on a code path your server has not seen from this client before. That is worth one manual request against a staging account before a release.
The reviewer checks the header assertion by decoding the expected value by hand, confirms the stub replaces only the network call, and reads the search results for any other deprecated alias. If the project supports Python 2 as well, the description says how the new line behaves there, since str and bytes are the same type on that side.
Run it on every new error in your tracker
A removed standard-library function shows up as a new error group the day a host or image moves to a newer Python. Create an automation on the repository's Project with the prompt above and a webhook trigger. Error trackers generally sign their own requests and cannot add the Authorization: Bearer header the trigger expects, so a handler on your server receives the tracker's webhook, verifies it, and forwards the event body to the trigger's URL with the secret. The traceback then follows the prompt as event text.
Send an Idempotency-Key header on that forwarded call to the trigger, set to the tracker's issue id, so a redelivery of the same event starts nothing new. The automation's max runs per hour limits how many runs a noisy day can start, and each run that finds a different error than this one reports it and opens no pull request.
The use case this job follows, with its automation and trigger: Fix a bug from an alert.
Why run it on Rush?
The quick fix for this error is a one-word rename that turns one exception into another, or worse, into a wrong header and a 401 from a server you do not control. Telling those apart takes running the line with real inputs on more than one interpreter. The agent does that in its own sandbox, installs the Python versions it needs there, and keeps each command and its output in the session, so the pull request arrives with the evidence that the value is right and not only that the exception is gone.
It is also a fix that gets deferred, because the code path is rare and the traceback looks trivial. Handing it to a run that starts from the alert means the reproduction, the test and the search for the sibling alias happen the same day, without pulling someone off other work to set up an old interpreter for a line they did not write.
Frequently asked questions
Can I just replace encodestring with encodebytes?
Only if the argument is already bytes and you want line-wrapped output, as in a MIME body. encodebytes rejects str, returns bytes, and adds a newline every 76 characters and at the end. For a header, a token or a URL parameter, b64encode is the right function.
Why did this code work on Python 2?
On Python 2, str was a byte string, so encodestring('user:pass') was valid and returned a str that formatted cleanly into the header. Code converted mechanically to Python 3 keeps the call and loses both properties. That is the situation in pacer-api, whose imports are urllib.request style while this line is unchanged.
Does the same apply to decodestring?
Yes, it was removed in the same release. base64.b64decode accepts str or bytes and returns bytes, so the usual follow-up is a .decode() where the caller expects text. The agent lists each decodestring call with what its result feeds.
What if my project has no tests at all?
Then the agent writes the smallest one that reaches the fixed line, with the network call replaced by a stub, and says in the pull request that it is the first test in the repository. It uses unittest from the standard library unless the project already names another runner.
More jobs like this: Fix a bug from an alert
- Fix "AttributeError: 'Flask' object has no attribute 'before_first_request'" with Flask-SQLAlchemy (Flask-SQLAlchemy)
- Fix "TypeError: BotBase.__init__() missing 1 required keyword-only argument: 'intents'" in discord.py 2 (discord.py)
- Fix a Scrapy Spider That Stopped Returning Items (Scrapy)
- Fix "DeprecationWarning: Buffer() is deprecated" (DEP0005) in Node.js (Node.js)
- Fix "DeprecationWarning: Call to deprecated method findAll" in Beautiful Soup (Beautiful Soup)
- Fix "Cannot find module 'webpack/lib/LoaderTargetPlugin'" With html-webpack-plugin 4 (html-webpack-plugin)