From bbd21b2267e7bedca34edde369bbb8b8514504fe Mon Sep 17 00:00:00 2001 From: Junhyuk Lee Date: Mon, 25 May 2026 00:21:06 -0500 Subject: [PATCH] fix(parse): add parentheses for operator precedence in minItems check The condition on line 149 of _transform.py evaluated as: (min_items is not None and min_items == 0) or (min_items == 1) instead of the intended: min_items is not None and (min_items == 0 or min_items == 1) While both produce the same result for all practical inputs, the explicit parentheses clarify intent and prevent future misreadings. Adds test coverage for minItems 0 and 1 cases. --- src/anthropic/lib/_parse/_transform.py | 2 +- tests/lib/_parse/test_transform.py | 32 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/anthropic/lib/_parse/_transform.py b/src/anthropic/lib/_parse/_transform.py index 755ad280d..60353360d 100644 --- a/src/anthropic/lib/_parse/_transform.py +++ b/src/anthropic/lib/_parse/_transform.py @@ -146,7 +146,7 @@ def transform_schema( strict_schema["items"] = transform_schema(items) min_items = json_schema.pop("minItems", None) - if min_items is not None and min_items == 0 or min_items == 1: + if min_items is not None and (min_items == 0 or min_items == 1): strict_schema["minItems"] = min_items elif min_items is not None: # add it back so its treated as an extra property and appended to the description diff --git a/tests/lib/_parse/test_transform.py b/tests/lib/_parse/test_transform.py index 443ca99c9..b1eeb5ffd 100644 --- a/tests/lib/_parse/test_transform.py +++ b/tests/lib/_parse/test_transform.py @@ -117,6 +117,38 @@ def test_array_schema(): ) +def test_array_schema_min_items_zero(): + schema = { + "type": "array", + "items": {"type": "string"}, + "minItems": 0, + } + result = transform_schema(schema) + assert result == snapshot( + { + "type": "array", + "items": {"type": "string"}, + "minItems": 0, + } + ) + + +def test_array_schema_min_items_one(): + schema = { + "type": "array", + "items": {"type": "string"}, + "minItems": 1, + } + result = transform_schema(schema) + assert result == snapshot( + { + "type": "array", + "items": {"type": "string"}, + "minItems": 1, + } + ) + + def test_string_schema_with_format_and_default(): schema = { "type": "string",