Skip to content

Add Schema builder for Structured Outputs #90

New issue

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

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

Already on GitHub? # to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

danielfriis
Copy link

@danielfriis danielfriis commented Apr 2, 2025

Suggesting an approach similar to how RubyLLM::Tool is implemented, allowing for this:

class MySchema < RubyLLM::StructuredOutput::Schema
  string :name
  number :age

  object :address do
    string :street
    string :city
  end

  array :tags, :string

  array :contacts do
    object do
      string :email
      string :phone
    end
  end

  any_of :status do
    string enum: ["active", "pending"]
    null
  end
end

And then allowing to call .json_schema on the class as input for using structured outputs.

@kieranklaassen
Copy link
Contributor

Nice! I started some implementation in #65 as well, but it is very overlapping. My approach is that people can either use a RubyLLM::StructuredOutput::Schema or bring their own.

Let me know if you want to join forces on this.

@danielfriis
Copy link
Author

Hey @kieranklaassen! I did see your PR, but noticed that it included the parser concept too, and in all honesty, it felt a bit overwhelming to dive into, so I wanted to suggest a simple approach for structured outputs only (initially). I'm game to join efforts — yours or mine — but let's maybe have @crmne's perspective on the direction before we spend too much time?

@kieranklaassen
Copy link
Contributor

Yes! For sure! I'm trying to build something I can actually use in production so kind of need that too but we can simplify. I love to keep things simple. Let's wait for @crmne to give his perspective and we can divide and conquer together

@danielfriis danielfriis changed the title Add structured output support Add Schema builder for Structured Outputs Apr 19, 2025
@danielfriis danielfriis marked this pull request as ready for review April 19, 2025 14:50
@danielfriis
Copy link
Author

@kieranklaassen feel free to take a look and see if I've missed anything and if it aligns with your expectations!

@jayelkaake
Copy link

jayelkaake commented Apr 22, 2025

@danielfriis I like your code, but on a high level I'm kind of against adding DSL to JSON Schema because IMO it's already it's own DSL. Then you're having to learn a DSL for a DSL.

This SDK core value is it abstracts the LLM-specific code with a simple interface. Since JSON schema is already abstracted, we don't need to add another abstraction layer on that, ya know?

That's just my opinion ... and that being said, your DSL here is quite simple and lovely - so good work!

@danielfriis
Copy link
Author

danielfriis commented Apr 22, 2025

@danielfriis I like your code, but on a high level I'm kind of against adding DSL to JSON Schema because IMO it's already it's own DSL. Then you're having to learn a DSL for a DSL.

This SDK core value is it abstracts the LLM-specific code with a simple interface. Since JSON schema is already abstracted, we don't need to add another abstraction layer on that, ya know?

That's just my opinion ... and that being said, your DSL here is quite simple and lovely - so good work!

I hear you! To me, the JSON structure is not intuitive though. Not unlike how tools are also covered by the gem.

But it's not a hill I will die on :) The gem works fine with just your new PR and I can run this next to the gem.

@jayelkaake
Copy link

To me, the JSON structure is not intuitive though. Not unlike how tools are also covered by the gem.

I agree that the schema language itself isn't super intuitive. I think that's the value a DSL like yours brings.

But it's not a hill I will die on :)

Same :).

I guess tools like Copilot now make me now lean towards not needing DSL since they autocomplete schema definitions anyways. (prob leaned the other way just last year!)

Copy link
Owner

@crmne crmne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your work @danielfriis!

Had a quick skim and I like the code.

However, isn't there any library that is already a great DSL for JSON Schemas? The core philosophy of RubyLLM is to not have to rely on any library for the core AI functionality because of how fast things change in that world, but for JSON Schemas I'm sure we can settle on a DSL made by someone else and actively maintained instead of maintaining our own

@danielfriis
Copy link
Author

Thanks for your work @danielfriis!

Had a quick skim and I like the code.

However, isn't there any library that is already a great DSL for JSON Schemas? The core philosophy of RubyLLM is to not have to rely on any library for the core AI functionality because of how fast things change in that world, but for JSON Schemas I'm sure we can settle on a DSL made by someone else and actively maintained instead of maintaining our own

Thanks a lot @crmne !

There are gems covering this (also specifically for LLMs).

Personally, I just don't like to rely on other libraries for core concepts — especially if the implementation is simple.

My initial thought was to make it part of a Structured Output / JSON mode PR ( #131 ).

To me, they go together as the current tool implementation has both a tool definition side, and a execution side.

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

Successfully merging this pull request may close these issues.

4 participants