Skip to content

Enforce JSON schema for Gemini responses#82

Open
JoaoPauloSPloger wants to merge 2 commits intocalesthio:masterfrom
JoaoPauloSPloger:master
Open

Enforce JSON schema for Gemini responses#82
JoaoPauloSPloger wants to merge 2 commits intocalesthio:masterfrom
JoaoPauloSPloger:master

Conversation

@JoaoPauloSPloger
Copy link
Copy Markdown

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:

  • Added responseMimeType: "application/json" and a detailed responseSchema to the generationConfig in the GeminiProvider class within lib/llm/gemini.mjs, specifying that the model must return an array of objects with required fields such as title, type, ticker, confidence, rationale, risk, horizon, and signals.

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
Copilot AI review requested due to automatic review settings March 27, 2026 18:30
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 responseMimeType to request JSON responses from Gemini.
  • Added a JSON responseSchema requiring an array of trade-idea objects with specific required fields.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 24 to 45
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"]
}
}
},
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment on lines 24 to +29
generationConfig: {
maxOutputTokens: opts.maxTokens || 4096,
// Trava estrutural para garantir o JSON nativo
responseMimeType: "application/json",
responseSchema: {
type: "ARRAY",
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
contents: [{ parts: [{ text: userMessage }] }],
generationConfig: {
maxOutputTokens: opts.maxTokens || 4096,
// Trava estrutural para garantir o JSON nativo
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Suggested change
// Trava estrutural para garantir o JSON nativo
// Structural safeguard to enforce native JSON output

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants