← Back to Pull requests

fix(ai): parse only the last final_answer message for structured output #23834

Closed
unknown closed this pull request without merging on Oct 23, 2026 19:03 UTC
View on GitHub
unknown unknown commented on Oct 22, 2026 15:26 UTC
Author

Background

#23832 reports that Output.object fails on OpenAI Responses replies that contain more than one assistant message (e.g. a commentary message and a final_answer message). Each message maps to a separate text content part, and generateText / streamText join all text parts before structured output parsing, so the parser receives two concatenated JSON documents ({...}{...}) and throws AI_NoObjectGeneratedError / AI_JSONParseError even though the final answer is valid JSON.

Summary

The message phase is already available in providerMetadata.<provider>.phase on the text parts (added for openai and azure under #12475/#12858). A new getOutputParseText helper in step-result.ts uses it: when text parts carry phase metadata, only the text of the last final_answer message is handed to parseCompleteOutput in both generateText and streamText. 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 "two final_answer messages" 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 generateText and streamText entry points with a reply of two phase-tagged messages containing individually valid JSON. Before the change, both threw the reported AI_JSONParseError (JSON parsing failed: Text: { "value": "commentary" }{ "value": "answer" }); after it, result.output resolves to the object parsed from the last final_answer message while finalStep.text still contains the full reply.

Checklist

  • All commits are signed (PRs with unsigned commits cannot be merged)

  • Tests have been added / updated (for bug fixes / features)

  • Documentation has been added / updated (for bug fixes / features)

  • A _patch_ changeset for relevant packages has been added (for bug fixes / features - run pnpm changeset in the project root)

  • I have reviewed this pull request (self-review)
  • Future Work

    streamText partial output (partialObjectStream) still sees the raw concatenated stream text; mid-stream partials may not parse for multi-message replies.

    Related Issues

    #23832

    Diff: 292 lines (+219 / -73) across 3 files Reviewer: princejain756 Review duration: 1657m
    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
    0.45
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 44.9% (MEDIUM) · Higher = larger blast radius
    44.9%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 17.7% · Safe to merge
    17.7%
    Repository