{"record":{"id":"9059f2c3a9b1f0b2","repo":"spring-projects/spring-ai","slug":"query-expansion-result-does-not-contain-the-reques","errorCode":null,"errorMessage":"Query expansion result does not contain the requested ${numberOfQueries} variants. Returning the input query unchanged.","messagePattern":"Query expansion result does not contain the requested (.+?) variants\\. Returning the input query unchanged\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"spring-ai-rag/src/main/java/org/springframework/ai/rag/preretrieval/query/expansion/MultiQueryExpander.java","lineNumber":120,"sourceCode":"\t\t}\n\n\t\tvar response = this.chatClient.prompt()\n\t\t\t.user(user -> user.text(this.promptTemplate.getTemplate())\n\t\t\t\t.param(\"number\", this.numberOfQueries)\n\t\t\t\t.param(\"query\", query.text()))\n\t\t\t.call()\n\t\t\t.content();\n\n\t\tif (response == null) {\n\t\t\tlogger.warn(\"Query expansion result is null. Returning the input query unchanged.\");\n\t\t\treturn List.of(query);\n\t\t}\n\n\t\tvar queryVariants = Arrays.asList(response.split(\"\\n\"));\n\n\t\tif (CollectionUtils.isEmpty(queryVariants) || this.numberOfQueries != queryVariants.size()) {\n\t\t\tif (logger.isWarnEnabled()) {\n\t\t\t\tlogger.warn(\"Query expansion result does not contain the requested \" + this.numberOfQueries\n\t\t\t\t\t\t+ \" variants. Returning the input query unchanged.\");\n\t\t\t}\n\t\t\treturn List.of(query);\n\t\t}\n\n\t\tvar queries = queryVariants.stream()\n\t\t\t.filter(StringUtils::hasText)\n\t\t\t.map(queryText -> query.mutate().text(queryText).build())\n\t\t\t.collect(Collectors.toList());\n\n\t\tif (this.includeOriginal) {\n\t\t\tlogger.debug(\"Including the original query in the result\");\n\t\t\tqueries.add(0, query);\n\t\t}\n\n\t\treturn queries;\n\t}\n","sourceCodeStart":102,"sourceCodeEnd":138,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/spring-ai-rag/src/main/java/org/springframework/ai/rag/preretrieval/query/expansion/MultiQueryExpander.java#L102-L138","documentation":"After splitting the model response on newlines, MultiQueryExpander checks that the number of variants equals numberOfQueries; if the list is empty or the count mismatches, it warns and returns the original query unchanged. LLMs often return fewer, more, or merged lines, so strict count matching frequently trips.","triggerScenarios":"expand() -> response.split(\"\\n\") yields a list whose size differs from this.numberOfQueries (or is empty).","commonSituations":"Model returns numbered or blank-line-separated queries causing miscount; model returns 1 line instead of N; extra explanation lines around the queries; reasoning models returning prose.","solutions":["Relax expectations: newer versions remove strict count matching — upgrade spring-ai-rag.","Use a prompt template that demands exactly N lines with no numbering or extra text.","Post-filter blank lines yourself by supplying a custom prompt and parsing tolerant input.","Reduce numberOfQueries or test with a model that follows line-count instructions reliably."],"exampleFix":"// before — strict count triggers fallback often\n.prompt(DEFAULT_USER_TEXT)\n// \"Generate {number} search queries...\"\n\n// after — template that enforces one query per line, no numbering\nMultiQueryExpander.builder()\n    .chatClient(chatClient)\n    .prompt(\"Generate exactly {number} search queries, one per line, no numbering, no extra text for: {query}\")\n    .numberOfQueries(3)\n    .build();","handlingStrategy":"fallback","validationCode":"String response = model.call(prompt);\nlong lines = response == null ? 0 : Arrays.stream(response.split(\"\\n\")).filter(s -> !s.isBlank()).count();\nif (lines != expectedNumberOfQueries) logger.warn(\"Expansion returned {} of {} lines\", lines, expectedNumberOfQueries);","typeGuard":null,"tryCatchPattern":"List<Query> expanded = expander.expand(query);\nif (expanded.size() == 1 && expanded.get(0).equals(query)) {\n    logger.info(\"Expansion fallback; proceeding with original query\");\n}","preventionTips":["Prompt for exactly N lines with no numbering or commentary.","Upgrade spring-ai-rag if available (strict count check was relaxed).","Avoid reasoning-style models for expansion.","Keep numberOfQueries modest (2-5) so models comply."],"tags":["rag","query-expansion","prompt"],"backgroundTag":"unexpected-api-response-shape","analyzedSha":"98a7beda4f29d80a71c5837eb4053b03a93a46f7","analyzedAt":"2026-09-11T14:15:49.441Z","contentChangedAt":"2026-09-11T14:15:49.441Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}