Skip to content

fix(layout): add id2label to LayoutConfig and guard missing label_task_mapping#104

Open
Manitchahar wants to merge 3 commits intozai-org:mainfrom
Manitchahar:fix/layout-config-defaults
Open

fix(layout): add id2label to LayoutConfig and guard missing label_task_mapping#104
Manitchahar wants to merge 3 commits intozai-org:mainfrom
Manitchahar:fix/layout-config-defaults

Conversation

@Manitchahar
Copy link
Copy Markdown

What this PR does

Fixes two layout-mode crashes when using custom config files:

  1. LayoutConfig did not declare id2label, but glmocr/layout/layout_detector.py
    reads config.id2label.

    • Added: id2label: Optional[Dict[Union[int, str], str]] = None in glmocr/config.py.
  2. layout_detector.py assumed label_task_mapping is always present and called
    .items() on it.

    • Added guard in start() to fallback when missing:
      • logs a warning
      • sets label_task_mapping = {"text": list(self.id2label.values())}

Reproduction

  1. Create custom config with layout enabled but without id2label and label_task_mapping.
  2. Run: glmocr parse "image.png" --config custom.yaml
  3. Before: AttributeError crash. After: pipeline continues safely.

Files changed

  • glmocr/config.py (+1 line)
  • glmocr/layout/layout_detector.py (+5 lines)

Notes

Scope is intentionally minimal (2 files only), with no behavior change unless those config fields are missing.

Copilot AI review requested due to automatic review settings February 21, 2026 21:38
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes two layout-mode crashes when using custom config files by adding the missing id2label field to LayoutConfig and adding a guard for missing label_task_mapping in the layout detector initialization.

Changes:

  • Added id2label field to LayoutConfig to prevent AttributeError when reading config.id2label
  • Added guard in layout_detector.py to handle missing label_task_mapping with a sensible default fallback

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
glmocr/config.py Adds id2label field to LayoutConfig class definition
glmocr/layout/layout_detector.py Adds guard to handle missing label_task_mapping with default fallback to text task type

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@then3rd
Copy link
Copy Markdown

then3rd commented Mar 23, 2026

Just fyi looks like there are new upstream changes to main that removed some dependencies needed for your branch to run. I had to to add "opencv-python>=4.8.0", back to the layout requirements in pyproject.toml

@yanhuixie
Copy link
Copy Markdown

这个PR啥时候可以合并呢?这是一个比较明确的bug,我也遇到了:
python -m glmocr.server --config .\config.yml
Error: 'LayoutConfig' object has no attribute 'id2label'

@Manitchahar Manitchahar force-pushed the fix/layout-config-defaults branch from d2a4edb to 51b19d1 Compare March 25, 2026 22:00
@Manitchahar
Copy link
Copy Markdown
Author

Rebased onto current upstream main, added regression tests for missing id2label / default label_task_mapping, and full pytest passes locally (124 passed, 23 skipped).

@Manitchahar Manitchahar reopened this Mar 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants