Repository navigation
fix(ai): parse only the last final_answer message for structured output - #23834
Closed
princejain756 wants to merge 2 commits into
Closed
princejain756 wants to merge 2 commits into
princejain756 wants to merge 2 commits into
Conversation
The phase filter also ran for the default text() spec, so result.output dropped earlier messages while result.text kept them. Only structured specs parse the last final_answer message now.
aayush-kapoor
left a comment
Collaborator
There was a problem hiding this comment.
theres some edge cases that this misses - will be handled in #23895
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.
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