From e9d7c6d391c6312abffcf07c49f720e14a4fcaac Mon Sep 17 00:00:00 2001 From: VinhNT Date: Wed, 23 Sep 2026 08:04:36 +0000 Subject: [PATCH] =?UTF-8?q?Gi=E1=BB=AF=20block=20=E1=BA=A3nh,=20QR,=20con?= =?UTF-8?q?=20d=E1=BA=A5u,=20ch=E1=BB=AF=20k=C3=BD=20r=E1=BB=97ng=20c?= =?UTF-8?q?=E1=BB=A7a=20model?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit validate_response từ chối mọi block có html rỗng, trong khi model trả đúng như thế cho các vùng không có chữ: xem chính dla_example_document.json, các block Image, QR Code, Stamp, Signature đều có "html": "". Nghĩa là mọi trang có con dấu hay QR đều bị coi là output hỏng, retry 3 lần rồi thành trang lỗi. Chỉ block chữ mà rỗng mới là output hỏng; thông báo lỗi kèm luôn nhãn. Co-Authored-By: Claude Opus 5 (1M context) --- idp_service/ocr.py | 6 ++++-- tests/test_service.py | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/idp_service/ocr.py b/idp_service/ocr.py index d91d83c..78fcf5f 100644 --- a/idp_service/ocr.py +++ b/idp_service/ocr.py @@ -18,6 +18,8 @@ LABELS = { } LABEL_ALIASES = {"Page-Header": "Header", "Page Header": "Header", "Page-Footer": "Footer", "Page Footer": "Footer"} +# Vùng không có chữ: model trả div rỗng, không phải output hỏng. +VISUAL_LABELS = {"Image", "QR Code", "Barcode", "Signature", "Stamp"} class InvalidOutput(Exception): @@ -54,8 +56,8 @@ def validate_response(response: dict) -> list[dict]: box = block["bbox"] if box is None or len(box) != 4 or not (0 <= box[0] < box[2] <= 1000 and 0 <= box[1] < box[3] <= 1000): raise InvalidOutput(f"Invalid bounding box: {box}") - if not block["html"]: - raise InvalidOutput("Empty layout block") + if not block["html"] and block["label"] not in VISUAL_LABELS: + raise InvalidOutput(f"Empty layout block: {block['label']}") return blocks diff --git a/tests/test_service.py b/tests/test_service.py index f734fb1..9d7c8d5 100644 --- a/tests/test_service.py +++ b/tests/test_service.py @@ -322,6 +322,26 @@ def test_kie_prompt_schema_and_answer_shape(): assert kie.select_pages(20, 5) == [1, 2, 3, 4, 20] +def test_keeps_empty_visual_blocks_but_rejects_empty_text_blocks(): + """Model trả div rỗng cho con dấu, QR, chữ ký; chỉ block chữ mà rỗng mới là output hỏng.""" + def response(content): + return {"choices": [{"finish_reason": "stop", "message": {"content": content}}]} + + blocks = validate_response(response( + '
' + '
' + '

Nội dung

')) + assert [block["label"] for block in blocks] == ["Stamp", "QR Code", "Paragraph"] + assert [block["html"] for block in blocks[:2]] == ["", ""] + + try: + validate_response(response('
')) + except InvalidOutput as exc: + assert "Paragraph" in str(exc) + else: + raise AssertionError("Block chữ rỗng đáng lẽ phải bị từ chối") + + def test_rejects_truncated_plain_text_and_invalid_bbox(): def response(content, finish="stop"): return {"choices": [{"finish_reason": finish, "message": {"content": content}}]}