diff --git a/backend/innercontext/api/products.py b/backend/innercontext/api/products.py index f8f3c77..d65b5db 100644 --- a/backend/innercontext/api/products.py +++ b/backend/innercontext/api/products.py @@ -356,12 +356,21 @@ def parse_product_text(data: ProductParseRequest) -> ProductParseResponse: config=genai_types.GenerateContentConfig( system_instruction=_product_parse_system_prompt(), response_mime_type="application/json", - max_output_tokens=4096, + max_output_tokens=8192, temperature=0.0, ), ) + raw = response.text + if not raw: + raise HTTPException(status_code=502, detail="LLM returned an empty response") + # Fallback: extract JSON object in case the model adds preamble or markdown fences + if not raw.lstrip().startswith("{"): + start = raw.find("{") + end = raw.rfind("}") + if start != -1 and end != -1: + raw = raw[start : end + 1] try: - parsed = json.loads(response.text) + parsed = json.loads(raw) except (json.JSONDecodeError, Exception) as e: raise HTTPException(status_code=502, detail=f"LLM returned invalid JSON: {e}") try: