Skip to content

Commit 1414f1d

Browse files
Enforce JSON schema for Gemini responses
Add responseMimeType and a responseSchema to the Gemini provider's generationConfig to force structured JSON output. With this, it should be usable with gemini-2.5-flash
1 parent 53f6d81 commit 1414f1d

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

lib/llm/gemini.mjs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,25 @@ export class GeminiProvider extends LLMProvider {
2323
contents: [{ parts: [{ text: userMessage }] }],
2424
generationConfig: {
2525
maxOutputTokens: opts.maxTokens || 4096,
26+
// Trava estrutural para garantir o JSON nativo
27+
responseMimeType: "application/json",
28+
responseSchema: {
29+
type: "ARRAY",
30+
items: {
31+
type: "OBJECT",
32+
properties: {
33+
title: { type: "STRING", description: "Short title (max 10 words)" },
34+
type: { type: "STRING", enum: ["LONG", "SHORT", "HEDGE", "WATCH", "AVOID"] },
35+
ticker: { type: "STRING", description: "Primary instrument" },
36+
confidence: { type: "STRING", enum: ["HIGH", "MEDIUM", "LOW"] },
37+
rationale: { type: "STRING", description: "2-3 sentence explanation citing specific data" },
38+
risk: { type: "STRING", description: "Key risk factor" },
39+
horizon: { type: "STRING", description: "Intraday, Days, Weeks, or Months" },
40+
signals: { type: "ARRAY", items: { type: "STRING" } }
41+
},
42+
required: ["title", "type", "ticker", "confidence", "rationale", "risk", "horizon", "signals"]
43+
}
44+
}
2645
},
2746
}),
2847
signal: AbortSignal.timeout(opts.timeout || 60000),
@@ -45,4 +64,4 @@ export class GeminiProvider extends LLMProvider {
4564
model: this.model,
4665
};
4766
}
48-
}
67+
}

0 commit comments

Comments
 (0)