Skip to content

KeyError: 'encoder' when loading recognition model with transformers 4.56+ #469

@MattiasHaughom

Description

@MattiasHaughom

Issue:
surya-ocr==0.12.1 crashes with KeyError: 'encoder' when using transformers>=4.56.0.

Error location:
surya/recognition/model/config.py, line 17:
encoder_config = kwargs.pop("encoder") # KeyError when encoder not in kwargs

Root cause:
Newer transformers versions changed how configs are initialized during from_pretrained(). The encoder and decoder keys are no
longer passed in kwargs.

Suggested fix:
encoder_config = kwargs.pop("encoder", None)
decoder_config = kwargs.pop("decoder", None)

if encoder_config is None or decoder_config is None:
if hasattr(self, 'encoder') and hasattr(self, 'decoder'):
return # Already set by parent class
raise ValueError("encoder and decoder configs required")

Current workaround:
Pin to transformers==4.36.2, but this conflicts with modern packages requiring transformers 4.41+.

Question: Does version 0.17.0 fix this issue?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions