fix(models): default CrawlResult links and media to the scraped shape - #2289
Open
talelboussetta wants to merge 3 commits into
Open
talelboussetta wants to merge 3 commits into
talelboussetta wants to merge 3 commits into
Conversation
CrawlResult.links and .media defaulted to {}. Successful crawls fill them from
the scraper's result, but the results AsyncWebCrawler builds itself -- the
robots.txt refusal, "All proxies failed", the exception path in arun, the
anti-bot raw-HTML fallback -- never set them, so those results had no
"internal"/"external" or "images" keys: result.links["internal"] raised
KeyError, and the Docker API returned "links": {} for exactly the entries of a
batch a client has to handle.
Default both to the keys a scraped page gets -- {"internal": [], "external": []}
and {"images": [], "videos": [], "audios": []} ("tables" is moved out of media
into CrawlResult.tables on the success path) -- so every construction site is
covered, including future ones.
There was a problem hiding this comment.
Copilot review overview
🔵 Needs a closer look
The default-isolation test needs strengthening, and documentation must be updated.
Review effort: Lite
Findings: None
What changed in this PR
Updates CrawlResult defaults so failed results expose consistent links and media shapes.
Changes:
- Adds per-instance defaults for
linksandmedia. - Adds regression tests for shape, serialization, isolation, and explicit values.
| File | Review finding |
|---|---|
tests/unit/test_crawl_result_default_shape.py |
Moderate (1 vote): Test expectations should use fresh literals or explicitly verify nested containers are not shared. |
crawl4ai/models.py |
Nit (1 vote): Update user-facing documentation and schema snippets that still show {} defaults. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
…n test
The CrawlResult sketches in docs/md_v2/api/crawl-result.md,
docs/md_v2/core/crawler-result.md and the /execute_js docstring still showed
`= {}` for links and media. The isolation test now also asserts the nested
lists are distinct objects, and compares against fresh literals.
complete-sdk-reference.md repeats the CrawlResult sketch from
docs/md_v2/api/crawl-result.md and still showed `= {}` for links and media.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #2286
CrawlResult.linksand.mediadefaulted to{}, so every result built without them (the robots.txt refusal, "All proxies failed", the exception path inarun, the anti-bot raw-HTML fallback) had nointernal/external/imageskeys. They now default to the keys a scraped page already gets:{"internal": [], "external": []}and{"images": [], "videos": [], "audios": []}(arunmovesmedia'stablestoCrawlResult.tableson the success path, so it is not part of the shape). Changing the default covers every construction site, including ones added later, instead of patching fourCrawlResult(...)calls.Behaviour change to be aware of: a failed result's
linksis now{"internal": [], "external": []}rather than{}, soif result.links:is truthy for it. Iterating over it is unaffected.List of files changed and why
crawl4ai/models.py:default_factoryforlinksandmedia.tests/unit/test_crawl_result_default_shape.py(new): a result built without links/media has the scraped shape; the default is not shared between instances;model_dump()keeps the keys; explicit values are kept.docs/md_v2/api/crawl-result.md,docs/md_v2/core/crawler-result.md, and theCrawlResultsketch in/execute_js's docstring (deploy/docker/server.py): show the new defaults.How Has This Been Tested?
The offline suites (
deploy/docker/testswithout the live-servertest_1–test_7scripts,tests/unit, the pool tests) show no new failures againstdevelop.End to end, on an image built from
develop@ 1f68e5b:/crawlwithcheck_robots_txt: trueand two URLs of a local test site, soCRAWL4AI_ALLOW_INTERNAL_URLS=true, one disallowed by its robots.txt. Before, and with this branch'smodels.pymounted over the installed package:cc @nightcityblade @ntohidi
Checklist: