# Fix "AttributeError: module 'tweepy' has no attribute 'StreamListener'"

Yes. An agent ports a Tweepy 3 StreamListener to StreamingClient, maps each tweet field your code reads to the v2 payload, and tests it on recorded data.

[Run this on Rush](https://cloud.getrush.ai/run/fix-tweepy-streamlistener-attribute-error/tweepy/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

Tweepy 4.0 merged `StreamListener` into `Stream`, so a class declared as `class MyStreamListener(tweepy.StreamListener)` fails the moment the module is imported: `AttributeError: module 'tweepy' has no attribute 'StreamListener'`. Most answers stop at the rename: subclass `tweepy.Stream` and move the callbacks over. That advice has aged. A check of installed releases shows `Stream` present in 4.0.0 and absent from 4.14.0 onward, including the current 4.17.0, where the only streaming class left is `StreamingClient`. On a fresh install the rename turns one `AttributeError` into another.

Chulong-Li's Real-time Sentiment Tracking on Twitter project is a typical streaming script. Its `requirements.txt` lists `tweepy` with no version. In `dash_app/scraping.py`, line 16 declares `class MyStreamListener(tweepy.StreamListener)`, with a comment that links the Tweepy 3.4.0 streaming guide, and line 123 starts it with `tweepy.Stream(auth = api.auth, listener = myStreamListener)` followed by `filter(languages=["en"], track = settings.TRACK_WORDS)`. The `on_status` callback builds a row of thirteen values for each status, reading `status.user.location`, `status.user.followers_count`, `status.coordinates`, `status.retweet_count` and `status.favorite_count` among others and scoring the text with TextBlob, and inserts it into Postgres.

Porting that to `StreamingClient` changes more than a class name, and the agent writes the mapping down before it edits. Authentication becomes a single bearer token passed to the constructor, in place of the four values the script reads from a gitignored `credentials.py`. The `track` and `languages` arguments become a rule added with `add_rules(tweepy.StreamRule(...))`, and the callback's early return for retweets becomes the rule operator `-is:retweet`. `on_status` becomes `on_tweet`, and it receives a v2 tweet that carries only an id and text unless more is requested, so follower counts and locations need `expansions` and `user_fields` on `filter()` and arrive through `on_includes`, and the two counters move under `public_metrics`.

The agent cannot open a live stream: that takes a bearer token for an X developer app whose access level includes the filtered stream, and it is told not to ask for one. So it verifies in two steps that need no network. It imports the module on the current Tweepy to show the error is gone, and it feeds recorded v2 stream payloads, written as fixture files from the documented response shape, through `on_data`, asserting that each of the thirteen columns gets the value the old code would have stored. Database writes go to a throwaway table or a stub. The pull request says in its first lines that no tweet was received from the live service.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "This repository fails with AttributeError: module 'tweepy' has no attribute 'StreamListener'. Read the dependency files and report how tweepy is specified. In a clean virtual environment install the dependencies, report the installed tweepy version, and import each module that uses tweepy to capture the traceback. Print which streaming classes the installed tweepy exposes (Stream, StreamListener, StreamingClient). Do not pin tweepy to 3.x. Find every use of StreamListener, tweepy.Stream, OAuthHandler used for streaming, and filter(track=..., languages=...), in .py files and notebooks, with file and line. Port the streaming code to tweepy.StreamingClient: a bearer token read from an environment variable, stream rules built from the old track and language arguments (use rule operators such as -is:retweet where the old callback filtered in code), on_tweet and on_includes in place of on_status, on_errors and on_request_error in place of on_error. List every attribute the old callback read from the status object and, for each, the v2 field, expansion or metric that now carries it and the tweet_fields, user_fields and expansions the filter call must request; say plainly if a value has no v2 equivalent. Keep database and downstream code unchanged except for the names of the values it receives. Do not use real API credentials and do not connect to the API. Write tests that pass recorded v2 stream payloads to the client's on_data method and assert the values handed to storage, with the database stubbed. Leave any committed or ignored credential files untouched and do not print their contents. Open a pull request with the field mapping table, the test output, the environment variable the reader must set, and the API access level the filtered stream needs.",
  "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 rewrites the streaming module and adds a test file with a few recorded payloads. Its description is led by the mapping table: each value the old callback took from a status, where it comes from in a v2 tweet, and what must be requested for it to be present. Values with no equivalent are named, so nobody learns from a column of nulls a month later that a field stopped arriving.

The reviewer's first decision is not about code. `StreamingClient` talks to the v2 filtered stream, and the description states the access level that endpoint requires and the single environment variable the script now reads. If the project's developer account does not have that access, the code change is correct and the stream will still not run, and it is better to know that before merging.

The test output shows the callback producing the same row shape as before from v2 input. What it cannot show is a live connection, rate limiting or reconnect behaviour. A reviewer with a bearer token runs the script once against a narrow rule and checks that a row is written.

## Run it on every repository in your org

Stream collectors are rarely alone. A lab or a data team tends to have several, forked from one tutorial and broken on the same day by the same unpinned install. List them with a code search for `StreamListener` and send the request above once per repository from a script with an API key, each with a `request_id` that is a UUID v5 of the repository name, so running the script twice returns the run the first call started instead of starting another.

Each run works in its own sandbox with that repository's own callback and columns, and runs past the key's `max_concurrent` limit are accepted with status `queued` and start as slots free. Give the key a webhook and the handler receives a `run.completed` for each, with the session URL to read the pull request from.

## Why run it on Rush?

The risk in this port is data that changes shape quietly. A v2 tweet without the right `expansions` has no author object, and a collector that tolerates missing values will fill a table with empty locations and zero follower counts without raising anything. Guarding against that means enumerating every field the old code read and testing each against v2 input. An agent does the enumeration from the source, writes the tests, and runs them in a sandbox on the Tweepy release a new install gets, with the output kept in the session.

It also keeps your credentials out of the work. The run needs no API key and is told not to look for one, so a port that is mostly careful reading and test writing happens on a machine that holds nothing worth leaking. The one step that does need a token, a short live check, stays with the person who owns the developer account.

## Frequently asked questions

### Can I just subclass tweepy.Stream instead?

Only if you also pin Tweepy to an early 4.x release. In a check of installed versions, `tweepy.Stream` existed in 4.0.0 and was gone in 4.14.0, 4.16.0 and 4.17.0. An unpinned install gets a release where `StreamingClient` is the only streaming class.

### Can I pin tweepy to 3.10 and keep my code?

The import will work, since 3.10.0 still has `StreamListener`. Whether the stream then delivers anything depends on the old streaming endpoint still accepting your app's keys, which is outside the library's control. The request above ports the code instead of pinning.

### Where did status.user.followers_count go?

In v2 the tweet carries an `author_id`, and the user object arrives separately when `filter()` is called with `expansions=["author_id"]` and the `user_fields` you need, such as `public_metrics` and `location`. The agent lists every such field your callback read and the parameters that bring it back.

### How do track and languages translate to StreamingClient?

Into stream rules. A rule is a query string, so `track=["Facebook"]` with `languages=["en"]` becomes a rule such as `Facebook lang:en`, added once with `add_rules`. Rules are stored on the API side for the app, so the script should list and clear old ones before adding its own.

### Does the agent run the stream to check the port?

No. It has no credentials and the request forbids connecting. It tests the callback with recorded payloads, and the pull request states that a live check with your bearer token is still to be done.

## More Tweepy jobs

- [Fix 'API' object has no attribute 'search' After a Tweepy Upgrade](https://getrush.ai/run/fix-api-object-has-no-attribute-search/tweepy/)

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

- [Fix "AttributeError: type object 'Qt' has no attribute 'AlignCenter'" When Moving to PyQt6](https://getrush.ai/run/fix-type-object-qt-has-no-attribute-aligncenter/pyqt6/)
- [Fix "uninitialized constant Fixnum (NameError)" on Ruby 3.2 and later](https://getrush.ai/run/fix-uninitialized-constant-fixnum/ruby/)
- [Fix "AttributeError: 'TestCase' object has no attribute 'assertEquals'" on Python 3.12](https://getrush.ai/run/fix-unittest-assertequals-attribute-error/unittest/)
- [Fix "AttributeError: module 'unittest' has no attribute 'makeSuite'" on Python 3.13](https://getrush.ai/run/fix-unittest-makesuite-attribute-error/unittest/)
- [Fix "TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts"" in ts-node](https://getrush.ai/run/fix-unknown-file-extension-ts/ts-node/)

## 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/)
