feat(conductor): Add retry loop for Tech Lead JSON parsing
This commit is contained in:
@@ -21,29 +21,35 @@ def generate_tickets(track_brief: str, module_skeletons: str) -> list[dict[str,
|
||||
old_system_prompt = ai_client._custom_system_prompt
|
||||
ai_client.set_custom_system_prompt(system_prompt or "")
|
||||
ai_client.current_tier = "Tier 2"
|
||||
last_error = None
|
||||
try:
|
||||
# 3. Call Tier 2 Model
|
||||
response = ai_client.send(
|
||||
md_content="",
|
||||
user_message=user_message
|
||||
)
|
||||
# 4. Parse JSON Output
|
||||
# Extract JSON array from markdown code blocks if present
|
||||
json_match = response.strip()
|
||||
if "```json" in json_match:
|
||||
json_match = json_match.split("```json")[1].split("```")[0].strip()
|
||||
elif "```" in json_match:
|
||||
json_match = json_match.split("```")[1].split("```")[0].strip()
|
||||
# If it's still not valid JSON, try to find a [ ... ] block
|
||||
if not (json_match.startswith('[') and json_match.endswith(']')):
|
||||
match = re.search(r'\[\s*\{.*\}\s*\]', json_match, re.DOTALL)
|
||||
if match:
|
||||
json_match = match.group(0)
|
||||
tickets: list[dict[str, Any]] = json.loads(json_match)
|
||||
return tickets
|
||||
except Exception as e:
|
||||
print(f"Error parsing Tier 2 response: {e}")
|
||||
return []
|
||||
for _ in range(3):
|
||||
try:
|
||||
# 3. Call Tier 2 Model
|
||||
response = ai_client.send(
|
||||
md_content="",
|
||||
user_message=user_message
|
||||
)
|
||||
# 4. Parse JSON Output
|
||||
# Extract JSON array from markdown code blocks if present
|
||||
json_match = response.strip()
|
||||
if "```json" in json_match:
|
||||
json_match = json_match.split("```json")[1].split("```")[0].strip()
|
||||
elif "```" in json_match:
|
||||
json_match = json_match.split("```")[1].split("```")[0].strip()
|
||||
# If it's still not valid JSON, try to find a [ ... ] block
|
||||
if not (json_match.startswith('[') and json_match.endswith(']')):
|
||||
match = re.search(r'\[\s*\{.*\}\s*\]', json_match, re.DOTALL)
|
||||
if match:
|
||||
json_match = match.group(0)
|
||||
tickets: list[dict[str, Any]] = json.loads(json_match)
|
||||
return tickets
|
||||
except json.JSONDecodeError as e:
|
||||
last_error = e
|
||||
correction = f"\n\nYour previous output failed to parse as JSON: {e}. Here was your raw output:\n{json_match[:500]}\n\nPlease fix the formatting and output ONLY valid JSON array."
|
||||
user_message += correction
|
||||
print(f"JSON parsing error, retrying... ({_ + 1}/3)")
|
||||
raise RuntimeError(f"Failed to generate valid JSON tickets after 3 attempts. Last error: {last_error}")
|
||||
finally:
|
||||
# Restore old system prompt and clear tier tag
|
||||
ai_client.set_custom_system_prompt(old_system_prompt or "")
|
||||
|
||||
Reference in New Issue
Block a user