Skip to content

fix(ai): add Joi schema validation for Gemini API responses#276

Open
akshara200829-lgtm wants to merge 1 commit into
Canopus-Labs:mainfrom
akshara200829-lgtm:fix/ai-response-schema-validation
Open

fix(ai): add Joi schema validation for Gemini API responses#276
akshara200829-lgtm wants to merge 1 commit into
Canopus-Labs:mainfrom
akshara200829-lgtm:fix/ai-response-schema-validation

Conversation

@akshara200829-lgtm

Copy link
Copy Markdown

📝 Pull Request Description

Related Issue

Closes #245

Summary

The AI endpoints (generate-questions and generate-explanation) were parsing Gemini responses using JSON.parse() but not validating the structure of the returned JSON before sending it to the frontend. This meant incomplete or unexpected Gemini responses could silently pass through and cause rendering issues on the frontend. This PR adds Joi schema validation after parsing to ensure only correctly structured responses reach the client.


Type of Change

  • 🐛 Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature
  • ♻️ Refactoring
  • 📝 Documentation update
  • 🎨 UI/UX improvement
  • 🔥 Other(please describe) ______

How Has This Been Tested?

  • Verified that const Joi = require("joi") is correctly imported and Joi is already listed as a dependency in package.json
  • Confirmed questionsSchema validates that each item in the questions array contains both question and answer as required strings
  • Confirmed explanationSchema validates that the explanation response contains both title and explanation as required strings
  • Verified that invalid responses return 500 with a clear "Invalid AI response format" message and details
  • Verified that valid responses continue to flow through normally with no breaking changes
  • generateInterviewTips is intentionally left unchanged as it was not mentioned in the issue scope

Screenshots (if applicable)

N/A


Checklist

  • My code follows the project's guidelines
  • I have tested my changes
  • I have updated documentation where necessary
  • I have linked the related issue
  • My changes do not introduce new warnings or errors

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.

[Bug] AI endpoints accept unvalidated Gemini JSON responses causing inconsistent API output

1 participant