Skip to main content

systemprompt_models/schema/
capabilities.rs

1//! Per-provider JSON-Schema capability matrices.
2//!
3//! [`ProviderCapabilities`] declares which JSON-Schema constructs a provider's
4//! tool/output schema parser accepts. It is the input to
5//! [`super::SchemaSanitizer`], which strips everything a provider does not
6//! support. The matrices live here in `shared/models` so both the gateway wire
7//! codecs and the agent-flow provider clients resolve the same authority; the
8//! wire protocol picks one via
9//! [`crate::services::WireProtocol::schema_capabilities`].
10//!
11//! Copyright (c) systemprompt.io — Business Source License 1.1.
12//! See <https://systemprompt.io> for licensing details.
13
14use serde_json::Value;
15
16#[derive(Debug, Clone, Copy, PartialEq, Eq)]
17pub struct SchemaComposition {
18    pub allof: bool,
19    pub anyof: bool,
20    pub oneof: bool,
21    pub if_then_else: bool,
22    pub not: bool,
23}
24
25#[expect(
26    clippy::struct_excessive_bools,
27    reason = "schema feature matrix: each bool is an independent JSON-Schema construct the \
28              provider does or does not accept, not state"
29)]
30#[derive(Debug, Clone, Copy, PartialEq, Eq)]
31pub struct SchemaFeatures {
32    pub references: bool,
33    pub definitions: bool,
34    pub additional_properties: bool,
35    pub const_values: bool,
36    pub exclusive_bounds: bool,
37    pub property_names: bool,
38    pub tuple_items: bool,
39}
40
41#[derive(Debug, Clone, Copy, PartialEq, Eq)]
42pub struct ProviderCapabilities {
43    pub composition: SchemaComposition,
44    pub features: SchemaFeatures,
45}
46
47impl ProviderCapabilities {
48    pub const fn anthropic() -> Self {
49        Self {
50            composition: SchemaComposition {
51                allof: true,
52                anyof: true,
53                oneof: true,
54                if_then_else: true,
55                not: true,
56            },
57            features: SchemaFeatures {
58                references: true,
59                definitions: true,
60                additional_properties: true,
61                const_values: true,
62                exclusive_bounds: true,
63                property_names: true,
64                tuple_items: true,
65            },
66        }
67    }
68
69    pub const fn openai() -> Self {
70        Self {
71            composition: SchemaComposition {
72                allof: true,
73                anyof: true,
74                oneof: true,
75                if_then_else: false,
76                not: false,
77            },
78            features: SchemaFeatures {
79                references: true,
80                definitions: true,
81                additional_properties: true,
82                const_values: true,
83                exclusive_bounds: true,
84                property_names: true,
85                tuple_items: true,
86            },
87        }
88    }
89
90    pub const fn gemini() -> Self {
91        Self {
92            composition: SchemaComposition {
93                allof: false,
94                anyof: true,
95                oneof: false,
96                if_then_else: false,
97                not: false,
98            },
99            features: SchemaFeatures {
100                references: false,
101                definitions: false,
102                additional_properties: false,
103                const_values: false,
104                exclusive_bounds: false,
105                property_names: false,
106                tuple_items: false,
107            },
108        }
109    }
110
111    pub fn requires_transformation(&self, schema: &Value) -> bool {
112        if let Some(obj) = schema.as_object() {
113            if obj.contains_key("allOf") && !self.composition.allof {
114                return true;
115            }
116            if obj.contains_key("anyOf") && !self.composition.anyof {
117                return true;
118            }
119            if obj.contains_key("oneOf") && !self.composition.oneof {
120                return true;
121            }
122            if obj.contains_key("if") && !self.composition.if_then_else {
123                return true;
124            }
125            if obj.contains_key("$ref") && !self.features.references {
126                return true;
127            }
128            if (obj.contains_key("definitions") || obj.contains_key("$defs"))
129                && !self.features.definitions
130            {
131                return true;
132            }
133            if obj.contains_key("not") && !self.composition.not {
134                return true;
135            }
136        }
137        false
138    }
139}