Repository navigation
Load the gem outside Rails with Zeitwerk - #1080
Open
pcbeingused333 wants to merge 4 commits into
Open
pcbeingused333 wants to merge 4 commits into
pcbeingused333 wants to merge 4 commits into
Conversation
Since the move to Rails::Engine (patterns-ai-core#1003), require "langchain" raised NameError (uninitialized constant Rails) in a plain Ruby script, and with Rails loaded but no app booted nothing under Langchain:: was autoloaded. When Rails::Engine isn't defined, set up a Zeitwerk loader with the acronyms from config/initializers/inflections.rb instead. Rails apps still get the engine. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts: # CHANGELOG.md
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.
Since the move to
Rails::Engine(#1003, not released yet: 0.19.5 predates it),mainonly loads inside a booted Rails app:require "langchain"raisesNameError: uninitialized constant Railsfromlib/langchain/engine.rb.require "rails"first but no app booted,require "langchain"succeeds, butLangchain::LLMis undefined, because only the engine sets up autoloading.So the next release would break scripts, Sinatra/Hanami apps, background workers outside Rails, and the gems that depend on langchainrb (#880's case).
Change
lib/langchain.rbrequires the engine whenRails::Engineis defined, as now. Otherwise it sets upZeitwerk::Loader.for_gem, the loader the gem used before #1003, with the acronyms fromconfig/initializers/inflections.rb. It ignoresengine.rbandapplication_record.rb, which need Rails. Nothing changes for Rails apps.railsstays a runtime dependency in the gemspec. It is installed but not loaded outside Rails. Making it optional would be a separate decision.Tests
spec/langchain_spec.rb: a subprocess runsrequire "langchain"without Rails, checks that Rails wasn't loaded, eager-loads every class (Zeitwerk::Loader.eager_load_all) and resolvesLangchain::LLM::OpenAIandLangchain::LLM::Response::OpenAIResponse. It fails onmainwith theNameErrorabove.Langchain::classes without Rails and instantiatedLangchain::LLM::Ollama.bundle exec rspec: 770 examples, 0 failures (this exercises the engine path throughtest/dummy).standardrbis clean.🤖 Generated with Claude Code