fix(backend): increase max_output_tokens to 65536, log finish_reason on error
Replace truncation-recovery heuristic with a higher token budget. On JSON parse failure, log finish_reason and 160-char error context to make the root cause visible. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
4abdc88286
commit
26069f5d66
1 changed files with 13 additions and 3 deletions
|
|
@ -360,13 +360,18 @@ 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=8192,
|
||||
max_output_tokens=65536,
|
||||
temperature=0.0,
|
||||
),
|
||||
)
|
||||
candidate = response.candidates[0] if response.candidates else None
|
||||
finish_reason = str(candidate.finish_reason) if candidate else "unknown"
|
||||
raw = response.text
|
||||
if not raw:
|
||||
raise HTTPException(status_code=502, detail="LLM returned an empty response")
|
||||
raise HTTPException(
|
||||
status_code=502,
|
||||
detail=f"LLM returned an empty response (finish_reason={finish_reason})",
|
||||
)
|
||||
# Fallback: extract JSON object in case the model adds preamble or markdown fences
|
||||
if not raw.lstrip().startswith("{"):
|
||||
start = raw.find("{")
|
||||
|
|
@ -380,7 +385,12 @@ def parse_product_text(data: ProductParseRequest) -> ProductParseResponse:
|
|||
try:
|
||||
parsed = json.loads(raw)
|
||||
except json.JSONDecodeError as e:
|
||||
log.error("Gemini parse-text raw response (failed):\n%s", raw)
|
||||
log.error(
|
||||
"Gemini parse-text JSON error at pos %d finish_reason=%s context=%r",
|
||||
e.pos,
|
||||
finish_reason,
|
||||
raw[max(0, e.pos - 80) : e.pos + 80],
|
||||
)
|
||||
raise HTTPException(status_code=502, detail=f"LLM returned invalid JSON: {e}")
|
||||
try:
|
||||
return ProductParseResponse.model_validate(parsed)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue