@@ -97,28 +97,32 @@ def test_partial():
9797 }, "Partial model JSON schema has changed"
9898
9999
100+ partial_chunks = ["\n " , "\t " , " " , "\x00 " , '{"a": 42, "b": {"b": 1}}' ]
101+ expected_sync_models = [
102+ # First model has default values (nested models show their fields as None)
103+ {"a" : None , "b" : {"b" : None }},
104+ {"a" : None , "b" : {"b" : None }},
105+ {"a" : None , "b" : {"b" : None }},
106+ {"a" : None , "b" : {"b" : None }},
107+ # Last model has all fields populated from JSON
108+ {"a" : 42 , "b" : {"b" : 1 }},
109+ ]
110+ expected_async_models = [
111+ {"a" : None , "b" : {"b" : None }},
112+ {"a" : None , "b" : {"b" : None }},
113+ {"a" : None , "b" : {"b" : None }},
114+ {"a" : None , "b" : {"b" : None }},
115+ {"a" : 42 , "b" : {"b" : 1 }},
116+ ]
117+
118+
100119def test_partial_with_whitespace ():
101120 partial = Partial [SamplePartial ]
102-
103121 # Get the actual models from chunks - must provide complete data for final validation
104- models = list (
105- partial .model_from_chunks (["\n " , "\t " , " " , '{"a": 42, "b": {"b": 1}}' ])
106- )
107-
108- # Print actual values for debugging
109- print (f"Number of models: { len (models )} " )
122+ models = list (partial .model_from_chunks (partial_chunks ))
123+ assert len (models ) == len (expected_sync_models )
110124 for i , model in enumerate (models ):
111- print (f"Model { i } : { model .model_dump ()} " )
112-
113- # Actual behavior: When whitespace chunks are processed, we may get models
114- # First model has default values (nested models show their fields as None)
115- assert models [0 ].model_dump () == {"a" : None , "b" : {"b" : None }}
116-
117- # Last model has all fields populated from JSON
118- assert models [- 1 ].model_dump () == {"a" : 42 , "b" : {"b" : 1 }}
119-
120- # Check we have the expected number of models (2 instead of 4)
121- assert len (models ) == 2
125+ assert model .model_dump () == expected_sync_models [i ]
122126
123127
124128@pytest .mark .asyncio
@@ -127,23 +131,15 @@ async def test_async_partial_with_whitespace():
127131
128132 # Handle any leading whitespace from the model - must provide complete data for final validation
129133 async def async_generator ():
130- for chunk in [ " \n " , " \t " , " " , '{"a": 42, "b": {"b": 1}}' ] :
134+ for chunk in partial_chunks :
131135 yield chunk
132136
133- # With completeness-based validation, nested models are constructed with None fields
134- expected_model_dicts = [
135- {"a" : None , "b" : {"b" : None }},
136- {"a" : None , "b" : {"b" : None }},
137- {"a" : None , "b" : {"b" : None }},
138- {"a" : 42 , "b" : {"b" : 1 }},
139- ]
140-
141137 i = 0
142138 async for model in partial .model_from_chunks_async (async_generator ()):
143- assert model .model_dump () == expected_model_dicts [i ]
139+ # Expected behavior: When whitespace chunks are processed, we should always get a model
140+ assert model .model_dump () == expected_async_models [i ]
144141 i += 1
145-
146- assert model .model_dump () == {"a" : 42 , "b" : {"b" : 1 }}
142+ assert i == len (expected_async_models )
147143
148144
149145@pytest .mark .skipif (not os .getenv ("OPENAI_API_KEY" ), reason = "OPENAI_API_KEY not set" )
0 commit comments