-
-
Notifications
You must be signed in to change notification settings - Fork 88
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
base: main
Are you sure you want to change the base?
Conversation
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 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: |
There was a problem hiding this comment.
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:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
wip for #11
Todo: