fix(backend): handle invalid/empty JSON from Gemini in product parse endpoint

- 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 <noreply@anthropic.com>
This commit is contained in:
Piotr Oleszczyk 2026-02-28 21:43:39 +01:00
parent d938c9999b
commit 54903a3bed

View file

@ -356,12 +356,21 @@ def parse_product_text(data: ProductParseRequest) -> ProductParseResponse:
config=genai_types.GenerateContentConfig( config=genai_types.GenerateContentConfig(
system_instruction=_product_parse_system_prompt(), system_instruction=_product_parse_system_prompt(),
response_mime_type="application/json", response_mime_type="application/json",
max_output_tokens=4096, max_output_tokens=8192,
temperature=0.0, 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: try:
parsed = json.loads(response.text) parsed = json.loads(raw)
except (json.JSONDecodeError, Exception) as e: except (json.JSONDecodeError, Exception) as e:
raise HTTPException(status_code=502, detail=f"LLM returned invalid JSON: {e}") raise HTTPException(status_code=502, detail=f"LLM returned invalid JSON: {e}")
try: try: