From 132973039cc4eb029115c46b2c0954d4e20fe74b Mon Sep 17 00:00:00 2001 From: im4saken <280051114+im4saken@users.noreply.github.com> Date: Mon, 18 May 2026 21:54:38 +0800 Subject: [PATCH] fix(gateway): constrain query expansion JSON key to "queries" The expansion prompt asks the model to "Rewrite the search query below into 3-4 different, related queries" without naming the JSON key. On OpenAI-compatible endpoints that don't enforce a strict JSON schema server-side (e.g. DeepSeek, many self-hosted gateways), the model picks the prompt-salient noun and emits {"rewrites": [...]}, which fails ExpansionSchema ({ queries: string[] }) validation. The catch block only warns for AIConfigError, so the schema-validation failure silently falls back to [query] and expansion is effectively disabled. Verified on two providers: oMLX serving Qwen3.6-35B-A3B-6bit at http://127.0.0.1:8888/v1 and deepseek-v4-flash at https://api.deepseek.com/v1. With the prompt constraint, both return {"queries": [...]} and gbrain query latency increases by ~150 ms (the expansion inference), confirming expansion now runs end-to-end. Refs #1156 --- src/core/ai/gateway.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/ai/gateway.ts b/src/core/ai/gateway.ts index e65b08948..824ba1f52 100644 --- a/src/core/ai/gateway.ts +++ b/src/core/ai/gateway.ts @@ -1624,7 +1624,7 @@ export async function expand(query: string): Promise { model, schema: ExpansionSchema, prompt: [ - 'Rewrite the search query below into 3-4 different, related queries that would help find relevant documents.', + 'Rewrite the search query below into 3-4 different, related queries that would help find relevant documents. Respond with a JSON object in exactly this shape: {"queries": ["rewrite1", "rewrite2", "rewrite3"]}. The JSON key MUST be exactly "queries" (not "rewrites" or any other variation).', 'Return ONLY the JSON object. Do NOT include the original query in the result.', 'Each rewrite should emphasize different aspects, synonyms, or framings.', '',