diff --git a/omlx/admin/routes.py b/omlx/admin/routes.py index c9b4d2c17..796373a9d 100644 --- a/omlx/admin/routes.py +++ b/omlx/admin/routes.py @@ -1339,7 +1339,8 @@ async def list_grammar_parsers(is_admin: bool = Depends(require_admin)): {"value": style, "label": style, "models": models} for style, models in supported.items() ] - except ImportError: + except Exception as e: + logger.warning("xgrammar parser discovery unavailable: %s", e) return [] diff --git a/tests/test_grammar.py b/tests/test_grammar.py index 97f95be30..1b9ad2b40 100644 --- a/tests/test_grammar.py +++ b/tests/test_grammar.py @@ -822,3 +822,18 @@ def test_returns_empty_when_xgrammar_unavailable(self, client): resp = client.get("/admin/api/grammar/parsers") assert resp.status_code == 200 assert resp.json() == [] + + def test_returns_empty_when_xgrammar_native_binding_fails(self, client): + def raise_binding_error(): + raise RuntimeError( + "Cannot find library: " + "libxgrammar_bindings.dylib, libxgrammar_bindings.so" + ) + + fake_xgrammar = SimpleNamespace( + get_builtin_structural_tag_supported_models=raise_binding_error + ) + with patch.dict("sys.modules", {"xgrammar": fake_xgrammar}): + resp = client.get("/admin/api/grammar/parsers") + assert resp.status_code == 200 + assert resp.json() == []