ScrapeGraphAI/Scrapegraph-ai · error · ValueError

Query must be a non-empty string

Error message

Query must be a non-empty string

What it means

ValueError from the pydantic validator on query in research_web: the search query must be a non-empty string. Empty strings, None, or non-string values are rejected before a web search runs.

Source

Thrown at scrapegraphai/utils/research_web.py:76

    serper_api_key: Optional[str] = Field(None, description="API key for Serper")
    region: Optional[str] = Field(None, description="Country/region code")
    language: str = Field("en", description="Language code")

    @validator("search_engine")
    def validate_search_engine(cls, v):
        """Validate search engine."""
        valid_engines = {"duckduckgo", "bing", "searxng", "serper"}
        if v.lower() not in valid_engines:
            raise ValueError(
                f"Search engine must be one of: {', '.join(valid_engines)}"
            )
        return v.lower()

    @validator("query")
    def validate_query(cls, v):
        """Validate search query."""
        if not v or not isinstance(v, str):
            raise ValueError("Query must be a non-empty string")
        return v

    @validator("max_results")
    def validate_max_results(cls, v):
        """Validate max results."""
        if v < 1 or v > 100:
            raise ValueError("max_results must be between 1 and 100")
        return v


# Define advanced PDF detection regex
PDF_REGEX = re.compile(r"\.pdf(#.*)?(\?.*)?$", re.IGNORECASE)


# Rate limiting decorator
def rate_limited(calls: int, period: int = 60):
    """
    Decorator to limit the rate of function calls.

View on GitHub (pinned to 532dfffbf6)

Solutions

  1. Default the query to a meaningful term when empty
  2. Validate user-supplied query strings before calling search
  3. Strip whitespace and reject blank queries early

Example fix

# before
params = {"query": user_input, ...}
# after
if not (query := user_input.strip()):
    raise ValueError("query required")
params = {"query": query, ...}
Defensive patterns

Strategy: validation

Validate before calling

if not isinstance(query, str) or not query.strip():
    raise ValueError("query required")

Type guard

def is_valid_query(q) -> bool:
    return isinstance(q, str) and len(q.strip()) > 0

Prevention

When it happens

Trigger: Passing query='' or query=None (or a non-str) when constructing the search/research parameter model.

Common situations: Programmatically generated queries where the template produced an empty string; forwarding user input without checking it.

Related errors


AI-assisted analysis of ScrapeGraphAI/Scrapegraph-ai@532dfffbf6 (2026-08-28). Data as JSON: /api/errors/37d5e98ae465716a. Report an issue: GitHub.