{"record":{"id":"8e7d535772cbb711","repo":"spring-projects/spring-ai","slug":"query-compression-result-is-null-empty-returning","errorCode":null,"errorMessage":"Query compression result is null/empty. Returning the input query unchanged.","messagePattern":"Query compression result is null/empty\\. 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/transformation/CompressionQueryTransformer.java","lineNumber":90,"sourceCode":"\n\t\tPromptAssert.templateHasRequiredPlaceholders(this.promptTemplate, \"history\", \"query\");\n\t}\n\n\t@Override\n\tpublic Query transform(Query query) {\n\t\tAssert.notNull(query, \"query cannot be null\");\n\n\t\tlogger.debug(\"Compressing conversation history and follow-up query into a standalone query\");\n\n\t\tvar compressedQueryText = this.chatClient.prompt()\n\t\t\t.user(user -> user.text(this.promptTemplate.getTemplate())\n\t\t\t\t.param(\"history\", formatConversationHistory(query.history()))\n\t\t\t\t.param(\"query\", query.text()))\n\t\t\t.call()\n\t\t\t.content();\n\n\t\tif (!StringUtils.hasText(compressedQueryText)) {\n\t\t\tlogger.warn(\"Query compression result is null/empty. Returning the input query unchanged.\");\n\t\t\treturn query;\n\t\t}\n\n\t\treturn query.mutate().text(compressedQueryText).build();\n\t}\n\n\tprivate String formatConversationHistory(List<Message> history) {\n\t\tif (history.isEmpty()) {\n\t\t\treturn \"\";\n\t\t}\n\n\t\treturn history.stream()\n\t\t\t.filter(message -> message.getMessageType().equals(MessageType.USER)\n\t\t\t\t\t|| message.getMessageType().equals(MessageType.ASSISTANT))\n\t\t\t.map(message -> \"%s: %s\".formatted(message.getMessageType(), message.getText()))\n\t\t\t.collect(Collectors.joining(\"\\n\"));\n\t}\n","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/spring-ai-rag/src/main/java/org/springframework/ai/rag/preretrieval/query/transformation/CompressionQueryTransformer.java#L72-L108","documentation":"CompressionQueryTransformer.transform asks the chat model to compress a conversation history plus query into a standalone query; when the returned content is null or blank it logs this warning and returns the input query unchanged. The pipeline continues with the un-compressed query instead of failing.","triggerScenarios":"transform() calls the compression prompt with history/query params and .call().content() returns null or whitespace (empty completion, content filter, or non-text response).","commonSituations":"Long histories with a low maxTokens budget yielding empty output; safety filters blocking the compressed output; misconfigured model; reasoning model returning no plain content.","solutions":["Increase maxTokens and verify the compression prompt template renders the history correctly.","Log the raw ChatResponse finish reason to see why content is empty.","Accept the fallback path — ensure callers can work with the original multi-turn query.","Switch to a model that reliably returns text for compression tasks."],"exampleFix":"// before\nCompressionQueryTransformer transformer = CompressionQueryTransformer.builder()\n    .chatClientBuilder(chatClient.mutate())\n    .build();\n\n// after — give the model room to answer\nCompressionQueryTransformer transformer = CompressionQueryTransformer.builder()\n    .chatClientBuilder(chatClient.mutate()\n        .defaultOptions(OpenAiChatOptions.builder().maxTokens(512).build()))\n    .build();","handlingStrategy":"fallback","validationCode":"// ensure the history isn't truncated to nothing\nif (query.history() == null || query.history().isEmpty()) {\n    logger.debug(\"No history to compress; transformer will pass query through\");\n}","typeGuard":null,"tryCatchPattern":"Query transformed = transformer.transform(query);\nif (transformed.text().equals(query.text()) && query.history() != null && !query.history().isEmpty()) {\n    logger.info(\"Compression fell back to original query; check model output\");\n}","preventionTips":["Raise maxTokens for the compression model.","Log ChatResponse finish reasons when content is empty.","Use an instruct (non-reasoning) model for compression.","Only apply the transformer when a meaningful history exists."],"tags":["rag","query-transformation","fallback"],"backgroundTag":"empty-api-response","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"}