fix(ai): parse only the last final_answer message for structured output #23834
All Vouch review confidence checks passed
Residual risk 0.18 = Change Risk (0.45) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.45 · 292 lines diff across 3 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.340
Shallow Review Depth
shallow_review_depth
0.289
Ai Authored
ai_authored
0.050
Real PR Conversation Timeline
1 comments
"Review active on #23834"
general
weight: 0.35
princejain756
Background
#23832 reports that
Output.objectfails on OpenAI Responses replies that contain more than one assistantmessage(e.g. acommentarymessage and afinal_answermessage). Each message maps to a separate text content part, andgenerateText/streamTextjoin all text parts before structured output parsing, so the parser receives two concatenated JSON documents ({...}{...}) and throwsAI_NoObjectGeneratedError/AI_JSONParseErroreven though the final answer is valid JSON.Summary
The message phase is already available in
providerMetadata.<provider>.phaseon the text parts (added foropenaiandazureunder #12475/#12858). A newgetOutputParseTexthelper instep-result.tsuses it: when text parts carryphasemetadata, only the text of the lastfinal_answermessage is handed toparseCompleteOutputin bothgenerateTextandstreamText.result.text,step.content, response messages, and usage keep the full reply unchanged, and replies without phase metadata behave exactly as before. The same path also handles the "twofinal_answermessages" variant, where only the last one is parsed.Contributor Credit
Issue report and reproduction by @roeibh (#23832).
End-to-End Verification
Reproduced the reported scenario through the public
generateTextandstreamTextentry points with a reply of two phase-tagged messages containing individually valid JSON. Before the change, both threw the reportedAI_JSONParseError(JSON parsing failed: Text: { "value": "commentary" }{ "value": "answer" }); after it,result.outputresolves to the object parsed from the lastfinal_answermessage whilefinalStep.textstill contains the full reply.Checklist
pnpm changesetin the project root)Future Work
streamTextpartial output (partialObjectStream) still sees the raw concatenated stream text; mid-stream partials may not parse for multi-message replies.Related Issues
#23832