Fix: ensure failed CrawlResult always has proper links and media keys - #2297
Open
dajiaohuang wants to merge 1 commit into
Open
dajiaohuang wants to merge 1 commit into
dajiaohuang wants to merge 1 commit into
Conversation
- unclecode#2286: Use Field(default_factory=...) for media and links attributes so failed CrawlResult instances (robots.txt refusal, proxy failures, exceptions) always include the expected internal/external and images/videos/audios/tables keys instead of empty dicts, preventing KeyError on client access.
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.
When a crawl fails (robots.txt refusal, proxy failure, exception),
CrawlResultwas initialized without setting thelinksandmediaattributes. The class defaults were mutable empty dicts ({}), causingKeyErrorwhen clients accessedresult.links['internal']orresult.media['images']on failure results. Since failed results are now included in batch/crawlresponses (per #2134), every mixed batch with a failed URL produced inconsistent response shapes.Changes
mediaandlinksdefaults inCrawlResultfrom mutable{}toField(default_factory=...)returning the expected keyed structure:media:{'images': [], 'videos': [], 'audios': [], 'tables': []}links:{'internal': [], 'external': []}This ensures all CrawlResult instances (success and failure) have consistent shapes, avoiding the need for client-side KeyError handling and preventing downstream type errors when checking link counts.
Validation
After this change, accessing
.links['internal']and.media['images']on any CrawlResult instance returns an empty list rather than raising KeyError.