Skip to content

[Feature]: Surface fallback model switching to application code — the switch and its cause currently reach only the log #3142

Closed

Description

@Buktal

Is your feature request related to a problem? Please describe.

When ReActAgent falls back from a failed primary model to fallbackModel, the switch is not surfaced on any framework contract. The original error is consumed inside the anonymous Model wrapper built by ReActAgent#modelForCall(): its switchOnFirst block logs a single WARN and discards the Throwable. No surface that downstream code can build on reports either the switch or its cause:

  • ModelCallEndEvent carries only replyId and usage — neither the model that served the call nor any error;
  • hooks receive the serving model's name per chunk (fireReasoningChunk) but never the error;
  • middlewares observe the AgentEvent stream, where the error never appears — it is swallowed inside mci.model().stream(...), below the event conversion.

What can be observed today are implicit side channels, each losing part of the fact:

  • wrapping the primary Model with doOnError sees the error, but cannot tell whether a fallback actually took over or the error propagated to the caller;
  • the wrapper's getModelName() flips to the fallback after the switch — attribution-grade, but it loses the Throwable, so a 429 cannot be told from a 500 or a quota error.

Consumers that need the switch itself — degraded-mode alerting, cooldown bookkeeping, per-provider accounting of which model served which call — are left with the log line: rate limiting, quota exhaustion, and provider outages are masked as silent success.

Describe the solution you'd like

A callback invoked at the switch site, with the original error — e.g.:

public interface FailoverPolicy {

    /** Called when the primary model fails and the fallback takes over. */
    default void onFailover(Model primary, Throwable error) {}
}

Required semantics, whatever the exact shape:

  • invoked synchronously at the switch site with the original Throwable;
  • implementations must be non-blocking (reactor thread) and thread-safe (one policy shared across the agent's concurrent runs);
  • an exception thrown by the implementation must not affect the switch (log-and-continue);
  • no policy configured (the default) behaves exactly as today.

Mounting naturally sits alongside fallbackModel (ModelConfig + builder setters); whether this lands as a new interface or on an existing observer surface (the hook dispatcher) is open for discussion.

Describe alternatives you've considered

  • Decorating the primary Model (delegate.stream(...).doOnError(...)): zero framework changes, and sufficient for cooldown-style bookkeeping on the primary's failures. Not a contract for the switch itself: it cannot distinguish "running degraded on the fallback" from "no fallback configured, error propagated", and every downstream reimplements it by hand.
  • A new AgentEvent type: the switch happens in the Model layer (Flux<ChatResponse>), below the conversion into AgentEvents; surfacing it as an event crosses that layer boundary and forces every event consumer — and the serialization side — to handle a new type. A callback keeps the signal at the mechanism layer without touching the event model.

Additional context

Verified on current main (commit 7d321e2b): ReActAgent#modelForCall() (switchOnFirst block), ModelCallEndEvent fields, per-chunk model-name attribution in fireReasoningChunk.

Activity

  1. self-assigned this
    on Sep 14, 2026
  2. changed the title [-][Feature]: No way to observe fallback model switching — the primary model's failure is swallowed inside ReActAgent[/-] [+][Feature]: Surface fallback model switching to application code — the switch and its cause currently reach only the log[/+] on Sep 14, 2026
  3. oss-maintainer commented on Sep 14, 2026

    @oss-maintainer
    Collaborator

    Issue Evaluation

    Category: enhancement | Area: area/core/agent | Status: Evaluated — feasible, needs a design decision

    The problem statement checks out. Re-verified against main @ 7d321e2b, every claim in the report is accurate:

    1. ReActAgent#modelForCall() (ReActAgent.java:4065-4090) wraps the primary in switchOnFirst; on signal.isOnError() it captures the Throwable, flips activeModel, emits a single log.warn(...) and returns fallbackModel.stream(...). The error never leaves that lambda.
    2. ModelCallEndEvent (ModelCallEndEvent.java:27-28) carries only replyId and usage — no serving-model identity and no error.
    3. Hooks see the serving model name per chunk through fireReasoningChunk(...) against the wrapper's getModelName(), but never the cause, so a 429 is indistinguishable from a 500 or a quota error.
    4. Middlewares observe the AgentEvent stream, which is downstream of the recovery point — the switch is invisible there too.

    Feasibility: moderate, and additive. fallbackModel already lives on the ModelConfig record plus ReActAgent.Builder#fallbackModel(...) (ReActAgent.java:4989, :5001), so a policy mounted alongside it follows an existing shape. Default (no policy configured) is byte-for-byte today's behaviour, and there is no change to the event model or its serialization — which is the right call, and rules out the new-AgentEvent-type alternative for the reasons given in the report.

    One compatibility constraint to respect: ModelConfig is a record (int maxRetries, Model fallbackModel). Adding a third component breaks binary compatibility for any external new ModelConfig(...) caller. Keep the existing two-arg constructor delegating with a null/no-op policy.

    Design decision needed before implementation — the notification surface:

    1. Dedicated FailoverPolicy callback invoked at the switch site: the contract is explicit and non-blocking-thread-friendly, but adds a new public core type plus plumbing through ModelConfig, the builder, and fromAgent(...) copy paths.
    2. Existing observer surface (the hook dispatcher): no new public type, and hooks already receive per-chunk model attribution — but the dispatcher signature would need to widen to carry a Throwable, which touches every hook implementation.

    Option 1 matches the stated required semantics most directly (synchronous at the switch site, original Throwable, log-and-continue on implementation error). Could a maintainer confirm the shape and naming (FailoverPolicy.onFailover(Model primary, Throwable error) vs. a method on the hook/Middleware surface) before this is implemented?

    Two semantics worth pinning down while deciding:

    • Retry interaction: ModelConfig javadoc says the fallback shares the primary's maxRetries budget. Should the callback fire once per switch (recommended), or per primary attempt that failed?
    • Copy path: fromAgent(...) copies model-resilience config today, so the policy must be carried there as well or toBuilder round-trips will silently drop it.

    Test coverage today: ReActAgentTest#testFallbackModel, ReActAgentSummarizingTest#testSummaryHooksUseFallbackModelName, ReActAgentNewLoopBuilderTest#fromAgentCopiesModelResilienceConfig — these pin that the switch happens; none asserts that the switch is observable. An implementation would add: callback fires once with the original cause, a throwing implementation does not break the switch, and the no-policy default is unchanged.

    Not generating an automated fix proposal for this one — a new public core interface in agentscope-core cascades to harness/distribution/extensions and needs maintainer sign-off on the API shape first.


    Automated evaluation by github-manager-bot

  4. added
    area/core/agent
    enhancement
    on Sep 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area/core/agentenhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions