Skip to content

generate_with inheritance: parent provider options leak into child when switching providers #304

@TheRealNeil

Description

@TheRealNeil

Summary

When a child agent overrides generate_with with a different provider, options from the parent provider (e.g., host, api_key, model) leak through and override the child's provider config from YAML.

Steps to Reproduce

# Parent agent
class ApplicationAgent < ActiveAgent::Base
  generate_with :ollama, model: 'gpt-oss:20b'
end

# Child agent — switches to Azure
class BusinessProcedureAgent < ApplicationAgent
  generate_with :azure, model: 'gpt-4.1', stream: true
end
# config/active_agent.yml
development:
  ollama:
    service: "Ollama"
    host: "http://localhost:11434/v1"
  azure:
    service: "AzureOpenAI"
    host: "https://mycompany.cognitiveservices.azure.com/openai/deployments/gpt-4.1"
    api_key: "secret"
    api_version: "2025-01-01-preview"

Expected: BusinessProcedureAgent uses host: "https://mycompany.cognitiveservices.azure.com/..." from the Azure YAML config.

Actual: The Ollama host: "http://localhost:11434/v1" from the parent leaks through and overrides the Azure host, causing requests to go to the wrong endpoint.

Root Cause

In ActiveAgent::Base.generate_with (base.rb, lines 103-115):

def self.generate_with(provider_reference, **agent_options)
  self.prompt_provider = provider_reference

  global_options    = provider_config_load(provider_reference)
  inherited_options = (self.prompt_options || {}).except(:instructions)

  if global_options[:service] != inherited_options[:service]
    inherited_options.extract!(:service, :api_version)
  end

  self.prompt_options = global_options.merge(inherited_options).merge(agent_options)
end

When the service changes, only :service and :api_version are removed from inherited_options. All other parent options (:host, :api_key, :model, :base_url, etc.) remain. Since the merge order is global_options.merge(inherited_options), the inherited (parent) values override the global (YAML) values.

Suggested Fix

When switching to a different service/provider, inherited options from the parent are meaningless and should be discarded entirely:

if global_options[:service] != inherited_options[:service]
  inherited_options = {}
end

This ensures a clean slate when changing providers, while still allowing same-provider inheritance (e.g., a child that tweaks temperature on the same provider).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions