Skip to content

Add structured output support for extracting JSON data #65

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

kieranklaassen
Copy link
Contributor

@kieranklaassen kieranklaassen commented Mar 24, 2025

wip for #11

Todo:

  • Implement with_response_format
  • Implement response_format in chat and providers
  • Implement a response parser to return he response_format model or open struct
  • add tests

@kieranklaassen
Copy link
Contributor Author

I'm thinking to simplify the PR and reduce scope, maybe I can just use the tool API to implement it. keep ya'll posted

@crmne crmne added the enhancement New feature or request label Apr 2, 2025
@kieranklaassen
Copy link
Contributor Author

@crmne Would you mind looking at the direction I'm taking here since it's a bigger feature, and I'd love to have your opinion on this. Let me know what you like and don't like. I can either continue, adjust, or abort. TY

schema_json = JSON.pretty_generate(schema)

<<~INSTRUCTIONS
You must respond with a valid JSON object that strictly adheres to the following JSON schema:

Choose a reason for hiding this comment

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

I was looking into replacing langchain in my app to use ruby_llm because it's api is much simpler. One of the missing things here is structured output support and when I searched the PR list and found this PR I was happy that somebody is working on adding this already 😄
I noticed that langchain takes a much stricter approach telling the ai model how to structure it's output, maybe this is a good idea.

Here's the prompt it uses:

You must format your output as a JSON value that adheres to a given "JSON Schema" instance.

"JSON Schema" is a declarative language that allows you to annotate and validate JSON documents.

For example, the example "JSON Schema" instance {"properties": {"foo": {"description": "a list of test words", "type": "array", "items": {"type": "string"}}}, "required": ["foo"]}}
would match an object with one required property, "foo". The "type" property specifies "foo" must be an "array", and the "description" property semantically describes it as "a list of test words". The items within "foo" must be strings.
Thus, the object {"foo": ["bar", "baz"]} is a well-formatted instance of this example "JSON Schema". The object {"properties": {"foo": ["bar", "baz"]}}} is not well-formatted.

Your output will be parsed and type-checked according to the provided schema instance, so make sure all fields in your output match the schema exactly and there are no trailing commas!

Here is the JSON Schema instance your output must adhere to. Include the enclosing markdown codeblock:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks!

Copy link

Choose a reason for hiding this comment

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

Just came across this while running into issues with gemini-2.5-exp. No matter what instructions i give it, it just will not return plain JSON but instead will always wrap it in markdown.

I get:

```json
  [myjson]
 ``

Instead of just plain JSON which i can then call JSON.parse on. I also tried the instructions from the langchain example, but removed the last sentence: Include the enclosing markdown codeblock:

I don't think prompting it to return plain JSON will work

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is json mode for gemini and ope ai

Copy link

@drale2k drale2k Apr 11, 2025

Choose a reason for hiding this comment

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

Is that supported by RubyLLM? I tried to find an option but cannot. Could you point me to the docs?

edit: Nevermind, you are talking about what the providers support but obviously RubyLLM does not yet - hence this PR

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

Successfully merging this pull request may close these issues.

4 participants