From 85b836487dbca485e18b909828ba08bcc0aba7f5 Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 2 Jul 2024 19:00:19 +0100 Subject: [PATCH] Make the order of array items consistent using sorted() --- src/transformers/utils/chat_template_utils.py | 2 +- tests/utils/test_chat_template_utils.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/transformers/utils/chat_template_utils.py b/src/transformers/utils/chat_template_utils.py index ee6173f2a153..414d2fb72454 100644 --- a/src/transformers/utils/chat_template_utils.py +++ b/src/transformers/utils/chat_template_utils.py @@ -80,7 +80,7 @@ def _parse_type_hint(hint: str) -> Dict: return_dict = subtypes[0] elif all(isinstance(subtype["type"], str) for subtype in subtypes): # A union of basic types can be expressed as a list in the schema - return_dict = {"type": [subtype["type"] for subtype in subtypes]} + return_dict = {"type": sorted([subtype["type"] for subtype in subtypes])} else: # A union of more complex types requires "anyOf" return_dict = {"anyOf": subtypes} diff --git a/tests/utils/test_chat_template_utils.py b/tests/utils/test_chat_template_utils.py index cff31c1f8a34..1816ddd95126 100644 --- a/tests/utils/test_chat_template_utils.py +++ b/tests/utils/test_chat_template_utils.py @@ -137,7 +137,7 @@ def fn(x: List[List[Union[str, int]]]): "properties": { "x": { "type": "array", - "items": {"type": "array", "items": {"type": ["string", "integer"]}}, + "items": {"type": "array", "items": {"type": ["integer", "string"]}}, "description": "The input", } }, @@ -455,13 +455,13 @@ def fn( }, "y": { "type": "array", - "items": {"type": ["string", "integer"]}, + "items": {"type": ["integer", "string"]}, "nullable": True, "description": "The second input. It's a big list with a single-line description.", }, "z": { "type": "array", - "prefixItems": [{"type": ["string", "integer"]}, {"type": "string"}], + "prefixItems": [{"type": ["integer", "string"]}, {"type": "string"}], "description": "The third input. It's some kind of tuple with a default arg.", }, },