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
This commit is contained in:
im4saken
2026-05-18 21:54:38 +08:00
parent 61b79e7c99
commit 132973039c
+1 -1
View File
@@ -1624,7 +1624,7 @@ export async function expand(query: string): Promise<string[]> {
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.',
'',