GH-5832: Validate all results in StructuredOutputValidationAdvisor#5846
Open
suryateja-g13 wants to merge 1 commit intospring-projects:mainfrom
Open
GH-5832: Validate all results in StructuredOutputValidationAdvisor#5846suryateja-g13 wants to merge 1 commit intospring-projects:mainfrom
suryateja-g13 wants to merge 1 commit intospring-projects:mainfrom
Conversation
…ationAdvisor Previously, validateOutputSchema() called getResult() which only returns the first ChatResponse result. Subsequent results were silently ignored, allowing invalid JSON in later generations to bypass schema validation entirely. This was tracked as a TODO comment in the source. Replace the single-result check with getResults() iteration: the first failing result short-circuits and triggers a retry with an augmented prompt. All results must be valid for the response to be accepted. Fixes spring-projectsGH-5832 (spring-projects#5832) Signed-off-by: Gorre Surya <sgorre92@gmail.com>
|
LGTM. Clean iteration over getResults() with short-circuit on first failure. Good test coverage with testValidationWithMultipleResultsAllValid and testValidationWithMultipleResultsSecondInvalid. Ship it. |
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.
Thank you for taking time to contribute this pull request!
Problem
StructuredOutputValidationAdvisor.validateOutputSchema()calledchatResponse.getResult(), which only retrieves the first result. When aChatResponsecontains multiple results, all subsequent results were silently skipped, allowing invalid JSON in later generations to bypass schema validation entirely.This was tracked as a TODO comment in the source:
Solution
Replace the single-result check with
getResults()iteration. The first failing result short-circuits the loop and triggers a retry with an augmented prompt. All results must pass for the response to be accepted.Changes
StructuredOutputValidationAdvisor.java— replacedgetResult()withgetResults()iteration invalidateOutputSchema(); null/empty results check updated accordinglyStructuredOutputValidationAdvisorTests.java— updated existingtestAdviseCallWithNullResultto stubgetResults()(the oldgetResult()stub became unused); added two new tests:testValidationWithMultipleResultsAllValid— response with 2 valid results passes without retrytestValidationWithMultipleResultsSecondInvalid— response where second result is invalid triggers retryChecklist
Signed-off-byline to commit (DCO)mainmvn package -pl spring-ai-client-chat)Fixes #5832