Giữ block ảnh, QR, con dấu, chữ ký rỗng của model

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) <noreply@anthropic.com>
This commit is contained in:
2026-09-23 08:04:36 +00:00
parent de967a64c2
commit e9d7c6d391
2 changed files with 24 additions and 2 deletions

View File

@@ -18,6 +18,8 @@ LABELS = {
} }
LABEL_ALIASES = {"Page-Header": "Header", "Page Header": "Header", LABEL_ALIASES = {"Page-Header": "Header", "Page Header": "Header",
"Page-Footer": "Footer", "Page Footer": "Footer"} "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): class InvalidOutput(Exception):
@@ -54,8 +56,8 @@ def validate_response(response: dict) -> list[dict]:
box = block["bbox"] 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): 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}") raise InvalidOutput(f"Invalid bounding box: {box}")
if not block["html"]: if not block["html"] and block["label"] not in VISUAL_LABELS:
raise InvalidOutput("Empty layout block") raise InvalidOutput(f"Empty layout block: {block['label']}")
return blocks return blocks

View File

@@ -322,6 +322,26 @@ def test_kie_prompt_schema_and_answer_shape():
assert kie.select_pages(20, 5) == [1, 2, 3, 4, 20] 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(
'<div data-label="Stamp" data-bbox="736 86 909 142"></div>'
'<div data-label="QR Code" data-bbox="814 55 896 112"></div>'
'<div data-label="Paragraph" data-bbox="92 85 795 151"><p>Nội dung</p></div>'))
assert [block["label"] for block in blocks] == ["Stamp", "QR Code", "Paragraph"]
assert [block["html"] for block in blocks[:2]] == ["", ""]
try:
validate_response(response('<div data-label="Paragraph" data-bbox="1 1 500 500"></div>'))
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 test_rejects_truncated_plain_text_and_invalid_bbox():
def response(content, finish="stop"): def response(content, finish="stop"):
return {"choices": [{"finish_reason": finish, "message": {"content": content}}]} return {"choices": [{"finish_reason": finish, "message": {"content": content}}]}