fix(guardrails): handle Aim multi-choice gather exceptions cleanly

Greptile P2: ``asyncio.gather`` without ``return_exceptions=True`` lets
the first failing call propagate immediately, leaving the other in-flight
inspections running until they complete on their own. Pass
``return_exceptions=True`` so every inspection finishes, then re-raise
the first exception encountered while iterating results.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
user
2026-05-01 04:58:00 +00:00
co-authored by Claude Opus 4.7
parent 5397ac4562
commit 41eeaaf630
@@ -266,6 +266,9 @@ class AimGuardrail(CustomGuardrail):
choices_to_inspect = [c for c in response.choices if isinstance(c, Choices)]
if not choices_to_inspect:
return response
# ``return_exceptions=True`` lets every inspection finish even if
# one fails — without it, the first exception would propagate and
# leave the remaining tasks running in the background.
results = await asyncio.gather(
*(
self.call_aim_guardrail_on_output(
@@ -275,9 +278,12 @@ class AimGuardrail(CustomGuardrail):
key_alias=user_api_key_dict.key_alias,
)
for choice in choices_to_inspect
)
),
return_exceptions=True,
)
for choice, aim_output_guardrail_result in zip(choices_to_inspect, results):
if isinstance(aim_output_guardrail_result, BaseException):
raise aim_output_guardrail_result
if aim_output_guardrail_result and aim_output_guardrail_result.get(
"detection_message"
):