Skip to content

DummyLM should share a trait with LM #77

Open

Description

@darinkishore

Problem

LM and DummyLM are structurally separate types with duplicated APIs:

  • Both have call(), inspect_history(), cache_handler, temperature, max_tokens
  • But they don't share a trait — code that wants to be generic over "any LM" can't be

DSPy Python has BaseLM as the abstract base class that both real and mock implementations extend.

Proposal

Extract a shared trait:

#[async_trait]
pub trait LanguageModel: Send + Sync {
    async fn call(&self, messages: Chat, tools: Vec<Arc<dyn ToolDyn>>) -> Result<LMResponse>;
    async fn inspect_history(&self, n: usize) -> Vec<HistoryEntry>;
    fn model(&self) -> &str;
}

LM and DummyLM both impl this. GLOBAL_SETTINGS stores Arc<dyn LanguageModel>. Modules reference the trait, not the concrete type.

Related

Activity

  1. darinkishore commented on Feb 12, 2026

    @darinkishore
    CollaboratorAuthor

    Codebase audit — LM vs DummyLM duplication

    LM (core/lm/mod.rs):

    • call(&self, messages, tools) → Result<LMResponse>
    • execute_tool_loop(...) → tool iteration
    • inspect_history(n) → reads from cache
    • Fields: model, temperature, max_tokens, cache, cache_handler, client, api_key, base_url, max_tool_iterations

    DummyLM (core/lm/mod.rs:554+):

    • call(&self, prompt) → returns canned responses
    • inspect_history(n) → reads from its own cache
    • Fields: api_key, base_url, temperature, max_tokens, cache, responses

    Settings (core/settings.rs):

    pub struct Settings {
        pub lm: Arc<LM>,  // concrete type, not trait
        pub adapter: Arc<dyn Adapter>,
    }

    Note adapter is already dyn Adapter (trait object) but lm is Arc<LM> (concrete). Inconsistent.

    Proposed trait:

    #[async_trait]
    pub trait LanguageModel: Send + Sync {
        async fn call(&self, messages: Chat, tools: Vec<Arc<dyn ToolDyn>>) -> Result<LMResponse>;
        async fn inspect_history(&self, n: usize) -> Vec<HistoryEntry>;
        fn model(&self) -> &str;
        fn temperature(&self) -> f32;
        fn max_tokens(&self) -> u32;
    }

    Migration:

    1. Define LanguageModel trait in core/lm/mod.rs
    2. Impl for both LM and DummyLM
    3. Change Settings.lm to Arc<dyn LanguageModel>
    4. Change get_lm() to return Arc<dyn LanguageModel>
    5. Update Predict and anywhere else that uses Arc<LM> directly

    This unblocks: #69 (per-module LM — modules store Option<Arc<dyn LanguageModel>>), better testing (inject DummyLM anywhere LM is expected), and third-party LM implementations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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