crawl4ai icon indicating copy to clipboard operation
crawl4ai copied to clipboard

[Bug]: arun() and arun_many() type hinting needs fixing

Open luzede opened this issue 7 months ago • 7 comments

crawl4ai version

0.8.6

Expected Behavior

Something like this

Image And the docstring comments also say that it returns CrawlResult Image

Current Behavior

Image

Is this reproducible?

Yes

Inputs Causing the Bug

RunManyReturn = Union[
    CrawlResultContainer[CrawlResultT],
    AsyncGenerator[CrawlResultT, None]
]

`arun()` is not an async generator, maybe you guys give it generator properties through CrawlResultContainer but the typing needs fixing.

Steps to Reproduce

I am using Pylance, I tried Pyrefly from meta too.

Code snippets

import asyncio
from crawl4ai import *

async def main():
    async with AsyncWebCrawler() as crawler:
        result = await crawler.arun(
            url="https://www.nbcnews.com/business",
        )
        print(result.markdown)

if __name__ == "__main__":
    asyncio.run(main())

OS

Windows

Python version

3.13.11

Browser

No response

Browser version

No response

Error logs & Screenshots (if applicable)

No response

luzede avatar Apr 05 '26 07:04 luzede

Fixed in PR #1901. arun() was annotated as RunManyReturn (includes AsyncGenerator) but always returns CrawlResultContainer. Changed to CrawlResultContainer — Pylance/Pyright should stop flagging result.markdown as an error.

Also added test_type_annotations.py — static analysis tests that catch this class of bug without needing pyright in CI. A full pyright scan found 46 return type mismatches across the codebase; this PR fixes the user-facing one and guards against future regressions.

pip install git+https://github.com/hafezparast/crawl4ai.git@fix/maysam-arun-type-hint-1898

hafezparast avatar Apr 06 '26 13:04 hafezparast

There is similar issue for arun_many() too.

luzede avatar Apr 06 '26 13:04 luzede

Hi, I'd like to work on this. I'll submit a PR shortly.

nightcityblade avatar Apr 08 '26 15:04 nightcityblade

@luzede Fixed in PR #1901 and merged into the develop branch. arun() now correctly returns CrawlResultContainer instead of RunManyReturn, so Pylance/Pyright will no longer flag result.markdown as an error. Will be in the next release.

ntohidi avatar Apr 24 '26 16:04 ntohidi

@ntohidi I reviewed that pr, it does not really solve it because CrawlResultContainer does not inherit CrawlResult class attributes, and does not have access to those. Also same issue on the arun_many(). The typing needs overhaul. I would make a pr for it but I don't know what the end result of arun() and arun_many() should be, the container from what I understand gives array like attributes to a crawlresult, maybe it is necessary maybe it is an extra that should be removed.

I would change both the typing and what the functions actually return.

luzede avatar Apr 24 '26 16:04 luzede

@luzede Good catch — you're right that PR #1901 didn't fully solve it. CrawlResultContainer uses __getattr__ which is invisible to type checkers.

PR #1957 fixes this properly: arun() now returns CrawlResult directly instead of wrapping in CrawlResultContainer. Pylance/Pyright will see .markdown, .html, and all other attributes correctly.

arun_many() still uses CrawlResultContainer — that's a separate issue since it genuinely needs list-like behavior. We'll address that separately if needed.

ntohidi avatar May 05 '26 06:05 ntohidi

This has been fixed and is available in the latest release, v0.9.1.

ntohidi avatar Jul 08 '26 14:07 ntohidi