[BUG] Agent.create resolves the model before applying the agent context

Aperta
#1,860 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@yorzi ci sta già lavorando.

Dal 27/11/2026.

  • #1864 di @yorzi — aperta

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
25/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
rails, ruby
Ambito
backend, databases

Direzione di ricerca

The issue identifies Agent.create/create!, Agent.chat, Agent.find, and the before_save :resolve_model callback in ActiveRecord::ChatMethods; it also links the proposed fix and specs in PR #1864. Read the linked pull request and its specs to understand the current implementation. Done means new records resolve the model and apply configuration from the agent context consistently, including when the caller supplies context:.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug
Basic checks
  • I searched existing issues - this hasn't been reported
  • I can reproduce this consistently
  • This is a RubyLLM bug, not my application code
What's broken?

Agent.create and Agent.create! call the chat class's create/create! first and apply the agent's configuration, including its context, only after the record is saved. The before_save :resolve_model callback in ActiveRecord::ChatMethods therefore runs with no context and falls back to the global RubyLLM.config. An agent whose context sets default_model, or the azure_deployments a declared model needs, gets a chat record with the global default model or fails to resolve its deployment.

Agent.chat resolves the context first and builds the chat inside it, so the same agent gets different models from chat and create!. AGENTS.md says a persisted chat behaves like a plain chat, and Agent.find already applies the agent context to the found record.

When the caller passes context: to create!, the record resolves its model with the caller's context and is then switched to the agent's context, so the model and the configuration that runs it come from different contexts. Agent.chat(context:) and Agent.find use the agent's context in that case.

How to reproduce

Save as spec/repro_spec.rb in the repo and run bundle exec rspec spec/repro_spec.rb:

require "rails_helper"

RSpec.describe RubyLLM::Agent do
  it "resolves the model from the agent context on create!" do
    RubyLLM.config.default_model = "gpt-4.1-mini"
    RubyLLM.config.openai_api_key = "global-key"

    agent = Class.new(RubyLLM::Agent) { chat_class Chat }
    agent.context do |config|
      config.default_model = "claude-haiku-4-5"
      config.anthropic_api_key = "agent-key"
    end

    puts "Agent.chat:    #{agent.chat.model.id}"
    puts "Agent.create!: #{agent.create!.model_id}"
  end
end
Expected behavior
Agent.chat:    claude-haiku-4-5
Agent.create!: claude-haiku-4-5
What actually happened
Agent.chat:    claude-haiku-4-5
Agent.create!: gpt-4.1-mini

I have a small fix with specs in #1864: the new record evaluates the agent context once, in a before_save callback ahead of model resolution, so it runs after after_initialize and validation, and the same value goes to the rest of the configuration.

@crmne, one question: when the caller also passes context: to create!, I made the agent's context win, as it does for Agent.chat(context:) and Agent.find today. Making the caller's context win would change all three entry points, so I left that out. Does that match what you want?

Environment
  • Ruby version: 4.0.9
  • RubyLLM version: main at 575e2c085
  • Provider: any (reproduced with OpenAI and Anthropic configuration, no requests sent)
  • OS: macOS
Lingua principale
Ruby
Stelle
4.5k
Fork
543
Merge medio
11h 38m
PR unite (30g)
410

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di crmne/ruby_llm

Tutte le issue di crmne/ruby_llm

Issue simili

Altre issue su Ruby

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.