AttributeError in errors.py when string response received instead of Response object
Author: jacobhessterCreated Nov 10, 2024Updated Apr 8, 2025
LabelsUnconfirmed Bug
Summary
When a string is received as a response instead of a Response object, the error handling in tweepy/errors.py fails with an AttributeError. The current error handling assumes if response.status_code fails, the response must be an aiohttp.ClientResponse, but it could also be a string.
Reproduction Steps
- Initialize tweepy client with a response type:
client = tweepy.Client(
consumer_key="...",
consumer_secret="...",
access_token="...",
access_token_secret="...",
return_type=requests.Response
)- Call create_tweet method:
response = client.create_tweet(text="test tweet")
Minimal Reproducible Example
import tweepy
import requests
# Initialize client
client = tweepy.Client(
consumer_key="YOUR_API_KEY",
consumer_secret="YOUR_API_SECRET",
access_token="YOUR_ACCESS_TOKEN",
access_token_secret="YOUR_ACCESS_SECRET",
return_type=requests.Response
)
def test_tweet():
try:
# Attempt to create tweet
response = client.create_tweet(text="Test tweet")
print(f"Response type: {type(response)}")
return response
except AttributeError as e:
print(f"AttributeError: {str(e)}")
# Show the actual response type when error occurs
print(f"Response type when error occurred: {type(response)}")
raise
if __name__ == "__main__":
test_tweet()Expected Results
When an invalid response type (like a string) is received, tweepy should:
- Either handle the string response gracefully OR
- Raise a clear, informative error message like:
TypeError: Expected requests.Response or aiohttp.ClientResponse object, got str
Actual Results
When receiving a string response, the error handling fails with a cascade of confusing AttributeErrors:
Traceback (most recent call last):
File "tweepy/errors.py", line 47, in __init__
status_code = response.status_code
AttributeError: 'str' object has no attribute 'status_code'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "...", line ..., in ...
status_code = response.status
AttributeError: 'str' object has no attribute 'status'
### Twitter API Access Plan
Free
### Tweepy Version
4.14.0
### Checklist
- [X] I have searched for duplicate issues.
- [X] If applicable, I have shown the entire traceback.
- [ ] If applicable, I have removed any visible credentials from my code and/or screenshots.
### Additional Context
_No response_Source: tweepy/tweepy