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

Types are always arrays, not compatible with some parsers #14

Open
ngsilverman opened this issue Aug 23, 2023 · 2 comments
Open

Types are always arrays, not compatible with some parsers #14

ngsilverman opened this issue Aug 23, 2023 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@ngsilverman
Copy link

The README states that this:

(json-schema.infer/infer-strict
    {:title "ent-1"})
    {:things [{:quantity 1}]})

Will generate this:

{:$schema "http://json-schema.org/draft-07/schema#"
 :title "ent-1"
 :type :object
 :additionalProperties false
 :properties {"things" {:type :array
                        :items {:type :object
                                :additionalProperties false
                                :properties {"quantity" {:type :integer}}
                                :required ["quantity"]}}}
 :required ["things"]}

However, for me, using version 0.4.1, it generates this:

{:$schema "http://json-schema.org/draft-07/schema#",
 :title "ent-1",
 :type #{:object},
 :additionalProperties false,
 :properties {"things" {:type #{:array},
                        :items {:type #{:object},
                                :additionalProperties false,
                                :properties {"quantity" {:type #{:integer}}},
                                :required #{"quantity"}}}},
 :required #{"things"}}

Notably, the types are sets, which gets correctly converted to JSON lists, and which is technically a valid schema but can cause issues. For example the OpenAI API doesn't recognize that format, it expects a single type rather than an array of types.

If there is only one type I would suggest making it a single value rather than an array.

Thanks for this library! The infer function is awesome!

@luposlip
Copy link
Owner

Makes sense, I'll look into that. I'm kind of busy these weeks, but I'll definitely prioritize it as soon as I can.

Thanks for your issue! 😃

@luposlip luposlip self-assigned this Aug 23, 2023
@luposlip luposlip added the enhancement New feature or request label Aug 23, 2023
@ngsilverman
Copy link
Author

Thanks for the prompt reply! I'm happy to help if you can give me some pointers.

In the meantime for anyone else running into this here's a quick fix that goes through the inferred JSON schema and updates the type values:

(defn walk-update [m k f]
  (clojure.walk/postwalk (fn [x] (if (and (map? x) (contains? x k))
                                   (update x k f)
                                   x))
                         m))

(defn infer [params & docs]
  (walk-update
    (apply (partial json-schema.infer/infer params) docs)
    :type
    #(if (and (coll? %) (= (count %) 1))
       (first %)
       %)))

@luposlip luposlip pinned this issue Aug 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants