{"record":{"id":"4c3ed76770709a45","repo":"FoundationAgents/MetaGPT","slug":"query-cannot-be-empty-or-contain-only-whitespace","errorCode":null,"errorMessage":"Query cannot be empty or contain only whitespace.","messagePattern":"Query cannot be empty or contain only whitespace\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"metagpt/actions/search_enhanced_qa.py","lineNumber":147,"sourceCode":"            self._validate_query(query)\n\n            processed_query = await self._process_query(query, rewrite_query)\n            context = await self._build_context(processed_query)\n\n            return await self._generate_answer(processed_query, context)\n\n    def _validate_query(self, query: str) -> None:\n        \"\"\"Validate the input query.\n\n        Args:\n            query (str): The query to validate.\n\n        Raises:\n            ValueError: If the query is invalid.\n        \"\"\"\n\n        if not query.strip():\n            raise ValueError(\"Query cannot be empty or contain only whitespace.\")\n\n    async def _process_query(self, query: str, should_rewrite: bool) -> str:\n        \"\"\"Process the query, optionally rewriting it.\"\"\"\n\n        if should_rewrite:\n            return await self._rewrite_query(query)\n\n        return query\n\n    async def _rewrite_query(self, query: str) -> str:\n        \"\"\"Write a better search query for web search engine.\n\n        If the rewrite process fails, the original query is returned.\n\n        Args:\n            query (str): The original search query.\n\n        Returns:","sourceCodeStart":129,"sourceCodeEnd":165,"githubUrl":"https://github.com/FoundationAgents/MetaGPT/blob/11cdf466d042aece04fc6cfd13b28e1a70341b1f/metagpt/actions/search_enhanced_qa.py#L129-L165","documentation":"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.","triggerScenarios":"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.","commonSituations":"Web UI or CLI frontends passing unset input; chat pipelines forwarding an empty last message; whitespace-only user input not trimmed upstream.","solutions":["Trim and check the query in your caller before invoking SearchEnhancedQA; prompt the user for non-empty input.","Default empty queries to a sensible fallback question or skip the search step.","If the query is LLM-generated, validate the generation step returned non-empty text."],"exampleFix":"# before\nresult = await qa.run(query=user_input)  # user_input = '   '\n\n# after\nquery = user_input.strip()\nif not query:\n    raise ValueError('Please enter a question.')\nresult = await qa.run(query=query)","handlingStrategy":"validation","validationCode":"query = (query or '').strip()\nif not query:\n    raise ValueError('Please provide a non-empty question.')\nresult = await search_qa.run(query=query)","typeGuard":"def is_valid_query(q: str | None) -> TypeGuard[str]:\n    return isinstance(q, str) and len(q.strip()) > 0","tryCatchPattern":"try:\n    result = await qa.run(query=q)\nexcept ValueError:\n    q = fallback_question\n    result = await qa.run(query=q)","preventionTips":["Strip input at the UI/CLI boundary.","Never forward empty LLM-generated text into search.","Skip the search-enhanced step when no question is present."],"tags":["search","qa","validation","user-input"],"backgroundTag":null,"analyzedSha":"11cdf466d042aece04fc6cfd13b28e1a70341b1f","analyzedAt":"2026-08-14T23:20:02.994Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}