From 29daf934a412238df2fb9c0609c64cf04450bf3b Mon Sep 17 00:00:00 2001 From: Matt Hill Date: Wed, 26 Nov 2025 08:22:25 +0000 Subject: [PATCH 1/2] Openapi schema gen preserve order of field remap During field remapping stage of openapi schema generation the order of iteration is not consistent across runs. This leads to non-deterministic schema generation Preserve the order of this iteration across runs by sorting by the "new_name", i.e. the value inserted in this mapping --- fastapi/_compat/v2.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/fastapi/_compat/v2.py b/fastapi/_compat/v2.py index 5cd49343b..4e288574a 100644 --- a/fastapi/_compat/v2.py +++ b/fastapi/_compat/v2.py @@ -302,7 +302,10 @@ def _remap_definitions_and_field_mappings( Dict[str, Any], ]: old_name_to_new_name_map = {} - for field_key, schema in field_mapping.items(): + for field_key, schema in sorted( + field_mapping.items(), + key=lambda x: model_name_map.get(x[0][0].type_), + ): model = field_key[0].type_ if model not in model_name_map: continue From 931dc1fbe107baaf282bb6a309e236e91e7e1e62 Mon Sep 17 00:00:00 2001 From: Matt Hill Date: Wed, 26 Nov 2025 08:50:15 +0000 Subject: [PATCH 2/2] fixup --- fastapi/_compat/v2.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fastapi/_compat/v2.py b/fastapi/_compat/v2.py index 4e288574a..fa8748be7 100644 --- a/fastapi/_compat/v2.py +++ b/fastapi/_compat/v2.py @@ -304,7 +304,7 @@ def _remap_definitions_and_field_mappings( old_name_to_new_name_map = {} for field_key, schema in sorted( field_mapping.items(), - key=lambda x: model_name_map.get(x[0][0].type_), + key=lambda x: model_name_map.get(x[0][0].type_, ""), ): model = field_key[0].type_ if model not in model_name_map: