Fix ActiveRecord with_tool/with_tools resetting messages mid-conversation#689
Open
ebeigarts wants to merge 2 commits intocrmne:mainfrom
Open
Fix ActiveRecord with_tool/with_tools resetting messages mid-conversation#689ebeigarts wants to merge 2 commits intocrmne:mainfrom
ebeigarts wants to merge 2 commits intocrmne:mainfrom
Conversation
Verifies that calling chat.with_tool from within a tool execute method (the ToolSearch pattern) does not corrupt the message sequence sent to the provider. Before the fix, to_llm reset messages from the database mid-conversation, inserting a stray empty assistant message between the tool_calls and tool result. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…tion When a tool dynamically registers new tools during execution (e.g., a ToolSearch tool that calls chat.with_tool to make discovered tools available), the ActiveRecord with_tool/with_tools methods were calling to_llm which reset all messages from the database. This inserted an extra empty assistant message between the tool_calls message and its tool result, violating the OpenAI API contract and causing a BadRequestError. Add a reset_messages parameter to to_llm so that with_tool/with_tools can initialize the chat object without disrupting the in-flight message sequence. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
ebeigarts
commented
Mar 19, 2026
| to_llm.with_tools(...) | ||
| to_llm(reset_messages: false).with_tools(...) | ||
| self | ||
| end |
Author
There was a problem hiding this comment.
Maybe it would be better to just use this instead?
def with_tool(...)
(@chat || to_llm).with_tool(...)
self
end
def with_tools(...)
(@chat || to_llm).with_tools(...)
self
end
Contributor
|
Ran into this issue as well. |
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.
When a tool dynamically registers new tools during execution (e.g., a ToolSearch tool that calls
chat.with_toolto make discovered tools available), the ActiveRecordwith_tool/with_toolsmethods were callingto_llmwhich reset all messages from the database. This inserted an extra empty assistant message between the tool_calls message and its tool result, violating the OpenAI API contract and causing aRubyLLM::BadRequestError.The solution is to add a
reset_messagesparameter toto_llmso thatwith_tool/with_toolscan initialize the chat object without disrupting the in-flight message sequence.To reproduce the issue: https://gist.github.com/ebeigarts/dae372091c42756c3bdf1fa9e2d5f2ef