Answers your program can read
Free text is good for people. A program, though, wants data: which category does this email belong to, how urgent is it, which name and which age are in this sentence? Ask a model to “answer in JSON” and it mostly does – but sometimes with a comment before it, a missing field or a word of its own. Structured output solves this: you describe the shape of the answer as a schema, and the model must answer in exactly that shape.
RubyLLM uses the gem Schematist for schemas, which comes with it. with_schema sets the schema, parsed reads the answer as a Hash:
install_gem "ruby_llm"
require "ruby_llm"
require "chunky_llm" # Chunky Mini, the little model in this page
class PersonSchema < Schematist::Schema
string :name, description: "The person's first name"
integer :age
string :city, required: false
end
response = RubyLLM.chat.with_schema(PersonSchema).ask("Alice is 30 and lives in Rome.")
puts response.content
response.parsedcontent is the JSON text the model wrote, parsed the Hash made from it. city is optional (required: false): a sentence without a city gets no "city". What the model receives is a JSON Schema, the standard all providers understand:
PersonSchema.new.to_json_schemaChoices: enum
Where only a few values make sense, enum: lists them – the model has to pick one. This is how a help desk sorts incoming emails before anyone reads them:
class TicketTriage < Schematist::Schema
string :category, enum: %w[billing shipping returns other]
string :priority, enum: %w[low normal urgent]
string :summary, description: "One sentence for the support queue"
end
emails = [
"My parcel still hasn't arrived and I need it for a wedding on Saturday!",
"Could you send me the invoice for my last order?",
"The jacket is too small, I'd like to return it."
]
emails.map do |email|
RubyLLM.chat.with_schema(TicketTriage).ask(email).parsed.values_at("category", "priority")
endEach email gets a chat of its own: in one shared chat the second email would carry the first along as history. An agent can hold the schema, so every chat it makes answers in that shape:
class TriageAgent < RubyLLM::Agent
instructions "Triage emails sent to the help desk of Chunky's shop."
schema TicketTriage
end
TriageAgent.new.ask("Where is my bacon? I ordered it two weeks ago!").parsedTesting without a model
How do you test code that uses a model? The real answers change from run to run, take seconds and cost money. RubyLLM brings a test helper: stub_replies decides what the model will answer – the chat, the agent and your code around them run for real, only the request never goes out. That is how you test your code: what it does with a reply.
require "ruby_llm/test_helper"
class TriageTest < Minitest::Test
include RubyLLM::TestHelper
def test_reads_the_category
stub_replies '{"category":"billing","priority":"low","summary":"Wants an invoice."}'
triage = TriageAgent.new.ask("Can I get an invoice?").parsed
assert_equal "billing", triage["category"]
end
def test_tool_call
stub_replies tool_call(:tiny_weather, city: "Bern"), "It is sunny in Bern."
chat = RubyLLM.chat.with_tools(TinyWeather)
assert_equal "It is sunny in Bern.", chat.ask("Weather in Bern?").content
assert_equal "sunny", chat.messages[2].content
end
end
class TinyWeather < RubyLLM::Tool
description "Gets the weather for a city"
parameter :city
def execute(city:) = "sunny"
end
run_testsThe second test scripts a tool call: the “model” asks for the tool, RubyLLM really runs TinyWeather#execute, and the test checks what the tool put into the chat. stub_replies holds until the end of the test (after_teardown clears it), so a test that follows talks to the real model again.
Put require "ruby_llm/test_helper" into test/test_helper.rb and include RubyLLM::TestHelper into the tests (RSpec: config.include RubyLLM::TestHelper). Whether a real model sorts your emails well is a different question: RubyLLM's evaluations (bin/rails generate ruby_llm:evaluation) run a set of examples against a model and count how often it is right – they cost money on every run, so they stay out of the normal test run.
SnackOrder with a field snack that can only be "bacon", "berries" or "eggs", and an integer count. Ask a chat with this schema “Chunky would like 3 eggs, please.” – the cell yields the parsed answer.install_gem "ruby_llm"
require "ruby_llm"
require "chunky_llm" # Chunky Mini, the little model in this page
# class SnackOrder < Schematist::Schema
# end