From 1fa11f42b152ffe55cddb7439e4659136c860c7d Mon Sep 17 00:00:00 2001 From: Lucas Kabela Date: Tue, 14 Oct 2025 14:18:42 +0000 Subject: [PATCH] [Bugfix][vLLM] Explicitly do not support instead of crashing for named tuples in infer schema (#165191) Fixes https://github.com/vllm-project/vllm/issues/25270 by being explicit in erroring; previously we had a cryptic `__origin__ undefined` error, but now should give proper error message that we don't support NamedTuples in schema Test with ``` python test/test_custom_ops.py TestCustomOp.test_unsupported_param_types ``` Pull Request resolved: https://github.com/pytorch/pytorch/pull/165191 Approved by: https://github.com/zou3519 --- test/test_custom_ops.py | 10 ++++++++++ torch/_library/infer_schema.py | 5 ++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/test/test_custom_ops.py b/test/test_custom_ops.py index 4838e73f1f4c..5898f5a346ba 100644 --- a/test/test_custom_ops.py +++ b/test/test_custom_ops.py @@ -1069,6 +1069,16 @@ class TestCustomOp(CustomOpTestCaseBase): del foo + # Define a named tuple for a Point with x and y coordinates + Point = collections.namedtuple("Point", ["x", "y"]) + with self.assertRaisesRegex(ValueError, "unsupported type"): + + @custom_ops.custom_op(f"{TestCustomOp.test_ns}::foo") + def foo(x: Tensor, y: Point) -> Tensor: + raise NotImplementedError + + del foo + def test_supported_schemas(self): # All of these should already be tested by PyTorch codegen # (we share the same mechanism), but here's a sanity check. diff --git a/torch/_library/infer_schema.py b/torch/_library/infer_schema.py index b9258c9dd037..05fe47cd3733 100644 --- a/torch/_library/infer_schema.py +++ b/torch/_library/infer_schema.py @@ -132,7 +132,10 @@ def infer_schema( "as it is a ScriptObject. Please manually specify the schema " "using the `schema=` kwarg with the actual type of the ScriptObject." ) - elif annotation_type.__origin__ is tuple: + elif ( + hasattr(annotation_type, "__origin__") + and annotation_type.__origin__ is tuple + ): list_type = tuple_to_list(annotation_type) example_type_str = "\n\n" # Only suggest the list type if this type is supported.