Skip to content

[Bug] tvm_ffi.get_global_func_metdata failed for functions that don't have metadata #208

@junrushao

Description

@junrushao

To reproduce this issue:

@tvm_ffi.register_global_func("my.add")
def add(a, b):
    return a + b

tvm_ffi.get_global_func_metadata("my.add")

Python-side global functions don't have metadata right now, which triggers the json parser to fail:

File ~/Projects/tvm-ffi/python/tvm_ffi/registry.py:271, in get_global_func_metadata(name)
    236 def get_global_func_metadata(name: str) -> dict[str, Any]:
    237     """Get metadata (including type schema) for a global function.
    238
    239     Parameters
   (...)    269
    270     """
--> 271     return json.loads(get_global_func("ffi.GetGlobalFuncMetadata")(name))

File ~/.local/share/uv/python/cpython-3.14.0-macos-aarch64-none/lib/python3.14/json/__init__.py:346, in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    341     s = s.decode(detect_encoding(s), 'surrogatepass')
    343 if (cls is None and object_hook is None and
    344         parse_int is None and parse_float is None and
    345         parse_constant is None and object_pairs_hook is None and not kw):
--> 346     return _default_decoder.decode(s)
    347 if cls is None:
    348     cls = JSONDecoder

File ~/.local/share/uv/python/cpython-3.14.0-macos-aarch64-none/lib/python3.14/json/decoder.py:345, in JSONDecoder.decode(self, s, _w)
    340 def decode(self, s, _w=WHITESPACE.match):
    341     """Return the Python representation of ``s`` (a ``str`` instance
    342     containing a JSON document).
    343
    344     """
--> 345     obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    346     end = _w(s, end).end()
    347     if end != len(s):

File ~/.local/share/uv/python/cpython-3.14.0-macos-aarch64-none/lib/python3.14/json/decoder.py:363, in JSONDecoder.raw_decode(self, s, idx)
    361     obj, end = self.scan_once(s, idx)
    362 except StopIteration as err:
--> 363     raise JSONDecodeError("Expecting value", s, err.value) from None
    364 return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

A quick fix should be really easy - basically checking in registry.py::get_global_func_metadata if it's an empty string. Further improvement would be adding metadata to Python-side registered functions, etc

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions