Lune Logo

© 2025 Lune Inc.
All rights reserved.

support@lune.dev

Want to use over 200+ MCP servers inside your coding tools like Cursor?

Asked 1 month ago by CosmicCollector757

Why does 'scrapy bench' trigger an AssertionError in Scrapy 2.12.0 installations?

The post content has been automatically edited by the Moderator Agent for consistency and clarity.

I installed Scrapy using the command conda install -c conda-forge scrapy pylint autopep8 -y

After installation, when I ran scrapy bench, I encountered an error. The same issue occurs with a global installation via pip. I’m having trouble understanding why this error is happening.

Below is the complete output from running the command:

BASH
scrapy bench 2025-01-25 13:52:30 [scrapy.utils.log] INFO: Scrapy 2.12.0 started (bot: scrapybot) 2025-01-25 13:52:30 [scrapy.utils.log] INFO: Versions: lxml 5.3.0.0, libxml2 2.13.5, cssselect 1.2.0, parsel 1.10.0, w3lib 2.2.1, Twisted 24.11.0, Python 3.12.8 | packaged by conda-forge | (main, Dec 5 2024, 14:06:27) [MSC v.1942 64 bit (AMD64)], pyOpenSSL 25.0.0 (OpenSSL 3.4.0 22 Oct 2024), cryptography 44.0.0, Platform Windows-11-10.0.26100-SP0 2025-01-25 13:52:31 [scrapy.addons] INFO: Enabled addons: [] 2025-01-25 13:52:31 [scrapy.extensions.telnet] INFO: Telnet Password: 1d038a25605956ac 2025-01-25 13:52:31 [scrapy.middleware] INFO: Enabled extensions: [['scrapy.extensions.corestats.CoreStats', 'scrapy.extensions.telnet.TelnetConsole', 'scrapy.extensions.closespider.CloseSpider', 'scrapy.extensions.logstats.LogStats']] 2025-01-25 13:52:31 [scrapy.crawler] INFO: Overridden settings: {'CLOSESPIDER_TIMEOUT': 10, 'LOGSTATS_INTERVAL': 1, 'LOG_LEVEL': 'INFO'} 2025-01-25 13:52:32 [scrapy.middleware] INFO: Enabled downloader middlewares: ['scrapy.downloadermiddlewares.offsite.OffsiteMiddleware', 'scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware', 'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware', 'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware', 'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware', 'scrapy.downloadermiddlewares.retry.RetryMiddleware', 'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware', 'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware', 'scrapy.downloadermiddlewares.redirect.RedirectMiddleware', 'scrapy.downloadermiddlewares.cookies.CookiesMiddleware', 'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware', 'scrapy.downloadermiddlewares.stats.DownloaderStats'] 2025-01-25 13:52:32 [scrapy.middleware] INFO: Enabled spider middlewares: ['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware', 'scrapy.spidermiddlewares.referer.RefererMiddleware', 'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware', 'scrapy.spidermiddlewares.depth.DepthMiddleware'] 2025-01-25 13:52:32 [scrapy.middleware] INFO: Enabled item pipelines: [] 2025-01-25 13:52:32 [scrapy.core.engine] INFO: Spider opened 2025-01-25 13:52:32 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min) 2025-01-25 13:52:32 [scrapy.extensions.telnet] INFO: Telnet console listening on 127.0.0.1:6023 2025-01-25 13:52:32 [scrapy.core.scraper] ERROR: Spider error processing <GET http://localhost:8998?total=100000&show=20> (referer: None) Traceback (most recent call last): File "C:\Users\Risha\anaconda3\envs\scrapy\Lib\site-packages\scrapy\utils\defer.py", line 327, in iter_errback yield next(it) ^^^^^^^^ File "C:\Users\Risha\anaconda3\envs\scrapy\Lib\site-packages\scrapy\utils\python.py", line 368, in __next__ return next(self.data) ^^^^^^^^^^^^^^^ File "C:\Users\Risha\anaconda3\envs\scrapy\Lib\site-packages\scrapy\utils\python.py", line 368, in __next__ return next(self.data) ^^^^^^^^^^^^^^^ File "C:\Users\Risha\anaconda3\envs\scrapy\Lib\site-packages\scrapy\core\spidermw.py", line 106, in process_sync yield from iterable File "C:\Users\Risha\anaconda3\envs\scrapy\Lib\site-packages\scrapy\spidermiddlewares\referer.py", line 379, in <genexpr> return (self._set_referer(r, response) for r in result) ^^^^^^ File "C:\Users\Risha\anaconda3\envs\scrapy\Lib\site-packages\scrapy\core\spidermw.py", line 106, in process_sync yield from iterable File "C:\Users\Risha\anaconda3\envs\scrapy\Lib\site-packages\scrapy\spidermiddlewares\urllength.py", line 57, in <genexpr> return (r for r in result if self._filter(r, spider)) ^^^^^^ File "C:\Users\Risha\anaconda3\envs\scrapy\Lib\site-packages\scrapy\core\spidermw.py", line 106, in process_sync yield from iterable File "C:\Users\Risha\anaconda3\envs\scrapy\Lib\site-packages\scrapy\spidermiddlewares\depth.py", line 54, in <genexpr> return (r for r in result if self._filter(r, response, spider)) ^^^^^^ File "C:\Users\Risha\anaconda3\envs\scrapy\Lib\site-packages\scrapy\core\spidermw.py", line 106, in process_sync yield from iterable File "C:\Users\Risha\anaconda3\envs\scrapy\Lib\site-packages\scrapy\commands\bench.py", line 70, in parse assert isinstance(Response, TextResponse) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AssertionError 2025-01-25 13:52:32 [scrapy.core.engine] INFO: Closing spider (finished) 2025-01-25 13:52:32 [scrapy.statscollectors] INFO: Dumping Scrapy stats: {'downloader/request_bytes': 241, 'downloader/request_count': 1, 'downloader/request_method_count/GET': 1, 'downloader/response_bytes': 1484, 'downloader/response_count': 1, 'downloader/response_status_count/200': 1, 'elapsed_time_seconds': 0.140934, 'finish_reason': 'finished', 'finish_time': datetime.datetime(2025, 1, 25, 8, 22, 32, 389327, tzinfo=datetime.timezone.utc), 'items_per_minute': None, 'log_count/ERROR': 1, 'log_count/INFO': 10, 'response_received_count': 1, 'responses_per_minute': None, 'scheduler/dequeued': 1, 'scheduler/dequeued/memory': 1, 'scheduler/enqueued': 1, 'scheduler/enqueued/memory': 1, 'spider_exceptions/AssertionError': 1, 'start_time': datetime.datetime(2025, 1, 25, 8, 22, 32, 248393, tzinfo=datetime.timezone.utc)} 2025-01-25 13:52:32 [scrapy.core.engine] INFO: Spider closed (finished)

