Repository navigation
RubyLLM 2 follow-ups: generation, sampling, and prompt argument fixes - #1
Open
jordan-brough wants to merge 5 commits into
Open
jordan-brough wants to merge 5 commits into
jordan-brough wants to merge 5 commits into
Conversation
RubyLLM 2's Chat#complete returns the existing assistant message when the conversation already ends with one, so an MCP prompt or sampling request that ends with assistant text echoed that text back without calling the provider. Add RubyLLM::MCP.generate_response, which calls Chat#generate when the installed RubyLLM defines it and then Chat#complete, and use it from Prompt#ask, Sample#chat, and SamplingActions#default_chat_completion. RubyLLM 1 has no Chat#generate and its Chat#complete always generates, so only Chat#complete is called there.
RubyLLM 2 messages carry a finish_reason (:stop, :max_tokens, :tool_calls, :content_filter) instead of a stop_reason, so sampling responses always reported endTurn even when the model was cut off by its token limit. Map finish_reason to the MCP stop reason names when the message provides one, camelizing unknown values as before, and keep the existing stop_reason handling and the endTurn default for RubyLLM 1 messages.
Prompt#include raised when a required argument was supplied and let a missing one through, and it built the error with a positional string that BaseError#initialize rejects, so any required argument produced an ArgumentError. Raise only when a required argument is absent, accepting String or Symbol keys, and construct the error with its message keyword.
…on RubyLLM 2 Sample#create_message and the SamplingActions message builders passed an MCP::Content object as the content of a RubyLLM::Message. RubyLLM 2 only accepts a String there, so every sampling request raised an ArgumentError before the model was called. Build these messages from Content#message_options, as Prompt and Resource already do, which yields the Content itself on RubyLLM 1 and separate text and attachments on RubyLLM 2.
…nstalled RubyLLM 1 declares tool parameters with Tool.param(desc:) and RubyLLM 2 with Tool.parameter(description:). spec_helper loads SimpleMultiplyTool, so the regular spec suite could not start under RubyLLM 2. Pick the method the installed RubyLLM provides so the suite loads on both.
This was referenced Sep 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Five small follow-ups to patvice#161, one commit each. All of them work on RubyLLM 1.x and 2.x.
1. Prompts and sampling that end with assistant text now call the model
On RubyLLM 2,
Chat#completetreats a conversation that already ends with an assistant message as finished and returns that message. An MCP prompt or sampling request that ends with a partial assistant answer therefore came back unchanged, and the provider was never called.The fix is a small
RubyLLM::MCP.generate_response(chat)helper. On RubyLLM 2 it callsChat#generatefor one new response, thenChat#completeto finish any tool calls. RubyLLM 1 has noChat#generate, so there it just callsChat#completeas before.Prompt#ask,Sample#chat, and the default sampling handler use it.2. Sampling responses report the real stop reason
On RubyLLM 2, every sampling response said
stopReason: "endTurn", even when the model hit its token limit. The code looked forstop_reason, but RubyLLM 2 messages carryfinish_reason.Now a
finish_reasonis mapped to the MCP names (endTurn,maxTokens,toolUse,contentFilter; anything else is camelized, which MCP allows). Messages without one still go through the existingstop_reasonhandling and default.3. Required prompt arguments are validated correctly
The check was inverted: supplying a required argument raised, and omitting it passed. The error was also built with a positional string, but
BaseErrortakesmessage:, so the failure surfaced as an unrelatedArgumentError. It now raisesPromptArgumentErroronly when a required argument is missing, with String or Symbol keys accepted.4. Sampling requests build messages RubyLLM 2 accepts
Sample#create_messageand the sampling handler passed anMCP::Contentas message content. RubyLLM 2 rejects that withArgumentError, so every sampling request failed before reaching the model. They now build messages throughContent#message_options, the same wayPromptalready does.5. The regular spec suite loads under RubyLLM 2
spec_helper.rbloadsspec/support/simple_multiply_tool.rb, which used the RubyLLM 1paramsyntax that RubyLLM 2 removed. Sobundle exec rspeccould not start on RubyLLM 2, which is why CI only runs the standalone compatibility spec there. The tool now declares its parameters with whichever method the installed RubyLLM provides.Loading is not the same as passing.
native/messages_spec.rbnow passes in full on RubyLLM 2 (94 examples). Other files still fail there for reasons outside this PR: VCR cassettes recorded against RubyLLM 1 request shapes, model ids the RubyLLM 2 registry no longer resolves, and assertions that expectMCP::Contentwhere RubyLLM 2 tools return a String.Verification
Bundled with
RUBY_LLM_VERSION="~> 1.0"and"~> 2.0"in turn. The new specs (generate_response_spec.rb,prompt_arguments_spec.rb) and the compatibility spec pass on both. The compatibility spec gains examples for changes 1, 2, 4, and 5 that assert the behavior of whichever RubyLLM is installed. On RubyLLM 1 the full suite was also run against the fixture servers with no version-related failures.