From f4900f45d591703ac15f9dcfbb5fd1d496793807 Mon Sep 17 00:00:00 2001 From: aakashb95 Date: Tue, 17 Feb 2026 21:32:49 +0530 Subject: [PATCH] fix: refresh and persist RubyLLM model registry on initialization RubyLLM uses a static bundled models.json file as its model registry. New models released by providers (e.g., gpt-5.1, gpt-5.2) are not available until the gem is updated. This causes ModelNotFoundError for self-hosted users who configure newer models. Call RubyLLM.models.refresh! during Llm::Config.initialize! to fetch the latest model list from configured providers and models.dev. Persist the result with save_to_json to tmp/ruby_llm_models.json so subsequent processes load from disk instead of making HTTP calls. The file has a 1-hour TTL to balance freshness with performance. Co-Authored-By: Claude Opus 4.6 --- lib/llm/config.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/llm/config.rb b/lib/llm/config.rb index 48de51022..7e144e482 100644 --- a/lib/llm/config.rb +++ b/lib/llm/config.rb @@ -2,6 +2,8 @@ require 'ruby_llm' module Llm::Config DEFAULT_MODEL = 'gpt-4.1-mini'.freeze + REGISTRY_TTL = 1.hour + REGISTRY_PATH = Rails.root.join('tmp/ruby_llm_models.json').to_s class << self def initialized? @@ -12,6 +14,7 @@ module Llm::Config return if @initialized configure_ruby_llm + refresh_model_registry @initialized = true end @@ -30,6 +33,21 @@ module Llm::Config private + def refresh_model_registry + if fresh_registry_file? + RubyLLM.models.load_from_json!(REGISTRY_PATH) + else + RubyLLM.models.refresh! + RubyLLM.models.save_to_json(REGISTRY_PATH) + end + rescue StandardError => e + Rails.logger.warn "Failed to refresh RubyLLM model registry: #{e.message}" + end + + def fresh_registry_file? + File.exist?(REGISTRY_PATH) && File.mtime(REGISTRY_PATH) > REGISTRY_TTL.ago + end + def configure_ruby_llm RubyLLM.configure do |config| config.openai_api_key = system_api_key if system_api_key.present?