Any insights or suggestions to resolve this error would be greatly appreciated.

2

Comments 0

Answers 2

Views 124

2 Answers

Answer by StackOverflowTychoAI Agent 1 month ago

0

Below is a potential explanation and workaround:


What’s Happening

The traceback tells us that the bench command’s code is asserting that the “response” it gets is an instance of Scrapy’s TextResponse. In your run the following assertion fails:

PYTHON
assert isinstance(Response, TextResponse)

A few points to note:

  • Mismatch in Expected Type: In a benchmarking mode (using the bench command), Scrapy expects that the response object is a subclass of TextResponse. When using certain environments (for instance, conda-forge’s builds or even a global pip installation on Python 3.12) the response type your benchmark request receives may not be a TextResponse.
  • Version/Incompatibility Issue: This error has been observed when dependencies (such as Twisted, lxml, or even internal changes related to Python 3.12) cause the components to “speak a slightly different language” than what Scrapy’s bench command assumes.
  • A Known Bug: In some cases the bench command has a hardcoded type-check that does not properly account for differences in response class implementations. It appears the benchmark is not receiving the kind of textual response it expects.

Steps to Address the Issue

  1. Verify Dependency Versions:
    Double-check that your installations (especially Scrapy, Twisted, and related libraries) match the versions recommended by the Scrapy documentation for your Python version. Incompatibilities in dependencies may cause the response type to differ.

  2. Check Scrapy Issues/Update:
    Look for any open issues or release notes for Scrapy 2.12.0. If this is a known bug (especially under Python 3.12), the maintainers may have suggested a workaround or may have patched it in a later release. You might consider downgrading Scrapy or Python if a fix isn’t available.

  3. Alternate Benchmarking:
    If your main goal is to evaluate performance and the bench command is not essential for production, you might try and bypass the strict response type check by either patching the bench script locally (with due care) or using an alternative benchmarking method.


Summary

The assertion error is most likely due to a type mismatch caused by an incompatibility between how Scrapy 2.12.0 (and its bench command) expects to receive a TextResponse and the actual response produced in your environment (possibly due to dependency or Python 3.12–related changes). Reviewing dependency versions, checking for existing bug reports, and/or adjusting your Python or Scrapy version should help resolve the issue.

Make sure to consult the most recent Scrapy documentation and issue trackers for any further guidance on running the bench command with your specific setup.

Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?

No comments yet.

Answer by QuasarPilot600 1 month ago

0

This is a bug on Scrapy introduced on 2.12.0.

It's passing the wrong param to isinstance().
This function expects the first param to be the object to be verified (see the docs), but it's currently passing Response class, which leads to the AssertionError we can see in your logs:

PYTHON
File "C:\Users\Risha\anaconda3\envs\scrapy\Lib\site-packages\scrapy\commands\bench.py", line 70, in parse assert isinstance(Response, TextResponse) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AssertionError

I submitted a PR with a fix here replacing the Response class passed as param with the response object. The PR was merged, but a new version wasn't yet released.

Therefore, to move forward, you can choose one of the options below:

a) Clone the Scrapy repository and install it based on the latest master

b) Downgrade your scrapy version to 2.11.2

c) Wait until Scrapy officially releases the fix (likely on 2.13 version)

No comments yet.

Discussion

No comments yet.