fix: return structured JSON from AI routes so insights actually populate#101
Open
Harshit-Mishra2212 wants to merge 1 commit into
Open
Conversation
|
@Harshit-Mishra2212 is attempting to deploy a commit to the SEETA's projects Team on Vercel. A member of the Team first needs to authorize it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📌 Description
Both /api/insights and /api/ai returned data in a shape that utils.ts could never use, causing the AI insights panel to always silently render empty despite the API calls succeeding.
/api/insights returned response.content directly - the raw Anthropic SDK content block array ([{ type: 'text', text: '...' }]) instead of the actual text. utils.ts then accessed metricsResult.insights?.keyTrends, areasOfConcern, recommendations, and achievements on it, none of which exist on a content block array, so all four fields were always undefined and fell back to [].
/api/ai correctly extracted textContent.text but returned a raw markdown string. utils.ts then accessed programResult.insights?.riskFactors, strategicRecommendations, and successStories on it - fields that don't exist on a plain string, so those were always undefined too.
Result: combineAndPrioritize([], []) ran every time, the insights object was always full of empty arrays, and the AI Analysis panel rendered nothing.
Not related to a specific issue.
✅ Changes
Fix
/api/insights now prompts Claude to respond with a specific JSON structure matching what utils.ts expects: { keyTrends, areasOfConcern, recommendations, achievements }
/api/ai now prompts Claude to respond with a specific JSON structure matching what utils.ts expects: { riskFactors, strategicRecommendations, successStories }
Both routes now correctly extract textContent.text from the Anthropic response and parse it as JSON before returning
Both routes now throw meaningful errors if Claude returns non-JSON instead of silently failing
🔍 Additional Notes
No breaking changes to the response envelope shape - both routes still return { insights, success }. Only the content of insights changes from undefined/raw array to the structured object utils.ts already expected.
Checklist