fix(ai): tolerate fenced provider JSON in suggestions, harden prompt#98
Merged
Conversation
Suggestions route carried a divergent getTextFromOpenRouter that skipped fence stripping, so gemini/minimax markdown-fenced JSON failed parsing and returned 502 "AI provider returned invalid suggestions". Reuse the shared fence-aware helper from @/lib/ai/ask-series-answer. Add output-contract guardrails to the suggestions prompt (JSON-only, no fences, empty array when nothing qualifies, verbatim evidence, no invented owners/dates). Dedupe enhance-notes onto the same shared helper. Lock both behaviors into the AI notes contract verifier.
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.
Problem
POST
/api/meetings/[meetingId]/suggestionsreturned 502 "AI provider returned invalid suggestions" in production. RCA: the provider (google/gemini-3.1-flash-lite, priorminimax-m3) wraps JSON in markdown fences despiteresponse_format: json_object. The suggestions route carried its own divergentgetTextFromOpenRouterthat skipped fence stripping, soJSON.parsethrew. Theaskandenhance-notesroutes were already hardened (commit b01e7f4); suggestions was the only one missed.Changes
getTextFromOpenRouterfrom@/lib/ai/ask-series-answer(deletes the local non-stripping copy).{"suggestions": []}when nothing qualifies, verbatimsource_excerpt, never invent owners/dates, confidence calibration, category definitions). Defense-in-depth alongside the parse-side fix.enhance-notesonto the same shared helper (removed a weaker forked parser + redundant double fence-strip; compacted prompt context JSON).Verification
pnpm build(full app type-check): passverify-ai-notes-contract,verify-query-contracts,verify-runtime-config: passFollow-up (separate PR)
Extract a shared
src/lib/ai/openrouter.ts(callOpenRouter+getOpenRouterApiKey) to collapse the 3-waygetOpenRouterData/URL/apiKey duplication across all three AI routes, and tighten theenhance-notesselect("*")over-fetch with a typed query.