Enforce JSON schema for Gemini responses#82
Enforce JSON schema for Gemini responses#82JoaoPauloSPloger wants to merge 2 commits intocalesthio:masterfrom
Conversation
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
There was a problem hiding this comment.
Pull request overview
This PR enforces structured JSON output from the Gemini provider by adding responseMimeType: "application/json" and a strict responseSchema to the request generationConfig.
Changes:
- Added
responseMimeTypeto request JSON responses from Gemini. - Added a JSON
responseSchemarequiring an array of trade-idea objects with specific required fields.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| generationConfig: { | ||
| maxOutputTokens: opts.maxTokens || 4096, | ||
| // Trava estrutural para garantir o JSON nativo | ||
| responseMimeType: "application/json", | ||
| responseSchema: { | ||
| type: "ARRAY", | ||
| items: { | ||
| type: "OBJECT", | ||
| properties: { | ||
| title: { type: "STRING", description: "Short title (max 10 words)" }, | ||
| type: { type: "STRING", enum: ["LONG", "SHORT", "HEDGE", "WATCH", "AVOID"] }, | ||
| ticker: { type: "STRING", description: "Primary instrument" }, | ||
| confidence: { type: "STRING", enum: ["HIGH", "MEDIUM", "LOW"] }, | ||
| rationale: { type: "STRING", description: "2-3 sentence explanation citing specific data" }, | ||
| risk: { type: "STRING", description: "Key risk factor" }, | ||
| horizon: { type: "STRING", description: "Intraday, Days, Weeks, or Months" }, | ||
| signals: { type: "ARRAY", items: { type: "STRING" } } | ||
| }, | ||
| required: ["title", "type", "ticker", "confidence", "rationale", "risk", "horizon", "signals"] | ||
| } | ||
| } | ||
| }, |
There was a problem hiding this comment.
There are unit tests for most other providers (e.g. test/llm-mistral.test.mjs) that assert the request body shape. Given this change alters the request payload and behavior significantly, please add a GeminiProvider unit test that verifies (a) the request includes responseMimeType/responseSchema when enabled and (b) the default call path doesn’t force an ideas-specific schema (once made opt-in), to prevent regressions.
| generationConfig: { | ||
| maxOutputTokens: opts.maxTokens || 4096, | ||
| // Trava estrutural para garantir o JSON nativo | ||
| responseMimeType: "application/json", | ||
| responseSchema: { | ||
| type: "ARRAY", |
There was a problem hiding this comment.
responseMimeType/responseSchema are being applied unconditionally for every complete() call. This makes the Gemini provider incompatible with other call sites that expect a different JSON shape (e.g. alert evaluation in lib/alerts/telegram.mjs expects a JSON object, not an array), and will likely cause parsing failures there. Consider making structured-output settings opt-in via opts (e.g. opts.responseSchema / opts.responseMimeType) or providing a separate method/helper specifically for the trade-ideas schema, leaving the default complete() behavior schema-free.
| contents: [{ parts: [{ text: userMessage }] }], | ||
| generationConfig: { | ||
| maxOutputTokens: opts.maxTokens || 4096, | ||
| // Trava estrutural para garantir o JSON nativo |
There was a problem hiding this comment.
The new inline comment is in Portuguese while the rest of the LLM provider files use English comments, which makes the file inconsistent to maintain for the broader team. Please translate it to English (and keep terminology consistent with other providers).
| // Trava estrutural para garantir o JSON nativo | |
| // Structural safeguard to enforce native JSON output |
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
Summary
This pull request enhances the Gemini LLM provider by enforcing structured JSON output for model responses. The main change is the addition of a strict response schema, which ensures that all generated outputs conform to a predefined format, improving reliability and downstream processing.
Structured response enforcement:
responseMimeType: "application/json"and a detailedresponseSchemato thegenerationConfigin theGeminiProviderclass withinlib/llm/gemini.mjs, specifying that the model must return an array of objects with required fields such astitle,type,ticker,confidence,rationale,risk,horizon, andsignals.