Skip to content

fix: return structured JSON from AI routes so insights actually populate#101

Open
Harshit-Mishra2212 wants to merge 1 commit into
seetadev:mainfrom
Harshit-Mishra2212:fix/ai-routes-return-structured-response-v2
Open

fix: return structured JSON from AI routes so insights actually populate#101
Harshit-Mishra2212 wants to merge 1 commit into
seetadev:mainfrom
Harshit-Mishra2212:fix/ai-routes-return-structured-response-v2

Conversation

@Harshit-Mishra2212

Copy link
Copy Markdown

📌 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

  • I have tested this code
  • I have added necessary documentation
  • I have linked relevant issue(s)
  • I followed the code style of the project
  • I reviewed my own code

@vercel

vercel Bot commented May 25, 2026

Copy link
Copy Markdown

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

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.

1 participant