Skip to content

Commit e640f6f

Browse files
raise clean error on malformed sam_configuration.json
1 parent efc0912 commit e640f6f

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

inference_models/inference_models/models/sam3/sam3_torch.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,15 @@ def from_pretrained(
102102
model_name_or_path, "sam_configuration.json"
103103
)
104104
if os.path.exists(sam_configuration_path):
105-
version = decode_sam_version(config_path=sam_configuration_path)
105+
try:
106+
version = decode_sam_version(config_path=sam_configuration_path)
107+
except (KeyError, ValueError) as error:
108+
raise CorruptedModelPackageError(
109+
message="Could not decode sam_configuration.json in SAM3 model package. "
110+
"If you run inference locally, verify the correctness of SAM3 model package. "
111+
"If you see the error running on Roboflow platform - contact us to get help.",
112+
help_url="https://todo",
113+
) from error
106114
if version not in SUPPORTED_VERSIONS:
107115
raise CorruptedModelPackageError(
108116
message=f"Detected unsupported version of SAM3 model: {version}. Supported versions: "

inference_models/tests/unit_tests/models/test_sam3_torch.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,21 @@ def test_from_pretrained_rejects_unsupported_sam_configuration_version(
194194
model_name_or_path=str(tmp_path),
195195
device=torch.device("cpu"),
196196
)
197+
198+
199+
def test_from_pretrained_rejects_malformed_sam_configuration(
200+
sam3_torch_module: ModuleType, tmp_path
201+
) -> None:
202+
# given - config present but missing the "version" key
203+
from inference_models.errors import CorruptedModelPackageError
204+
205+
(tmp_path / "weights.pt").touch()
206+
(tmp_path / "bpe_simple_vocab_16e6.txt.gz").touch()
207+
(tmp_path / "sam_configuration.json").write_text('{"unexpected": "content"}')
208+
209+
# when / then
210+
with pytest.raises(CorruptedModelPackageError):
211+
sam3_torch_module.SAM3Torch.from_pretrained(
212+
model_name_or_path=str(tmp_path),
213+
device=torch.device("cpu"),
214+
)

0 commit comments

Comments
 (0)