Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to remove the "format" field for Numeric properties? #345

Open
jBernavaPrah opened this issue Sep 30, 2024 · 2 comments
Open

How to remove the "format" field for Numeric properties? #345

jBernavaPrah opened this issue Sep 30, 2024 · 2 comments

Comments

@jBernavaPrah
Copy link

Hi,

firstly thanks for this crate! Very useful!

As mentioned in the title, how can I programmatically remove the "format" from the json schema?

I'm using the v1.

use schemars::{ schema_for, JsonSchema};


#[derive(JsonSchema)]
struct MyTest {
    value: i32,
}

fn main() {
    let schema = schema_for!(MyTest);
    
   println!("{}", serde_json::to_string_pretty(&schema).unwrap());
    
    // output:
    // {
    //   "$schema": "https://json-schema.org/draft/2020-12/schema",
    //   "title": "MyTest",
    //   "type": "object",
    //   "properties": {
    //     "value": {
    //       "type": "integer",
    //       "format": "int32" <-- this one!
    //     }
    //   },
    //   "required": [
    //     "value"
    //   ]
    // }
    
}

@shmy
Copy link

shmy commented Jan 28, 2025

Some issues

@IlyaBizyaev
Copy link

This is required to use schemars with ChatGPT's structured outputs. They don't support format and other validation keywords.

I found schemars after reading https://github.com/64bit/async-openai/blob/main/examples/structured-outputs-schemars/src/main.rs.

As a quick hack for my simple schema:

fn remove_property_format(schema: &mut schemars::schema::RootSchema) {
    let object_mut = schema.schema.object();
    for property in object_mut.properties.values_mut() {
        if let schemars::schema::Schema::Object(obj) = property {
            obj.format = None;
        }
    }
}

Used like this:

let mut schema = schema_for!(T);
remove_property_format(&mut schema);
let schema_value = serde_json::to_value(&schema)?;

Also note that you should use signed numbers, or schemars will insert "minimum": 0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants