FoundationAgents/MetaGPT · error · ValueError

Query cannot be empty or contain only whitespace.

Error message

Query cannot be empty or contain only whitespace.

What it means

SearchEnhancedQA validates its input query before doing web search + LM answer generation. _validate_query strips whitespace and raises ValueError when nothing remains, i.e. the query was '' or only spaces/tabs/newlines. This guards the downstream search engine call from meaningless empty queries.

Source

Thrown at metagpt/actions/search_enhanced_qa.py:147

            self._validate_query(query)

            processed_query = await self._process_query(query, rewrite_query)
            context = await self._build_context(processed_query)

            return await self._generate_answer(processed_query, context)

    def _validate_query(self, query: str) -> None:
        """Validate the input query.

        Args:
            query (str): The query to validate.

        Raises:
            ValueError: If the query is invalid.
        """

        if not query.strip():
            raise ValueError("Query cannot be empty or contain only whitespace.")

    async def _process_query(self, query: str, should_rewrite: bool) -> str:
        """Process the query, optionally rewriting it."""

        if should_rewrite:
            return await self._rewrite_query(query)

        return query

    async def _rewrite_query(self, query: str) -> str:
        """Write a better search query for web search engine.

        If the rewrite process fails, the original query is returned.

        Args:
            query (str): The original search query.

        Returns:

View on GitHub (pinned to 11cdf466d0)

Solutions

  1. Trim and check the query in your caller before invoking SearchEnhancedQA; prompt the user for non-empty input.
  2. Default empty queries to a sensible fallback question or skip the search step.
  3. If the query is LLM-generated, validate the generation step returned non-empty text.

Example fix

# before
result = await qa.run(query=user_input)  # user_input = '   '

# after
query = user_input.strip()
if not query:
    raise ValueError('Please enter a question.')
result = await qa.run(query=query)
Defensive patterns

Strategy: validation

Validate before calling

query = (query or '').strip()
if not query:
    raise ValueError('Please provide a non-empty question.')
result = await search_qa.run(query=query)

Type guard

def is_valid_query(q: str | None) -> TypeGuard[str]:
    return isinstance(q, str) and len(q.strip()) > 0

Try / catch

try:
    result = await qa.run(query=q)
except ValueError:
    q = fallback_question
    result = await qa.run(query=q)

Prevention

When it happens

Trigger: Calling SearchEnhancedQA.run/awith query='' , query=' ', or a query string built from an empty variable (e.g. user submitted an empty form field). Also triggers when the query comes from truncated LLM output that ended up blank.

Common situations: Web UI or CLI frontends passing unset input; chat pipelines forwarding an empty last message; whitespace-only user input not trimmed upstream.

Related errors


AI-assisted analysis of FoundationAgents/MetaGPT@11cdf466d0 (2026-08-14). Data as JSON: /api/errors/4c3ed76770709a45. Report an issue: GitHub.