Skip to content

Add typed createChatCompletion method for structured response handling#453

Merged
adamw merged 1 commit intosoftwaremill:masterfrom
jasonqu:add_typed_createChatCompletion
Mar 12, 2026
Merged

Add typed createChatCompletion method for structured response handling#453
adamw merged 1 commit intosoftwaremill:masterfrom
jasonqu:add_typed_createChatCompletion

Conversation

@jasonqu
Copy link
Contributor

@jasonqu jasonqu commented Mar 11, 2026

Add a typed createChatCompletion method for convenience, which could simplify the OpenAI Json Structured Outputs example to code like this:

// if MathReasoning derives zio.json
val response : MathReasoning = syncClient.createChatCompletion[MathReasoning](chatRequestBody) {
  _.fromJson[MathReasoning]
}

The parseFunction is defined as String => Either[String, T] in order to remind developer to treat the error as kind of "checked exception", and parsed error will be rethrow as DeserializationOpenAIException.

Other parseFunction example:

// uPickle
Try(upickle.read[T](rawJson).toEither.left.map(_.getMessage())
// or simply throw the original exception
Right(upickle.read[T](rawJson))

// circe
parse(rawJson).left.map(_.getMessage())

// zio.json
rawJson.fromJson[T]

So this method could be an idiomatic way to create a "typed chat", but might not be a urgent demand.

@adamw adamw merged commit 45ae00f into softwaremill:master Mar 12, 2026
5 checks passed
@adamw
Copy link
Member

adamw commented Mar 12, 2026

Thanks!

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