pub struct JsonItem {
pub type_name: String,
pub properties: FxHashMap<String, ItemType>,
pub required: Option<Vec<String>>,
pub additional_properties: bool,
}
Expand description
Represents a JSON object structure with properties, requirements, and constraints.
This structure defines a JSON object schema including its properties, which fields are required, and whether additional properties are allowed. It’s used to build complex nested object structures within JSON schemas.
§Fields
type_name
- Always “object” for object schemasproperties
- Map of property names to their type definitionsrequired
- List of property names that must be presentadditional_properties
- Whether properties not defined in schema are allowed
§Schema Validation
- Required Fields: Properties listed in
required
must be present in valid JSON - Type Validation: Each property must match its defined type
- Additional Properties: When false, only defined properties are allowed
§Example
use openai_tools::structured_output::{JsonItem, ItemType};
use fxhash::FxHashMap;
// Create properties map
let mut properties = FxHashMap::default();
properties.insert(
"name".to_string(),
ItemType::new("string".to_string(), Some("Person's name".to_string()))
);
properties.insert(
"age".to_string(),
ItemType::new("number".to_string(), Some("Person's age".to_string()))
);
// Create object schema
let person_schema = JsonItem::new("object", properties);
Fields§
§type_name: String
§properties: FxHashMap<String, ItemType>
§required: Option<Vec<String>>
§additional_properties: bool
Implementations§
Source§impl JsonItem
impl JsonItem
Sourcepub fn new(type_name: &str, properties: FxHashMap<String, ItemType>) -> Self
pub fn new(type_name: &str, properties: FxHashMap<String, ItemType>) -> Self
Creates a new JsonItem
object schema with the specified properties.
All properties provided will automatically be marked as required. Additional properties are disabled by default for strict validation.
§Arguments
type_name
- The type name (typically “object”)properties
- Map of property names to their type definitions
§Returns
A new JsonItem
with all provided properties marked as required.
§Example
use openai_tools::structured_output::{JsonItem, ItemType};
use fxhash::FxHashMap;
let mut props = FxHashMap::default();
props.insert("id".to_string(), ItemType::new("number".to_string(), None));
props.insert("title".to_string(), ItemType::new("string".to_string(), None));
let schema = JsonItem::new("object", props);
// Both "id" and "title" will be required
Sourcepub fn default() -> Self
pub fn default() -> Self
Creates a default empty object schema.
Returns a new JsonItem
representing an empty object with no properties,
no required fields, and additional properties disabled.
§Returns
A new empty JsonItem
ready for property addition.
§Example
use openai_tools::structured_output::{JsonItem, ItemType};
let mut schema = JsonItem::default();
// Add properties later using add_property()
Sourcepub fn add_property(&mut self, prop_name: String, item: ItemType)
pub fn add_property(&mut self, prop_name: String, item: ItemType)
Adds a property to this object schema and marks it as required.
This method adds a new property to the schema and automatically updates the required fields list to include this property.
§Arguments
prop_name
- The name of the property to additem
- The type definition for this property
§Example
use openai_tools::structured_output::{JsonItem, ItemType};
let mut schema = JsonItem::default();
let string_prop = ItemType::new("string".to_string(), Some("A name".to_string()));
schema.add_property("name".to_string(), string_prop);
// "name" is now a required property
Sourcepub fn add_array(&mut self, prop_name: String, items: JsonItem)
pub fn add_array(&mut self, prop_name: String, items: JsonItem)
Adds an array property with the specified item structure.
This method creates an array property where each element conforms
to the provided JsonItem
structure. The array property is automatically
marked as required.
§Arguments
prop_name
- The name of the array propertyitems
- The schema definition for array elements
§Example
use openai_tools::structured_output::{JsonItem, ItemType};
use fxhash::FxHashMap;
let mut schema = JsonItem::default();
// Define structure for array items
let mut item_props = FxHashMap::default();
item_props.insert("id".to_string(), ItemType::new("number".to_string(), None));
item_props.insert("name".to_string(), ItemType::new("string".to_string(), None));
let item_schema = JsonItem::new("object", item_props);
schema.add_array("items".to_string(), item_schema);
Trait Implementations§
Source§impl<'de> Deserialize<'de> for JsonItem
impl<'de> Deserialize<'de> for JsonItem
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Auto Trait Implementations§
impl Freeze for JsonItem
impl RefUnwindSafe for JsonItem
impl Send for JsonItem
impl Sync for JsonItem
impl Unpin for JsonItem
impl UnwindSafe for JsonItem
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more