Skip to content

Pass router as request_handler to crawler - is this intended? #1600

Description

@vdusek

For example:

from crawlee.crawlers import PlaywrightCrawler
from crawlee.router import Router

router = Router[PlaywrightCrawlingContext]()

# ...

crawler = PlaywrightCrawler(
    request_handler=router,
)

Shouldn't the arg be named router?

We would also need to update its type and this part of the BasicCrawler's init:

# Request router setup
self._router: Router[TCrawlingContext] | None = None
if isinstance(cast('Router', request_handler), Router):
    self._router = cast('Router[TCrawlingContext]', request_handler)
elif request_handler is not None:
    self._router = None
    self.router.default_handler(request_handler)

Activity

  1. added
    t-toolingIssues with this label are in the ownership of the tooling team.
    on Dec 5, 2025
  2. added this to the 2.0 milestone on Dec 5, 2025
  3. janbuchar commented on Dec 5, 2025

    @janbuchar
    Collaborator

    It is intended, but it can be re-evaluated for sure. Originally I wanted to be as permissive as possible and accept even simple callables in the request_handler.

  4. vdusek commented on Dec 6, 2025

    @vdusek
    CollaboratorAuthor

    I understand, and we can keep the current behavior if it makes sense. But I believe the name should reflect the most common use case, which is registering a router. Registering a single handler can be done without passing it as an argument, and throughout our documentation, we consistently use the default router's request-handler decorator for that purpose. So I believe most people will do just that.

  5. janbuchar commented on Dec 6, 2025

    @janbuchar
    Collaborator

    Well, I believe the most common case is that people just use the default router and never touch this parameter:

    crawler = BasicCrawler()
    
    @crawler.router.default_handler
    async def handle(context: BasicCrawlingContext):
        ...
  6. vdusek commented on Dec 8, 2025

    @vdusek
    CollaboratorAuthor

    Yes, I agree - registering the handler through the router's default handler is the most common usage. And right after that would come using the router instance and passing it as an argument. And only as the least common would be passing the handler itself directly as an argument. So we should name the argument according to its most common usage pattern.

  7. self-assigned this
    on Dec 17, 2025
  8. removed their assignment
    on Dec 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    t-toolingIssues with this label are in the ownership of the tooling team.

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions