From 14c0b3acca40ef5d59599de27cb8e71169a963fa Mon Sep 17 00:00:00 2001 From: Kevin Turcios <106575910+KRRT7@users.noreply.github.com> Date: Fri, 6 Mar 2026 07:32:30 +0000 Subject: [PATCH] fix: handle syntactically invalid LLM output in testgen repair (#2472) ## Summary - Catch `ParserSyntaxError` when parsing LLM-repaired code instead of letting it bubble to the generic 500 handler - Reduces Sentry noise from expected LLM failures - The CLI already handles non-200 responses gracefully (returns `None`, continues) --- django/aiservice/core/shared/testgen_review/repair.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/django/aiservice/core/shared/testgen_review/repair.py b/django/aiservice/core/shared/testgen_review/repair.py index 4cc4c70ab..19a322009 100644 --- a/django/aiservice/core/shared/testgen_review/repair.py +++ b/django/aiservice/core/shared/testgen_review/repair.py @@ -90,7 +90,11 @@ async def testgen_repair( from core.languages.python.testgen.validate import instrument_tests, validate_request_data # noqa: PLC0415 from core.shared.testgen_models import TestGenSchema # noqa: PLC0415 - repaired_cst = parse_module_to_cst(repaired_code) + try: + repaired_cst = parse_module_to_cst(repaired_code) + except Exception: + logging.warning("LLM returned syntactically invalid repaired code, falling back to original") + return 500, TestRepairErrorSchema(error="LLM returned syntactically invalid code") original_cst = parse_module_to_cst(data.test_source) # Extract repaired function nodes by name