Skip to content

Commit

Permalink
Merge pull request #1199 from edeandrea/patch-2
Browse files Browse the repository at this point in the history
Enhance output message when output guardrails fail
  • Loading branch information
geoand authored Jan 7, 2025
2 parents 6db70fb + 577ca23 commit 9314bcc
Showing 1 changed file with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

import jakarta.enterprise.inject.spi.CDI;

Expand Down Expand Up @@ -106,7 +108,15 @@ public static Response<AiMessage> invokeOutputGuardrails(AiServiceMethodCreateIn
}

if (attempt == max) {
throw new GuardrailException("Output validation failed. The guardrails have reached the maximum number of retries");
var failureMessages = Optional.ofNullable(result.failures())
.orElseGet(List::of)
.stream()
.map(OutputGuardrailResult.Failure::message)
.collect(Collectors.joining(System.lineSeparator()));

throw new GuardrailException(
"Output validation failed. The guardrails have reached the maximum number of retries. Guardrail messages:"
+ System.lineSeparator() + failureMessages);
}

if (result.hasRewrittenResult()) {
Expand Down

0 comments on commit 9314bcc

Please sign in to comment.