@@ -8,6 +8,9 @@ pub struct ToJsonSchemaOptions {
88 /// Use union type (with `null`) for optional fields instead.
99 /// Models like OpenAI will reject the schema if a field is not required.
1010 pub fields_always_required : bool ,
11+
12+ /// If true, the JSON schema supports the `format` keyword.
13+ pub supports_format : bool ,
1114}
1215
1316pub trait ToJsonSchema {
@@ -54,27 +57,46 @@ impl ToJsonSchema for schema::BasicValueType {
5457 }
5558 schema:: BasicValueType :: Uuid => {
5659 schema. instance_type = Some ( SingleOrVec :: Single ( Box :: new ( InstanceType :: String ) ) ) ;
57- schema. format = Some ( "uuid" . to_string ( ) ) ;
60+ if options. supports_format {
61+ schema. format = Some ( "uuid" . to_string ( ) ) ;
62+ } else {
63+ schema. metadata . get_or_insert_default ( ) . description =
64+ Some ( "A UUID, e.g. 123e4567-e89b-12d3-a456-426614174000" . to_string ( ) ) ;
65+ }
5866 }
5967 schema:: BasicValueType :: Date => {
6068 schema. instance_type = Some ( SingleOrVec :: Single ( Box :: new ( InstanceType :: String ) ) ) ;
61- schema. format = Some ( "date" . to_string ( ) ) ;
69+ if options. supports_format {
70+ schema. format = Some ( "date" . to_string ( ) ) ;
71+ } else {
72+ schema. metadata . get_or_insert_default ( ) . description =
73+ Some ( "A date, e.g. 2025-03-27" . to_string ( ) ) ;
74+ }
6275 }
6376 schema:: BasicValueType :: Time => {
6477 schema. instance_type = Some ( SingleOrVec :: Single ( Box :: new ( InstanceType :: String ) ) ) ;
65- schema. format = Some ( "time" . to_string ( ) ) ;
78+ if options. supports_format {
79+ schema. format = Some ( "time" . to_string ( ) ) ;
80+ } else {
81+ schema. metadata . get_or_insert_default ( ) . description =
82+ Some ( "A time, e.g. 13:32:12" . to_string ( ) ) ;
83+ }
6684 }
6785 schema:: BasicValueType :: LocalDateTime => {
6886 schema. instance_type = Some ( SingleOrVec :: Single ( Box :: new ( InstanceType :: String ) ) ) ;
69- schema. format = Some ( "date-time" . to_string ( ) ) ;
87+ if options. supports_format {
88+ schema. format = Some ( "date-time" . to_string ( ) ) ;
89+ }
7090 schema. metadata . get_or_insert_default ( ) . description =
71- Some ( "Date time without timezone offset, YYYY-MM-DDThh:mm:ss " . to_string ( ) ) ;
91+ Some ( "Date time without timezone offset, e.g. 2025-03-27T13:32:12 " . to_string ( ) ) ;
7292 }
7393 schema:: BasicValueType :: OffsetDateTime => {
7494 schema. instance_type = Some ( SingleOrVec :: Single ( Box :: new ( InstanceType :: String ) ) ) ;
75- schema. format = Some ( "date-time" . to_string ( ) ) ;
95+ if options. supports_format {
96+ schema. format = Some ( "date-time" . to_string ( ) ) ;
97+ }
7698 schema. metadata . get_or_insert_default ( ) . description =
77- Some ( "Date time with timezone offset in RFC3339" . to_string ( ) ) ;
99+ Some ( "Date time with timezone offset in RFC3339, e.g. 2025-03-27T13:32:12Z, 2025-03-27T07:32:12.313-06:00 " . to_string ( ) ) ;
78100 }
79101 schema:: BasicValueType :: Json => {
80102 // Can be any value. No type constraint.
0 commit comments