Skip to content

fix: handle ask ai answers that don't match the metadata format 🐛 - #920

Open
bryanrg22 wants to merge 2 commits into
colorstackorg:mainfrom
bryanrg22:fix/ask-ai-metadata-json
Open

bryanrg22 wants to merge 2 commits into
colorstackorg:mainfrom
bryanrg22:fix/ask-ai-metadata-json

Conversation

@bryanrg22

@bryanrg22 bryanrg22 commented Nov 22, 2026 •

Copy link
Copy Markdown

Description ✏️

Closes #906.

parseAnswerForMemberProfile passed everything before the first --- straight to JSON.parse, so Ask AI threw away the whole answer with "Failed to parse thread metadata into JSON." whenever the model wrapped the metadata JSON in a ```json code fence. A reply with no JSON at all failed with "Missing metadata section or answer section from response."

  • Strips the code fence with stripCodeFence from infrastructure/ai.ts before parsing the metadata, as suggested in ask ai shows "Failed to parse thread metadata into JSON." instead of an answer #906 (the same helper fix: strip code fences from AI JSON before parsing opportunities #908 uses for opportunities).
  • Shows a reply without any metadata as an answer with no sources (it still fails if the answer references a thread).
  • Reports the metadata section (never the answer, which is built from members' Slack messages) when it still can't be parsed, so the failure can be debugged.
  • Tells the model not to put the JSON in a code block.
  • Adds tests for parseAnswerForMemberProfile (fenced JSON, delimiter spacing, a --- inside the answer, no metadata, and the cases that should still fail).

Out of scope: a reply with text before the JSON still fails (and is reported). Production only showed fenced JSON, so this PR doesn't handle that case; #906 is updated to match.

Testing

Rebased onto the latest main (including #908 and #918) and re-tested to confirm the bug is still present there and that this fix resolves it.

Tested against the real model (claude-sonnet-4-6) through answerMemberProfileQuestion with seeded Slack threads, 8 questions per run:

  • Current prompt on main (including fix: render Ask AI answers as markdown 📝 #918's Markdown rule): 3 of 8 replies wrapped the JSON in a ```json fence. The parser on main failed on all 3 with "Failed to parse thread metadata into JSON."; this PR's parser handled all 8.
  • With this PR: 0 of 8 replies were fenced and all 8 parsed with the correct sources, including a Markdown table answer (its |---| rows come after the delimiter, so the split is unaffected).

Lint, type-check, build and tests (--filter against main, as in CI) all pass locally.

Type of Change 🐞

  • Feature - A non-breaking change which adds functionality.
  • Fix - A non-breaking change which fixes an issue.
  • Refactor - A change that neither fixes a bug nor adds a feature.
  • Documentation - A change only to in-code or markdown documentation.
  • Tests - A change that adds missing unit/integration tests.
  • Chore - A change that is likely none of the above.

Checklist ✅

  • I have done a self-review of my code.
  • I have manually tested my code (if applicable).
  • I have added/updated any relevant documentation (if applicable).

The model sometimes wraps the metadata JSON in a ```json code fence, which
made parseAnswerForMemberProfile fail with "Failed to parse thread metadata
into JSON." instead of showing the answer.

- Strip the code fence with stripCodeFence before parsing the metadata.
- Show output without any metadata as an answer with no sources.
- Report the raw output when it still can't be parsed.
- Tell the model not to put the JSON in a code block.
- Add tests for parseAnswerForMemberProfile.

@queenellie873 queenellie873 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for picking this up, @bryanrg22! Reusing stripCodeFence keeps it consistent with opportunities, and the tests are great. They call the parser directly, and the fence and no-metadata cases fail on main, so they catch exactly the bug from 11/18. Keeping a --- or table inside the answer working is a nice touch too.

Two things before we merge:

  1. Don't report the full output. failToParse sends the whole model reply to Sentry and the logs. The answer is built from members' Slack messages, so it can include names and personal details we don't want in error reports. The part that actually fails is the metadata section (just channel/thread IDs), so can you report metadataSection instead of output? That's still enough to debug the failure. The two tests that check context: { output } would need to change to match.

  2. Text before the JSON. #906 lists "text before the JSON" as a case that should still show the answer, but this PR keeps it failing and adds a test for that. Totally fine if you'd rather leave it out of scope, since prod only showed fences. Just say so in the PR description and update #906. If you do want to handle it, that works too.

Also, CI hasn't run the lint/type-check/test workflow on this PR yet. I'll approve the run on my side.

Once the first one is in, this looks good to me.

@bryanrg22

Copy link
Copy Markdown
Author

Thanks for the review, @queenellie873!

  1. Don't report the full output: done. failToParse now reports only the metadata section, never the answer:
    • If the reply has no --- delimiter at all, the "metadata section" would be the whole reply, so that case reports no content.
    • The reported section is also cut off at the JSON's closing brace. That covers the case where the model drops the delimiter and the first --- is actually inside the answer (for example a Markdown table row), which would otherwise pull answer text into the report.
    • I updated the two tests and added one that runs every failure path, including the table case, with a member's name and email in the answer and checks they never end up in the report.
  2. Text before the JSON: leaving it out of scope, since prod only showed fences. I updated the PR description and ask ai shows "Failed to parse thread metadata into JSON." instead of an answer #906 to say so.

This branch has not been deployed

No deployments
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.

ask ai shows "Failed to parse thread metadata into JSON." instead of an answer

2 participants