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

Make JsonSchema.fromSerializableSchema package private #3170

Merged
merged 1 commit into from
Sep 26, 2024

Conversation

guersam
Copy link
Contributor

@guersam guersam commented Sep 26, 2024

While working with OpenAI structured outputs, I need an uncommon requirement like omitting format field from the schema.

To overcome such technical constraint, I need an escape hatch between JsonSchema and SerializableJsonSchema for a workaround like the below:

package zio.http.endpoint.openapi

import zio.prelude.Newtype
import zio.schema.Schema

object OpenAIJsonSchema extends Newtype[JsonSchema] {

  given Schema[Type] =
    SerializableJsonSchema.schema.transform[Type](
      ss => wrap(JsonSchema.fromSerializableSchema(ss)), // `JsonSchema.fromSerializableSchema` is inaccessible
      s => {
        val ss = unwrap(s).toSerializableSchema
        println(s)
        println(unwrap(s).toJson)
        adjustForOpenAI(ss)
      }
    )

  def fromZSchema(s: Schema[?]): Type =
    wrap(JsonSchema.fromZSchema(s, JsonSchema.SchemaStyle.Inline))

  private def adjustForOpenAI(s: SerializableJsonSchema): SerializableJsonSchema =
    s.copy(
      format = None,
      properties = s.properties.map(_.view.mapValues(adjustForOpenAI).toMap),
      items = s.items.map(adjustForOpenAI),
      additionalProperties =
        Option.when(s.properties.nonEmpty) {
          BoolOrSchema.BooleanWrapper(false)
        }
    )
}
type OpenAIJsonSchema = OpenAIJsonSchema.Type

@guersam
Copy link
Contributor Author

guersam commented Sep 26, 2024

Failed test:

    - AuthSpec - With server - Auth from query parameter with context and endpoint client
      Exception in thread "zio-fiber-174869" zio.http.codec.HttpCodecError$MalformedStatus: Expected status code BadRequest but found NotFound
      	at zio.http.endpoint.AuthSpec.spec.response(AuthSpec.scala:204)
      	at zio.http.endpoint.AuthSpec.spec.response(AuthSpec.scala:205)
      	at zio.http.endpoint.AuthSpec.spec(AuthSpec.scala:213)
      	at zio.http.endpoint.AuthSpec.spec(AuthSpec.scala:214)

This spec is failing consistently on the current main branch since last week. Maybe there's a race among the parallel cases on http://localhost:8080?

@987Nabil 987Nabil merged commit fb96e3f into zio:main Sep 26, 2024
35 checks passed
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

Successfully merging this pull request may close these issues.

2 participants