Skip to main content

gproxy_protocol/protocol/openai/
models.rs

1use std::collections::BTreeMap;
2
3use serde::{Deserialize, Serialize};
4
5use super::common::*;
6
7pub type ModelsWireModel = OpenAiWireModel<(), ModelListResponse>;
8pub type ModelRetrieveWireModel = OpenAiWireModel<(), Model>;
9
10#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, gproxy_protocol_macros::WireBuilder)]
11#[non_exhaustive]
12pub struct ModelListResponse {
13    pub data: Vec<Model>,
14    pub object: ListObjectType,
15    #[serde(default, flatten, skip_serializing_if = "BTreeMap::is_empty")]
16    pub extra: Extra,
17}
18
19#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, gproxy_protocol_macros::WireBuilder)]
20#[non_exhaustive]
21pub struct Model {
22    pub id: OpenAiModelId,
23    // OpenAI-compatible providers (e.g. DeepSeek) omit `created`; keep it
24    // optional so decoding their model list for a response transform doesn't
25    // fail on the missing field.
26    #[serde(default, skip_serializing_if = "Option::is_none")]
27    pub created: Option<u64>,
28    // gproxy extension: token limits surfaced from providers that report them
29    // (Claude, Gemini). The official OpenAI model object has no such fields.
30    #[serde(default, skip_serializing_if = "Option::is_none")]
31    pub max_input_tokens: Option<u64>,
32    #[serde(default, skip_serializing_if = "Option::is_none")]
33    pub max_output_tokens: Option<u64>,
34    pub object: ModelObjectType,
35    pub owned_by: String,
36    #[serde(default, flatten, skip_serializing_if = "BTreeMap::is_empty")]
37    pub extra: Extra,
38}
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43
44    /// DeepSeek (and other OpenAI-compatible providers) return model entries
45    /// without `created`; decoding for a response transform must not fail.
46    #[test]
47    fn model_decodes_without_created() {
48        let m: Model = serde_json::from_str(
49            r#"{"id":"deepseek-chat","object":"model","owned_by":"deepseek"}"#,
50        )
51        .expect("decode without created");
52        assert_eq!(m.created, None);
53        // …and a missing `created` is omitted on re-encode, not fabricated.
54        let s = serde_json::to_string(&m).unwrap();
55        assert!(!s.contains("created"), "{s}");
56    }
57}