All jobs

Migrate From aioredis to redis-py's asyncio Client

Yes. An agent moves an async Python app from aioredis to redis.asyncio, tests pub/sub against a real Redis and reports what else blocks Python 3.11.

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

What the agent does

aioredis stopped at 2.0.1 in December 2021 and its code now lives inside redis-py as redis.asyncio. Most teams meet this as a traceback: on Python 3.11, import aioredis fails with TypeError: duplicate base class TimeoutError, raised from line 14 of the package's own exceptions.py. The agent starts by reproducing that error in the sandbox, then lists every import. In a project like brave-chat/brave-chat-server there are two: from aioredis import from_url in app/config.py, where Settings.redis_conn awaits from_url(...) with decode_responses=True, and from aioredis.client import PubSub, Redis in app/utils/pub_sub_handlers.py. The pin appears three times, in requirements.txt, in pyproject.toml and in poetry.lock.

The code change is two import lines: from redis.asyncio import from_url and from redis.asyncio.client import PubSub, Redis. The calls this app makes, conn.pubsub(), subscribe, publish and get_message(ignore_subscribe_messages=True), keep their names and the message stays a dict with type, pattern, channel and data. The dependency change takes more care. The app runs on Python 3.9.10 in its Dockerfile, in CircleCI and in tox.ini, and current redis-py requires 3.10, so the resolver settles on redis 7.0.1 here. That version needs async-timeout>=4.0.3 on this interpreter, while requirements.txt pins async-timeout==4.0.2, so the install fails until the agent raises that pin as well. poetry.lock already holds 4.0.3.

The repository's tests cover auth, contacts and users, and none of them opens a websocket or touches Redis. So the agent installs a Redis server in the sandbox with apt, starts it on a local port, and writes a test that drives the real handlers: it subscribes through producer_handler's code path, publishes the JSON the consumer sends, and asserts the text a websocket would receive. It runs that test on aioredis first, on Python 3.9, then on redis-py, so the same assertions pass on both sides of the change.

Two things in this code only show up once the import works, and the agent handles them differently. await conn.close() still works but now warns that close is deprecated since redis-py 5.0.1 in favour of aclose(); the agent renames the three calls on the client, two in the router and one in consumer_handler. In app/web_sockets/router.py, both endpoints pass bare coroutines to asyncio.wait, which Python 3.11 rejects with TypeError: Passing coroutines is forbidden, use tasks explicitly. Replacing aioredis alone therefore does not make the app run on 3.11. The agent wraps the two handlers in asyncio.create_task if your prompt asks for 3.11 and otherwise reports it, with the line numbers.

Start it with one API call

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

{
  "agent": "claude",
  "input": "Migrate this app from aioredis to redis-py's asyncio client. Read requirements.txt, pyproject.toml, the lock file, the Dockerfile, the CI config and every file that imports aioredis. First show the reason for the change: create a Python 3.11 environment, install the pinned aioredis and print what importing it raises. Then install a Redis server with apt and start it on a local port; there is no Docker here. Before editing, write a test that exercises this app's own Redis code against that server on the Python version the project pins: connect the way the settings module does, subscribe, publish, read the message and close. Run it on aioredis. Replace the imports with redis.asyncio, choose the newest redis release that supports the project's Python version and say which one and why, and resolve any pin that conflicts with it, in every manifest and the lock file. Run the same test again and show both outputs. Run with warnings as errors and fix the deprecations redis-py reports, such as close() to aclose(). Do not change the Python version. Separately, try the app's Redis and websocket code on Python 3.11 and list anything else that fails there, with file and line; fix only what is a one-line change and report the rest. Run the existing suite if its database is reachable and say so if it is not. Open a pull request with the test, both outputs and the list.",
  "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 the two import blocks, requirements.txt, pyproject.toml and poetry.lock, renames close() to aclose() and adds the pub/sub test. The description opens with the Python 3.11 traceback from the old package, then the test output on aioredis and on redis-py. It names the redis version chosen and the reason: 7.0.1 is the last release whose metadata admits Python 3.9, and 7.1.0 onward requires 3.10. It lists the async-timeout pin as a change the migration forced.

A reviewer reads the list of what the run could not prove. This app's suite needs SingleStore credentials that CircleCI injects into .env, and a sandbox does not have them, so the description says the database tests did not run and the Redis test did. It also carries the findings the agent left alone: redis_conn is annotated -> str but returns a client, get_message is called with no timeout inside while True and returns at once when nothing is waiting, and the except blocks in the router test if conn: for a name that is never bound when the connection itself failed.

Run it on every Linear issue labelled python-upgrade

The aioredis error usually arrives as one line in a larger ticket about moving a service to a newer Python. Connect Linear in the console under Settings → Connections, then add an automation to the Project with a linear trigger on linear.issue.labeled, the label your team uses, and the prompt above. The labelled issue is placed after the prompt as event text, so the prompt has to name the job by itself: it cannot read a package name out of the issue.

The trigger fires only when the person who adds the label is in your Rush org; a label added by an integration does nothing. One issue starts one run, and the automation's max runs per hour decides how many start when a backlog is labelled in one sitting. The rest wait as queued runs.

The use case this job follows, with its automation and trigger: Turn a Linear label into a PR.

Why run it on Rush?

This migration needs two interpreters and a running Redis at the same moment: Python 3.11 to show the import error, the project's 3.9 to compare old and new behaviour, and a server for pub/sub to mean anything. The run sets all three up in its own sandbox and removes them with it. On a laptop that is an afternoon of version managers and a Redis left running in the background.

The run also keeps what it saw. The transcript holds each install, the resolver's refusal of the async-timeout pin and the test output on both libraries, and the pull request quotes them. A reviewer who doubts that the message shape is unchanged can read the two outputs next to each other.

Frequently asked questions

What causes TypeError: duplicate base class TimeoutError?

aioredis 2.0.1 defines its TimeoutError with both asyncio.TimeoutError and the built-in TimeoutError as bases. From Python 3.11 those two names are the same class, so the class statement fails at import. No newer aioredis release fixes it; the fix is to import from redis.asyncio.

Is redis.asyncio the same API as aioredis 2?

For the calls most apps make, yes: from_url, Redis, pubsub(), publish, subscribe and get_message keep their names and arguments. The differences the agent looks for are deprecations added since, such as close() giving way to aclose(). An app still on aioredis 1.x has a larger job, because 2.0 replaced the pool and command classes.

Which redis-py version should I use on Python 3.9?

The newest release whose requires_python admits it, which pip or Poetry selects for you. At the time of writing that is redis 7.0.1; 7.1.0 and later require Python 3.10. The agent reads this from the package metadata during the run and states it in the pull request.

Does the agent need my Redis credentials?

No. It tests against a Redis server it installs in the sandbox and connects with a local URL in the same form the app builds. Your hosted Redis, its host name and its password are never read, and the request names no connection.

More jobs like this: Turn a Linear label into a PR

Keep reading