assafelovic/gpt-researcher · error · ValueError

Percentage should be between 0 and 1

Error message

Percentage should be between 0 and 1

What it means

BrowserScraper._scroll_to_percentage(ratio) validates its scroll argument: ratio must be a float between 0 and 1, where 1 is the bottom of the page. Passing a percentage like 50, a negative value, or anything >1 raises ValueError('Percentage should be between 0 and 1') before the JavaScript scroll is executed.

Source

Thrown at gpt_researcher/scraper/browser/browser.py:255

            title = extract_title(soup)

        return text, image_urls, title

    def _scroll_to_bottom(self):
        """Scroll to the bottom of the page to load all content"""
        last_height = self.driver.execute_script("return document.body.scrollHeight")
        while True:
            self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
            time.sleep(2)  # Wait for content to load
            new_height = self.driver.execute_script("return document.body.scrollHeight")
            if new_height == last_height:
                break
            last_height = new_height

    def _scroll_to_percentage(self, ratio: float) -> None:
        """Scroll to a percentage of the page"""
        if ratio < 0 or ratio > 1:
            raise ValueError("Percentage should be between 0 and 1")
        self.driver.execute_script(f"window.scrollTo(0, document.body.scrollHeight * {ratio});")

    def _add_header(self) -> None:
        """Add a header to the website"""
        self.driver.execute_script(open(f"{FILE_DIR}/browser/js/overlay.js", "r").read())

View on GitHub (pinned to 6f998577d5)

Solutions

  1. Pass a fraction in [0, 1]: use 0.5 for 50%, 1.0 for the bottom of the page.
  2. If your config is in percent, divide by 100 before calling: ratio = pct / 100.
  3. Clamp user-supplied values: ratio = min(max(ratio, 0.0), 1.0).
  4. Default to 1.0 when the intended behavior is 'scroll to bottom'.

Example fix

# before
scraper._scroll_to_percentage(50)  # ValueError: Percentage should be between 0 and 1

# after
scraper._scroll_to_percentage(0.5)  # 50% of page height
# or clamp: scraper._scroll_to_percentage(min(max(pct / 100, 0.0), 1.0))
Defensive patterns

Strategy: validation

Validate before calling

ratio = min(max(float(ratio), 0.0), 1.0)
assert 0.0 <= ratio <= 1.0, 'scroll ratio must be in [0, 1]'

Type guard

def is_scroll_ratio(v) -> bool:
    return isinstance(v, (int, float)) and not isinstance(v, bool) and 0.0 <= v <= 1.0

Try / catch

try:
    scraper._scroll_to_percentage(ratio)
except ValueError:
    scraper._scroll_to_percentage(min(max(ratio, 0.0), 1.0))  # clamp and retry

Prevention

When it happens

Trigger: Calling scraper methods that scroll (e.g., scrape() with scroll config, or _scroll_to_percentage directly) with ratio passed as 0-100 percent (e.g., 0.5 intended as 50% is fine, but 50 is not), a negative number, or a value greater than 1.0.

Common situations: Configuring GPT Researcher's browser scroll settings with a 0-100 scale instead of 0-1 (passing 50 for 'halfway'); copy-pasted snippets using percent integers; passing None or a computed fraction that can go negative/out of range due to a math bug upstream.

Related errors


AI-assisted analysis of assafelovic/gpt-researcher@6f998577d5 (2026-08-28). Data as JSON: /api/errors/cd085c2dac6f5e72. Report an issue: GitHub.