From 54903a3bedb42997a45b41b4bae999f8cd87959d Mon Sep 17 00:00:00 2001 From: Piotr Oleszczyk Date: Sat, 28 Feb 2026 21:43:39 +0100 Subject: [PATCH] fix(backend): handle invalid/empty JSON from Gemini in product parse endpoint MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Increase max_output_tokens 4096 → 8192 to prevent truncated JSON on products with long INCI lists - Return explicit 502 when response.text is None (safety filter blocks) - Fallback JSON extraction strips markdown fences or leading preamble Co-Authored-By: Claude Sonnet 4.6 --- backend/innercontext/api/products.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) 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: