From ca060c9d58d5f7f9733181e1bbe4e0775f6e5943 Mon Sep 17 00:00:00 2001 From: Valentin Nazarov Date: Sun, 1 Mar 2026 14:39:29 +0300 Subject: [PATCH 1/2] fix(playwright): dispose of APIResponse body Dispose of Playwright's APIResponse body, otherwise it stays in memory until the context closes. If not done, this can lead to excessive memory usage during crawling when using `send_request` within a handler. --- src/crawlee/crawlers/_playwright/_types.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/crawlee/crawlers/_playwright/_types.py b/src/crawlee/crawlers/_playwright/_types.py index 01721bf373..ebc56b805a 100644 --- a/src/crawlee/crawlers/_playwright/_types.py +++ b/src/crawlee/crawlers/_playwright/_types.py @@ -3,13 +3,14 @@ from dataclasses import dataclass from typing import TYPE_CHECKING, Literal, Protocol, TypedDict +from playwright.async_api import APIResponse, Response + from crawlee import HttpHeaders from crawlee._utils.docs import docs_group if TYPE_CHECKING: from collections.abc import AsyncGenerator - from playwright.async_api import APIResponse, Response from typing_extensions import NotRequired, Self @@ -56,6 +57,9 @@ async def from_playwright_response(cls, response: Response | APIResponse, protoc # Used http protocol version cannot be obtained from `Response` and has to be passed as additional argument. http_version = protocol _content = await response.body() + # If not called then the body will stay in memory until the context closes. + if isinstance(response, APIResponse): + await response.dispose() return cls(http_version=http_version, status_code=status_code, headers=headers, _content=_content) From 5ad4de603c57e9261568873cfe29ccf1311694a0 Mon Sep 17 00:00:00 2001 From: Valentin Nazarov Date: Mon, 2 Mar 2026 12:17:22 +0300 Subject: [PATCH 2/2] fix: optimize imports --- src/crawlee/crawlers/_playwright/_types.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/crawlee/crawlers/_playwright/_types.py b/src/crawlee/crawlers/_playwright/_types.py index ebc56b805a..393e6f5a8d 100644 --- a/src/crawlee/crawlers/_playwright/_types.py +++ b/src/crawlee/crawlers/_playwright/_types.py @@ -3,7 +3,7 @@ from dataclasses import dataclass from typing import TYPE_CHECKING, Literal, Protocol, TypedDict -from playwright.async_api import APIResponse, Response +from playwright.async_api import APIResponse from crawlee import HttpHeaders from crawlee._utils.docs import docs_group @@ -11,6 +11,7 @@ if TYPE_CHECKING: from collections.abc import AsyncGenerator + from playwright.async_api import Response from typing_extensions import NotRequired, Self