11use async_openai:: types:: chat:: {
22 ChatCompletionRequestSystemMessageArgs , ChatCompletionRequestUserMessageArgs ,
3- CreateChatCompletionRequest , CreateChatCompletionRequestArgs ,
3+ ChatCompletionStreamOptions , CreateChatCompletionRequest , CreateChatCompletionRequestArgs ,
44} ;
55
66#[ test]
@@ -26,3 +26,37 @@ fn chat_types_serde() {
2626 let deserialized: CreateChatCompletionRequest = serde_json:: from_str ( & serialized) . unwrap ( ) ;
2727 assert_eq ! ( request, deserialized) ;
2828}
29+
30+ #[ test]
31+ fn stream_options_none_fields_not_serialized ( ) {
32+ // When include_obfuscation is None, it should not appear in the serialized JSON.
33+ // This is important for OpenAI-compatible providers (like NVIDIA NIM) that reject unknown fields.
34+ let stream_options = ChatCompletionStreamOptions {
35+ include_usage : Some ( true ) ,
36+ include_obfuscation : None ,
37+ } ;
38+
39+ let serialized = serde_json:: to_string ( & stream_options) . unwrap ( ) ;
40+
41+ // Verify include_usage is present
42+ assert ! ( serialized. contains( "include_usage" ) ) ;
43+ // Verify include_obfuscation is NOT present (not even as null)
44+ assert ! (
45+ !serialized. contains( "include_obfuscation" ) ,
46+ "include_obfuscation should not be serialized when None, but got: {}" ,
47+ serialized
48+ ) ;
49+
50+ // Test when both are None
51+ let stream_options_empty = ChatCompletionStreamOptions {
52+ include_usage : None ,
53+ include_obfuscation : None ,
54+ } ;
55+
56+ let serialized_empty = serde_json:: to_string ( & stream_options_empty) . unwrap ( ) ;
57+ assert_eq ! ( serialized_empty, "{}" ) ;
58+
59+ // Test roundtrip deserialization
60+ let deserialized: ChatCompletionStreamOptions = serde_json:: from_str ( & serialized) . unwrap ( ) ;
61+ assert_eq ! ( stream_options, deserialized) ;
62+ }
0 commit comments