@@ -73,18 +73,87 @@ impl PricingEndpoint {
7373 }
7474 }
7575
76- /// Returns whether a body contains usable pricing data for this endpoint.
76+ /// Returns whether a body has a non-empty pricing shape for this endpoint.
7777 pub fn validates ( self , json : & str ) -> bool {
78- let mut map = PricingMap :: default ( ) ;
78+ let Ok ( Value :: Object ( entries) ) = serde_json:: from_str :: < Value > ( json) else {
79+ return false ;
80+ } ;
81+
7982 match self {
80- Self :: LiteLlm => map. load_json ( json) > 0 ,
81- Self :: ModelsDev => map
82- . load_models_dev_json_missing ( json)
83- . is_some_and ( |loaded_count| loaded_count > 0 ) ,
83+ Self :: LiteLlm => entries. values ( ) . any ( litellm_entry_has_required_cost) ,
84+ Self :: ModelsDev => models_dev_object_has_required_shape ( & entries) ,
8485 }
8586 }
8687}
8788
89+ fn litellm_entry_has_required_cost ( value : & Value ) -> bool {
90+ let Some ( entry) = value. as_object ( ) else {
91+ return false ;
92+ } ;
93+ let full = entry
94+ . get ( "input_cost_per_token" )
95+ . is_some_and ( Value :: is_number)
96+ && entry
97+ . get ( "output_cost_per_token" )
98+ . is_some_and ( Value :: is_number) ;
99+ let compact = entry. get ( "i" ) . is_some_and ( Value :: is_number)
100+ && entry. get ( "o" ) . is_some_and ( Value :: is_number) ;
101+ full || compact
102+ }
103+
104+ fn models_dev_object_has_required_shape ( entries : & serde_json:: Map < String , Value > ) -> bool {
105+ if entries. values ( ) . any ( models_dev_entry_has_models_field) {
106+ return entries. values ( ) . all ( models_dev_provider_has_required_shape)
107+ && entries. values ( ) . any ( models_dev_provider_has_required_cost) ;
108+ }
109+ entries. values ( ) . all ( models_dev_entry_has_required_cost)
110+ && entries. values ( ) . any ( models_dev_entry_has_nonzero_cost)
111+ }
112+
113+ fn models_dev_provider_has_required_shape ( value : & Value ) -> bool {
114+ let Some ( provider) = value. as_object ( ) else {
115+ return false ;
116+ } ;
117+ let Some ( models) = provider. get ( "models" ) . and_then ( Value :: as_object) else {
118+ return false ;
119+ } ;
120+ models. values ( ) . all ( Value :: is_object)
121+ }
122+
123+ fn models_dev_provider_has_required_cost ( value : & Value ) -> bool {
124+ value
125+ . as_object ( )
126+ . and_then ( |provider| provider. get ( "models" ) )
127+ . and_then ( Value :: as_object)
128+ . is_some_and ( |models| models. values ( ) . any ( models_dev_model_has_required_cost) )
129+ }
130+
131+ fn models_dev_model_has_required_cost ( value : & Value ) -> bool {
132+ value
133+ . as_object ( )
134+ . and_then ( |model| model. get ( "cost" ) )
135+ . and_then ( Value :: as_object)
136+ . is_some_and ( models_dev_cost_has_required_rates)
137+ }
138+
139+ fn models_dev_entry_has_nonzero_cost ( value : & Value ) -> bool {
140+ value
141+ . as_object ( )
142+ . and_then ( |entry| entry. get ( "cost" ) )
143+ . and_then ( Value :: as_object)
144+ . is_some_and ( models_dev_cost_has_required_rates)
145+ }
146+
147+ fn models_dev_cost_has_required_rates ( cost : & serde_json:: Map < String , Value > ) -> bool {
148+ let Some ( input) = cost. get ( "input" ) . and_then ( Value :: as_f64) else {
149+ return false ;
150+ } ;
151+ let Some ( output) = cost. get ( "output" ) . and_then ( Value :: as_f64) else {
152+ return false ;
153+ } ;
154+ input != 0.0 || output != 0.0
155+ }
156+
88157#[ derive( Debug , Clone , Copy ) ]
89158pub struct Pricing {
90159 pub input : f64 ,
@@ -2234,10 +2303,15 @@ mod tests {
22342303 r#"{"openai":{"models":{"gpt-test":{"cost":{"input":1.0,"output":2.0}}}}}"# ;
22352304
22362305 assert ! ( PricingEndpoint :: LiteLlm . validates( litellm) ) ;
2306+ assert ! ( PricingEndpoint :: LiteLlm . validates( r#"{"gpt-test":{"i":1.0,"o":2.0}}"# ) ) ;
22372307 assert ! ( !PricingEndpoint :: LiteLlm . validates( models_dev) ) ;
22382308 assert ! ( PricingEndpoint :: ModelsDev . validates( models_dev) ) ;
22392309 assert ! ( !PricingEndpoint :: ModelsDev . validates( litellm) ) ;
22402310 assert ! ( !PricingEndpoint :: ModelsDev . validates( "{}" ) ) ;
2311+ assert ! (
2312+ !PricingEndpoint :: ModelsDev
2313+ . validates( r#"{"openai":{"models":{"free":{"cost":{"input":0.0,"output":0.0}}}}}"# )
2314+ ) ;
22412315 }
22422316
22432317 #[ test]
0 commit comments