{"record":{"id":"8dd20be487770c4d","repo":"run-llama/llama_index","slug":"failed-to-select-query-engine","errorCode":null,"errorMessage":"Failed to select query engine","messagePattern":"Failed to select query engine","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"llama-index-core/llama_index/core/query_engine/router_query_engine.py","lineNumber":193,"sourceCode":"\n                    selected_query_engine = self._query_engines[engine_ind]\n                    responses.append(selected_query_engine.query(query_bundle))\n\n                if len(responses) > 1:\n                    final_response = combine_responses(\n                        self._summarizer, responses, query_bundle\n                    )\n                else:\n                    final_response = responses[0]\n            else:\n                try:\n                    selected_query_engine = self._query_engines[result.ind]\n                    log_str = f\"Selecting query engine {result.ind}: {result.reason}.\"\n                    logger.info(log_str)\n                    if self._verbose:\n                        print_text(log_str + \"\\n\", color=\"pink\")\n                except ValueError as e:\n                    raise ValueError(\"Failed to select query engine\") from e\n\n                final_response = selected_query_engine.query(query_bundle)\n\n            # add selected result\n            final_response.metadata = final_response.metadata or {}\n            final_response.metadata[\"selector_result\"] = result\n\n            query_event.on_end(payload={EventPayload.RESPONSE: final_response})\n\n        return final_response\n\n    async def _aquery(self, query_bundle: QueryBundle) -> RESPONSE_TYPE:\n        with self.callback_manager.event(\n            CBEventType.QUERY, payload={EventPayload.QUERY_STR: query_bundle.query_str}\n        ) as query_event:\n            result = await self._selector.aselect(self._metadatas, query_bundle)\n\n            if len(result.inds) > 1:","sourceCodeStart":175,"sourceCodeEnd":211,"githubUrl":"https://github.com/run-llama/llama_index/blob/afd0fef371831f9bda13e5af7167cf4e981278ab/llama-index-core/llama_index/core/query_engine/router_query_engine.py#L175-L211","documentation":"In RouterQueryEngine._query (sync), after the selector picks a query engine it indexes self._query_engines[result.ind]. If that lookup raises ValueError (index out of range because the LLM selector returned an invalid index), it is re-raised as 'Failed to select query engine' with the original as cause.","triggerScenarios":"Single-selection routing where the selector LLM returns result.ind outside 0..len(query_engines)-1 (e.g. returns 2 when only 2 engines exist, or a tool index misaligned with the choices list).","commonSituations":"Weak or non-instruct LLM that fails to follow the selector output format; a custom selector whose choices list order does not match the query_engines list; flaky structured-output parsing in the Pydantic/LLM selector.","solutions":["Use a stronger instruction-following LLM for routing (e.g. an OpenAI function-calling model with PydanticSingleSelector)","Pass an explicit selector: RouterQueryEngine(..., selector=get_selector_from_llm(llm)) with a reliable LLM, rather than relying on the default","Verify the QueryEngineTool list is small and well-differentiated so the selector's numbered choice is unambiguous","Catch ValueError and retry the query or fall back to a default engine"],"exampleFix":"// before\nrouter = RouterQueryEngine(selector=LLMSingleSelector.from_defaults(llm=weak_llm), query_engine_tools=tools)\nresp = router.query(q)\n\n// after\nfrom llama_index.core.selectors import PydanticSingleSelector\nrouter = RouterQueryEngine(\n    selector=PydanticSingleSelector.from_defaults(llm=strong_llm),\n    query_engine_tools=tools,\n)\nresp = router.query(q)","handlingStrategy":"retry","validationCode":"# Validate selector sanity before querying: choices must match engines\nassert router_engine._selector is not None\n# keep engine/tool counts aligned at construction time:\n# RouterQueryEngine(query_engine_tools=tools) builds both from the same list, prefer that API","typeGuard":null,"tryCatchPattern":"try:\n    resp = router_engine.query(q)\nexcept ValueError as e:\n    if \"Failed to select query engine\" in str(e) and e.__cause__ is not None:\n        resp = router_engine.query(q)  # one retry; selector output is nondeterministic\n    else:\n        raise","preventionTips":["Route with function-calling LLMs (PydanticSingleSelector) rather than text-parsing selectors","Prefer RouterQueryEngine.from_defaults(query_engine_tools=tools) so selector choices and engines stay aligned","Log selector results to spot invalid indices early"],"tags":["router","selector","llm-output","index-error"],"backgroundTag":null,"analyzedSha":"afd0fef371831f9bda13e5af7167cf4e981278ab","analyzedAt":"2026-08-15T05:42:58.429Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}