diff --git a/Agents/AI Service/agency.py b/Agents/AI Service/agency.py index 6a25710..8c338ee 100644 --- a/Agents/AI Service/agency.py +++ b/Agents/AI Service/agency.py @@ -157,6 +157,16 @@ def __init__(self, role: str, prompt: ChatPromptTemplate): logger.info(f"Loading {role} model: {model_name}") self.llm = ChatOllama(model=model_name) + @staticmethod + def _parse_json(text: str) -> Any: + """Extract and parse the first JSON object found in text.""" + start = text.find("{") + if start == -1: + raise json.JSONDecodeError("No JSON object found", text, 0) + decoder = json.JSONDecoder() + obj, _ = decoder.raw_decode(text[start:]) + return obj + async def run(self, project_json: str, context: Dict[str, Any], timeout: Optional[float] = 60.0) -> Any: """ Invoke the LLM with formatted messages and return parsed JSON. @@ -176,7 +186,7 @@ async def run(self, project_json: str, context: Dict[str, Any], timeout: Optiona timeout, ) raw = getattr(message, "content", message) - parsed = json.loads(raw) + parsed = self._parse_json(raw) logger.info(f"{self.role} responded successfully") return parsed except asyncio.TimeoutError: