[BUG] Agent.create resolves the model before applying the agent context
I maintainer di solito rispondono entro 1 giorno
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 25/100
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
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:
mainat575e2c085 - 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
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di crmne/ruby_llm
-
[BUG] MCP over HTTP can't read a JSON reply that starts with a byte order markForse già presa @yorzi l’ha presa oggi. Apertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
crmne/ruby_llm#1879 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
[FEATURE] config.price cannot set image pricesForse già presa @alfredszhao-art l’ha presa 2 giorni fa. Apertaenhancement
Difficoltà 2/5 1-3 ore Idoneità per principianti 69/100
I maintainer di solito rispondono entro 1 giorno
-
[BUG] XLSX date-times on January 1, 1904 lose the dateForse già presa @yorzi l’ha presa oggi. Apertabug
Difficoltà 3/5 Mezza giornata Idoneità per principianti 35/100
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 3/5 Mezza giornata Idoneità per principianti 48/100
crmne/ruby_llm#1876 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
[BUG] after_fallback reports a fallback as failed when after_message raises on its replyForse già presa @yorzi l’ha presa 2 giorni fa. Apertabug
Difficoltà 3/5 1-2 giorni Idoneità per principianti 35/100
crmne/ruby_llm#1861 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di crmne/ruby_llm
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
swombat/souls-house#1203 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
we-promise/sure#5924 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
CircuitVerse/CircuitVerse#8342 · 1 commento · 1 reazione ·
I maintainer di solito rispondono entro 2 giorni
-
free_port can hand one port to two callers, so the fixed-port http tests fail in parallel runsForse già presa Una pull request collegata a questa issue è aperta o già unita. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 92/100
erkamyaman/rust-anything#963 ·
I maintainer di solito rispondono entro 1 giorno