Skip to content

fix(adaptive-crawler): Skip transient static failures in rendering type detection - #2438

Open
chalakbilla wants to merge 5 commits into
apify:masterfrom
chalakbilla:fix/adaptive-static-failure-detection
Open

chalakbilla wants to merge 5 commits into
apify:masterfrom
chalakbilla:fix/adaptive-static-failure-detection

Conversation

@chalakbilla

@chalakbilla chalakbilla commented Nov 11, 2026 •

Copy link
Copy Markdown

Description

When the static sub-crawler fails during rendering type detection because of a transient infrastructure error (connection error, timeout, proxy failure), static_run.result evaluates to None and the request was previously labelled as 'client only'. That label sticks in the predictor and lowers the detection probability recommendation for similar URLs, routing them through the heavier browser path even though the failure was unrelated to the page.

This change:

  • Treats transient static failures (connection errors, timeouts, ProxyError, RequestThrottledError, and asyncio.TimeoutError on Python 3.10) as inconclusive: the predictor is not trained (store_result is not called) and a debug message is logged.
  • Keeps blocks (SessionError) and HTTP status errors (such as 401/403/404) as 'client only', because when the browser succeeds they are genuine evidence the page needs client-side rendering.
  • Counts errors raised by the request handler as 'client only'. The only inconclusive handler-side case is the timeout of the whole static run.
  • Keeps the static success comparison logic unchanged.

Issues

Testing

Added a parameterized integration test (test_detection_run_static_fetch_failure) that feeds realistic static failures (connection error, timeout, ProxyError, 401/404 responses) and checks whether store_result is called and with which label.

Checklist

  • CI passed

@janbuchar
janbuchar self-requested a review November 12, 2026 11:05
@vdusek vdusek changed the title fix(adaptive-crawler): do not train rendering type predictor on transient static failure fix: Skip transient static failures in rendering type detection Nov 17, 2026
@vdusek vdusek changed the title fix: Skip transient static failures in rendering type detection fix(adaptive-crawler): Skip transient static failures in rendering type detection Nov 17, 2026

@vdusek vdusek left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR description needs a fix. It has broken control characters where backticks were (\tests/..., ^Gsyncio), it mentions a _TRANSIENT_STATIC_ERRORS constant that is not in the code, and the issue link should have a colon (Closes: #2096).

✍️ Drafted by Claude Code

exception = exception.wrapped_exception

transient_errors: tuple[type[Exception], ...] = (
SessionError,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important: In the static pipeline, SessionError means the site blocked the HTTP client (a blocked status code or a block detected in the content). Line 534 also skips every ContextPipelineInitializationError, including a 403 wrapped as HttpClientStatusCodeError. When the browser run then succeeds, that is a repeatable signal for client only, not a transient failure. With no stored results, DefaultRenderingTypePredictor.predict() keeps returning detection_probability_recommendation=1, so on a site that blocks plain HTTP every request runs both crawls. A local repro with 6 URLs, a static run that always gets 403, and a browser run that succeeds: this PR stores 0 results and detection stays at 1; master stores 4 and detection drops to 0.1. Could you treat only fetch failures as inconclusive (connection errors, timeouts, ProxyError, RequestThrottledError) and keep blocks and status code errors as client only?

✍️ Drafted by Claude Code

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Blocks (SessionError) and status code errors now count as client only. With your repro, 4 results are stored and detection drops to 0.1.

The rest of ContextPipelineInitializationError is still inconclusive, because the HTTP clients don't raise builtin ConnectionError. A reset shows up as impit.ConnectError or httpx2.ConnectError, so listing exception types would bring the original bug back. If the parser or a pre-nav hook fails on every static page, detection never drops, but that's broken enough to show up in the logs anyway.

transient_errors: tuple[type[Exception], ...] = (
SessionError,
RequestThrottledError,
TimeoutError,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important: On Python 3.10, asyncio.TimeoutError is not a subclass of TimeoutError. The handler timeout raised by wait_for in _crawl_one is still stored as client only there, and test_transient_failure_skips_rendering_type_detection[asyncio-timeout-error] fails on 3.10 (checked locally; 3.10 is in the CI matrix). Please add asyncio.TimeoutError to the tuple.

✍️ Drafted by Claude Code

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. The new test passes on 3.10.

TimeoutError,
ConnectionError,
)
if 'UserHandlerTimeoutError' in globals():

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: 'UserHandlerTimeoutError' in globals() is always true, because the class is imported at the top of the module. Please drop the check and define the tuple once as a module-level constant that includes UserHandlerTimeoutError.

✍️ Drafted by Claude Code

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gone, together with UserHandlerTimeoutError.

return True

if isinstance(exception, RequestHandlerError):
exception = exception.wrapped_exception

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: The docstring says that an exception raised by the request handler itself is evidence that the page needs a browser. After this unwrap, a UserHandlerTimeoutError, ConnectionError or RequestThrottledError raised in the handler counts as inconclusive. Which behavior is intended? The docstring and the code should match.

✍️ Drafted by Claude Code

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RequestHandlerError isn't unwrapped anymore, so anything the handler raises counts as client only, as the docstring says. The only handler-side case still inconclusive is the timeout of the whole static run from _crawl_one.

crawler, predictor = _make_crawler_and_predictor()
pw_result = RequestHandlerRunResult(key_value_store_getter=AsyncMock())

async def mock_crawl_one(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: The four unit tests repeat this mock_crawl_one stub, and the new integration tests already cover the same cases. Could you merge them into one parametrized test that patches _crawl_one with AsyncMock(side_effect=...), or drop them?

✍️ Drafted by Claude Code

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dropped them. The integration tests are now one parametrized test that feeds http_client.crawl realistic outcomes: an impit.ConnectError, a timeout, a ProxyError, a 403 and a 404.

…-failure-detection

# Conflicts:
#	src/crawlee/crawlers/_adaptive_playwright/_adaptive_playwright_crawler.py
@janbuchar
janbuchar requested a review from vdusek December 4, 2026 13:14

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rendering type detection treats a failed static run as 'client only'

3 participants