Struct JsonItem

Source
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 schemas
  • properties - Map of property names to their type definitions
  • required - List of property names that must be present
  • additional_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

Source

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
Source

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()
Source

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 add
  • item - 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
Source

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 property
  • items - 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);
Source

pub fn clone(&self) -> Self

Creates a deep clone of this JsonItem instance.

This method performs a deep copy of all properties and nested structures, ensuring that modifications to the clone don’t affect the original.

§Returns

A new JsonItem instance that is an exact copy of this one.

Trait Implementations§

Source§

impl Clone for JsonItem

Source§

fn clone(&self) -> JsonItem

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for JsonItem

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for JsonItem

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for JsonItem

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> ErasedDestructor for T
where T: 'static,