Skip to content

Commit 364f89e

Browse files
committed
Skip serializing None fields in ChatCompletionStreamOptions
Add serde attribute to omit None values for include_usage and include_obfuscation. Add tests to verify correct serialization and deserialization behavior.
1 parent 839ff51 commit 364f89e

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

async-openai/src/types/chat/chat_.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,6 +1029,7 @@ pub struct ChatCompletionStreamOptions {
10291029
/// All other chunks will also include a `usage` field, but with a null
10301030
/// value. **NOTE:** If the stream is interrupted, you may not receive the
10311031
/// final usage chunk which contains the total token usage for the request.
1032+
#[serde(skip_serializing_if = "Option::is_none")]
10321033
pub include_usage: Option<bool>,
10331034

10341035
/// When true, stream obfuscation will be enabled. Stream obfuscation adds
@@ -1038,6 +1039,7 @@ pub struct ChatCompletionStreamOptions {
10381039
/// of overhead to the data stream. You can set `include_obfuscation` to
10391040
/// false to optimize for bandwidth if you trust the network links between
10401041
/// your application and the OpenAI API.
1042+
#[serde(skip_serializing_if = "Option::is_none")]
10411043
pub include_obfuscation: Option<bool>,
10421044
}
10431045

async-openai/tests/ser_de.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use 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

Comments
 (0)