From 586a3abdf8674cb54d0df08e9094cc7ada761f0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=AD=E5=93=B2=E6=96=87?= Date: Tue, 18 Aug 2026 16:46:15 +0800 Subject: [PATCH] Preserve required argument order in errors --- src/openai/_utils/_utils.py | 3 +-- tests/test_required_args.py | 7 ++++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/openai/_utils/_utils.py b/src/openai/_utils/_utils.py index 02046a352c..7947af5f25 100644 --- a/src/openai/_utils/_utils.py +++ b/src/openai/_utils/_utils.py @@ -288,8 +288,7 @@ def wrapper(*args: object, **kwargs: object) -> object: else: assert len(variants) > 0 - # TODO: this error message is not deterministic - missing = list(set(variants[0]) - given_params) + missing = [param for param in variants[0] if param not in given_params] if len(missing) > 1: msg = f"Missing required arguments: {human_join([quote(arg) for arg in missing])}" else: diff --git a/tests/test_required_args.py b/tests/test_required_args.py index 5d1a5224ff..78b8675a46 100644 --- a/tests/test_required_args.py +++ b/tests/test_required_args.py @@ -47,10 +47,11 @@ def foo(a: str = "", *, b: str = "", c: str = "") -> str | None: assert foo(a="a", b="b", c="c") == "a b c" - error_message = r"Missing required arguments.*" - - with pytest.raises(TypeError, match=error_message): + with pytest.raises(TypeError) as exc_info: foo() + assert str(exc_info.value) == "Missing required arguments: 'a', 'b' or 'c'" + + error_message = r"Missing required arguments.*" with pytest.raises(TypeError, match=error_message): foo(a="a")