class RubyLLM::Question
A question a tool call waits on until a person answers it: the approval a tool declared with Tool.requires_approval asks before it runs, a question the tool asks itself with Tool#ask, or one an MCP server asks while it works on the call. Chat#pending_questions returns the questions a chat waits on.
question = chat.pending_questions.first question.message # => "Window or aisle?" question.fields.first.choices # => ["window", "aisle"] question.answer "window" chat.complete
An approval asks whether its call may run, so it is answered yes or no:
question.approval? # => true question.tool_call.name # => "issue_refund" question.approve # or question.decline("Not on this account")
A server can also ask the person to visit a page, such as to connect an account: url? is true, and answering says they agreed to go. Before a server’s question reaches the chat, MCP.before_input_request callbacks can answer it.
Constants
- Field
-
A value a question asks for.
choiceslists the values a field offers, andchoice_titlesmaps each one to the title to show for it, which is the value itself when the field gives it none:field.choices # => ["s", "l"] field.choice_titles # => {"s" => "Small", "l" => "Large"}
Both are
nilfor a field that takes any value.
Attributes
The question, or nil for an approval.
The ToolCall that waits on the question, when it came from a chat.
The page the question asks the person to visit, or nil. When an MCP server asks, show the whole address before the person goes, since the server chose it. RubyLLM declines a server’s request to visit anything but an http or https address (see MCP.input_requests).
Public Instance Methods
# File lib/ruby_llm/question.rb, line 211 def answer(value = nil, **values) if @chat @chat.answer(self, value, **values) else respond(response_to(value, values)) end self end
Answers the question and returns self. A question with one field, as Tool#ask asks without a block, takes its value. One with several takes them by name, as keywords or a Hash, and a field left out takes the default the question gives it. An approval or a page to visit takes no value: answering an approval approves its call, and answering a page says the person agreed to visit it.
question.answer "window" question.answer environment: "staging", reason: "Release 2.2"
A question from a chat records the answer there, as Chat#answer does, and the next Chat#complete carries the call on.
Strings become the integers, numbers, and booleans the fields ask for, the way a form sends them, a Symbol becomes text, text and the items of a list lose the spaces around them, and a blank String counts as left out. The ActionController::Parameters a form sends for several fields work as a Hash, keeping only the fields the question asks for. Raises ArgumentError, with a sentence to show the person, for a value the question does not ask for, a choice it does not offer, a value of the wrong type, or a required value left out.
Source
# File lib/ruby_llm/question.rb, line 186 def answered? !response.nil? end
Returns whether the question has been answered or declined.
Source
# File lib/ruby_llm/question.rb, line 171 def approval? @approval end
Returns whether the question asks whether its call may run: the approval of a tool declared with Tool.requires_approval. Answer it with approve or decline.
Source
# File lib/ruby_llm/question.rb, line 246 def approve(remember: false) self.class.decided_call(self, 'answer') @chat ? @chat.approve(self, remember:) : respond(ACCEPTED) self end
Approves the call an approval asks about, as Chat#approve does, and returns self. With remember: true, later calls to the same tool run without asking for the rest of the chat.
Raises ArgumentError for a question that is not an approval.
Source
# File lib/ruby_llm/question.rb, line 232 def decline(note = nil) if @chat @chat.decline(self, note) else respond(self.class.declined(note)) end self end
Declines the question and returns self. Declining an approval keeps its call from running. A note tells the model why: an approval’s call gets it as its result, in place of a plain refusal, and a tool that asked gets nil from Tool#ask, and the model gets the note after the tool’s result. A Hash note is an approval’s whole result, as given, and follows a tool’s result as JSON. A question from a chat records it there, as Chat#decline does.
question.decline question.decline("The order shipped already") question.decline(error: "Denied", reason: "Wrong account")
Source
# File lib/ruby_llm/question.rb, line 181 def form? !url? && !approval? end
Returns whether the question asks for fields.
Source
# File lib/ruby_llm/question.rb, line 162 def id return @nonce if tool_call.nil? || approval? "#{tool_call.id}/#{@nonce}" end
Returns a String that identifies the question among those of its chat. Chat#answer and the other methods that take a question take its id too, so a form can send it. An approval has its tool call’s id.
Protected Instance Methods
Source
# File lib/ruby_llm/question.rb, line 308 def comparable params = JSON.parse(JSON.generate(@params.to_h)) params.delete('url') if params['mode'] == 'url' params end
The question’s params as JSON stores them, without a page’s address.