From 10b8e1ec733c40f9359f66ad3dc7e69a9f7782e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Gil=20P=C3=A9rez?= Date: Sun, 19 Apr 2026 14:52:46 +0200 Subject: [PATCH] fix(find): drop hallucinated future-date filters from AI output MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Small-to-medium LLMs (qwen2.5:7b locally, Gemini 2.0 flash in #218) frequently echo the prompt's "Today's date is …" context back as a `takenAfter: ` filter even when the user query has no date reference. This silently filters out all results (photos cannot be taken in the future), surfacing as "No results found / Taken After 2026-04-19" in the UI. Two changes: 1. Tighten the prompt to explicitly say today's date should only be used to resolve relative expressions that actually appear in the query. 2. Guardrail: if the model still returns `takenAfter >= today` (or `takenBefore > today`), drop it. This is narrow and defensible — a future date literally cannot match any asset, so removing it never loses a valid match. No locale-specific regex, no guessing intent; just reject provably invalid filters. Repro with qwen2.5:7b via Ollama OpenAI-compat endpoint, exact prompt that parseFindQuery produces today: Input: "beach" Output: {"query":"beach","takenAfter":"2026-04-19"} With this patch the stale `takenAfter` is dropped before hitting Immich. Ref: #218 (same symptom reported with Gemini) --- src/helpers/ai.helper.ts | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/helpers/ai.helper.ts b/src/helpers/ai.helper.ts index c4ecf92..3930bed 100644 --- a/src/helpers/ai.helper.ts +++ b/src/helpers/ai.helper.ts @@ -67,10 +67,11 @@ const getOpenAICompatibleModel = () => { }; export const parseFindQuery = async (query: string): Promise => { + const today = new Date().toISOString().split("T")[0]; const prompt = [ `Parse the following query and return extracted filters as JSON: ${query}.`, "Do not include any information that is not intentionally provided in the query.", - `Today's date is ${new Date().toISOString().split("T")[0]}.`, + `Today's date is ${today}. Use it ONLY to resolve relative expressions like "last week" or "yesterday" that APPEAR IN THE QUERY. If no date-related word is in the query, DO NOT return takenAfter or takenBefore.`, "Dates must be in YYYY-MM-DD format.", "Return ONLY valid JSON object with keys: query, personIds, city, country, state, size, model, takenAfter, takenBefore, type.", "Do not use null values. Omit a key when it is not present in the query.", @@ -81,10 +82,9 @@ export const parseFindQuery = async (query: string): Promise => { model: getOpenAICompatibleModel(), prompt, output: Output.object({ - name:"json", + name: "json", schema: responseSchema, }), - }); const parsedResponse = JSON.parse(text) as FindQuery; @@ -94,9 +94,18 @@ export const parseFindQuery = async (query: string): Promise => { delete parsedResponse.type; } } - const cleanedResponse = { - ...parsedResponse - }; - return removeNullOrUndefinedProperties(cleanedResponse) as any as FindQuery; + // Guardrail against a common LLM failure mode: small-to-medium models + // (qwen2.5:7b, Gemini flash, etc.) often echo the prompt's "today" date as + // `takenAfter` even when the query has no date. A date in the future can + // never match a photo, so drop any takenAfter/takenBefore that is today or + // later — this keeps the filter purely corrective without guessing intent. + if (parsedResponse.takenAfter && parsedResponse.takenAfter >= today) { + delete parsedResponse.takenAfter; + } + if (parsedResponse.takenBefore && parsedResponse.takenBefore > today) { + delete parsedResponse.takenBefore; + } + + return removeNullOrUndefinedProperties(parsedResponse) as any as FindQuery; };