Skip to content

API Reference parrot.bots.mixins.model_switching

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.bots.mixins.model_switching

View source

ModelSwitchingMixin — dual-LLM model switching for AbstractBot.

Adds the ability to configure a secondary LLM client next to the bot's primary one, and use it in one of two modes:

  • fallback: the primary client serves every call; when it raises, the same call is retried once on the secondary client (cross-provider failover — complementary to the client-level same-provider fallback_model retries).
  • contrastive: both clients answer the same prompt concurrently and the results are merged into a single :class:~parrot.models.responses.AIMessage whose metadata['model_switching'] attributes each answer to the model that produced it.

The mixin hooks the get_client() / execute_llm_call() extension points on :class:~parrot.bots.abstract.AbstractBot — the same cooperative pattern used by IntentRouterMixin (_resolve_output_mode) and SkillRegistryMixin (post_configure).

Usage::

from parrot.bots.agent import Agent
from parrot.bots.mixins import ModelSwitchingMixin, ModelSwitchMode

class ResearchAgent(ModelSwitchingMixin, Agent):
    pass

agent = ResearchAgent(
    llm="google:gemini-3.8-flash",
    secondary_llm="anthropic:claude-sonnet-5",
    model_switch_mode=ModelSwitchMode.CONTRASTIVE,
)

Limitations (v1): only non-streaming calls (ask) are switched; ask_stream always uses the primary client.

Alias AbstractClient

Re-exported from parrot.clients.base.AbstractClient.

Alias BudgetError

Re-exported from parrot.core.exceptions.BudgetError.

Alias CompletionUsage

Re-exported from parrot.models.basic.CompletionUsage.

Alias ConfigError

Re-exported from parrot.exceptions.ConfigError.

Class ModelSwitchMode

How the secondary LLM client participates in a call.

Class ModelSwitchingMixin

Mixin to add dual-LLM model switching to any bot or agent.

Mix in before the bot class so the MRO reaches this class first::

class MyAgent(ModelSwitchingMixin, Agent):
    ...

The merged response always carries a metadata['model_switching'] payload attributing which model produced what, e.g. for contrastive::

{
    "mode": "contrastive",
    "responses": [
        {"role": "primary", "provider": "google", "model": "...",
         "output": "...", "usage": {...}, "response_time": 1.2},
        {"role": "secondary", ...},
    ],
}
Parameter Type Default Description
*args Any ()
**kwargs Any {}

Attributes:

Attribute Type Description
enable_model_switching bool Master switch. When False the mixin is a pure passthrough. Default True.
model_switch_mode ModelSwitchMode :class:ModelSwitchMode (or its string value) selecting fallback or contrastive behavior. Default fallback.
secondary_llm Union[str, dict, AbstractClient, None] Specification of the secondary client, in any format :meth:~parrot.bots.abstract.AbstractBot._resolve_llm_config accepts — "provider:model" string, AbstractClient subclass or instance, or a model_config dict. Default None (switching disabled until provided).

Method ModelSwitchingMixin.post_configure

Build the secondary client after base configuration completes.

Chains super().post_configure() first (documented contract of :meth:AbstractBot.post_configure), then resolves :attr:secondary_llm through the existing :meth:~parrot.interfaces.tools.ToolInterface.configure_llm helper so the secondary client gets the same tool sync and conversation memory as the primary.

post_configure() -> None

Raises:

  • ConfigError — If switching is enabled with a secondary_llm spec that cannot be resolved into a client.

Method ModelSwitchingMixin.should_switch_on

Decide whether an error from the primary triggers the secondary.

Default: switch on any exception except asyncio.CancelledError. Override for finer control (e.g. only capacity errors).

should_switch_on(error: Exception) -> bool

Parameter Type Default Description
error Exception The exception raised by the primary client's call.

Returns: bool — True to retry the call on the secondary client.

Method ModelSwitchingMixin.execute_llm_call

Execute the LLM call with fallback or contrastive switching.

Passes straight through to the primary when switching is disabled, no secondary client is configured, or the call is not an ask.

execute_llm_call(client: AbstractClient, method: str = 'ask', **llm_kwargs: Any) -> Any

Parameter Type Default Description
client AbstractClient The already-entered primary client.
method str 'ask' Client coroutine name (only "ask" is switched).
**llm_kwargs Any {} Keyword arguments for the client call.

Returns: Any — The (possibly merged/annotated) AIMessage.

Method ModelSwitchingMixin.cleanup

Close the secondary client, then continue the cleanup chain.

cleanup() -> None

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally