fix: harden extraction batch handling
Deploy contracts-flask / validate (push) Canceled after 0s

This commit is contained in:
Repinoid
2026-10-31 19:06:34 +03:00
parent 9c73a730d1
commit 79de42da96
6 changed files with 79 additions and 21 deletions
+21 -9
View File
@@ -250,12 +250,16 @@ def _save_extraction(document_id, extracted, validation, status, raw_response="{
def extract_document(document, llm_client=None, batch_id=None):
"""Извлечь паспорт одного документа, изолируя ошибки на уровне документа."""
document_id = document["id"]
batch_id = batch_id if batch_id is not None else document.get("batch_id")
elements = _decode_elements(document.get("elements", document.get("elements_json")))
structured_text = format_elements_with_structure(elements)
document_id = document.get("id") if isinstance(document, dict) else None
raw_response = "{}"
try:
if not document_id:
raise ValueError("document id is required")
batch_id = batch_id if batch_id is not None else document.get("batch_id")
elements = _decode_elements(document.get("elements", document.get("elements_json")))
# Formatting is part of this document's failure boundary: corrupt stored
# elements must not escape and stop extraction of later batch members.
structured_text = format_elements_with_structure(elements)
if _is_garbage_by_filename(document.get("filename") or "") or _is_garbage_by_header(structured_text[:2000]):
extracted = {
"doc_class": "ignored_accounting", "intent_mode": "no_spec",
@@ -299,11 +303,19 @@ def extract_document(document, llm_client=None, batch_id=None):
return {"ok": True, "document_id": document_id, "status": result["status"], "extraction": saved}
except Exception as error:
log.exception("Stateless extraction failed for document %s", document_id)
_save_extraction(document_id, {
"doc_class": "unknown", "intent_mode": "no_spec", "counterparty": {},
"referenced_contracts": [], "sections": [],
}, {"status": "FAILED", "error": str(error)}, "FAILED", raw_response)
return {"ok": False, "document_id": document_id, "error": str(error)}
result = {"ok": False, "document_id": document_id, "error": str(error)}
if document_id:
# Failure persistence is best-effort; a secondary write failure must
# not escape this handler and abort processing of remaining documents.
try:
_save_extraction(document_id, {
"doc_class": "unknown", "intent_mode": "no_spec", "counterparty": {},
"referenced_contracts": [], "sections": [],
}, {"status": "FAILED", "error": str(error)}, "FAILED", raw_response)
except Exception as persistence_error:
log.exception("Failed to persist extraction failure for document %s", document_id)
result["persistence_error"] = str(persistence_error)
return result
def extract_batch(batch_id, llm_client=None, progress_callback=None, documents=None):