feat: retry with backoff and opt-in model fallback

未关闭
#353 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
35/100
Issue 类型
功能
描述清晰度
需要澄清
活跃度
冷清
技术栈
python

调研方向

先阅读 chatlas/_parallel.py,尤其是 _parallel_chat_impl 和 ErrorController,然后查看 chatlas/_chat.py 中的 Chat.init 以及 chatlas/_provider.py 中的 Provider ABC。确定第一阶段的范围是 parallel_chat、所有 chat 调用,还是两者,并在实现前定义临时错误和 fallback 行为。完成标准应包括一个达成共识的 API,以及可观察到的重试和 fallback 结果,同时不重试通用的 4xx 错误。

由索引模型根据 Issue 内容生成。

描述

ai-triage:needs-review enhancement

Motivation

LiteLLM's core value proposition is request-level retries and fallback routing; Pydantic AI ships FallbackModel(default, fallbacks=..., fallback_on=...) (https://ai.pydantic.dev/api/models/fallback/) so an agent can drop from a primary to a backup model/provider on failure; LangChain has .with_retry() and .with_fallbacks() runnable wrappers. Chatlas has neither today. A transient 429/500/connection error in the middle of an overnight batch_chat() job, or partway through a large parallel_chat() run, currently just raises and is captured as an Exception per-conversation (see _parallel_chat_impl in chatlas/_parallel.py, which has on_error: Literal["return", "continue", "stop"] and a fixed-rate RateLimiter, but no retry-with-backoff — a failed request is simply recorded via ErrorController.record_error() and never resubmitted).

Note that chatlas already relies on the official openai/anthropic Python SDKs (constructed in chatlas/_provider_openai.py's ChatOpenAI() and chatlas/_provider_anthropic.py's ChatAnthropic()), which default to max_retries=2 internally and are configurable today by passing kwargs_client={"max_retries": ...} through to the underlying OpenAI()/Anthropic() constructor — this is undocumented and doesn't help higher-level orchestration failures (e.g. after SDK retries are exhausted, or provider-agnostic paths like ChatGoogle/ChatBedrockAnthropic). ellmer hit exactly this gap in tidyverse/ellmer#722 ("chat_openai() errors instead of backing off") and closed it by adding ellmer_req_robustify() in R/httr2.R, which wraps every provider's HTTP request with httr2::req_retry(max_tries = getOption("ellmer_max_tries", 3), is_transient = ..., retry_on_failure = TRUE), with some providers (e.g. provider-claude.R) overriding is_transient for provider-specific transient-error detection. Because ellmer's providers talk HTTP directly (no vendor SDK), this pattern doesn't map 1:1 onto chatlas, which should layer on top of — not duplicate — the vendor SDKs' own retry logic.

Proposed approach

import chatlas as ctl

# (a) Opt-in retry w/ backoff, layered above SDK-level retries, for
# orchestration-level failures (e.g. exhausted SDK retries, or providers
# like ChatGoogle without built-in retry semantics)
chat = ctl.ChatAnthropic()
chats = await ctl.parallel_chat(
    chat,
    prompts,
    on_error="return",
    retry=ctl.RetryPolicy(max_tries=3, backoff="exponential", base_delay=1.0),
)

# (b) Opt-in ordered fallback across models/providers
chat = ctl.ChatOpenAI(model="gpt-5", fallback=[ctl.ChatAnthropic(), ctl.ChatOpenAI(model="gpt-5-mini")])
chat.chat("Summarize this report")  # falls through the list on transient failure

Candidate implementation points: extend ErrorController in chatlas/_parallel.py to resubmit (rather than just record) on transient errors before giving up, since that's where unattended runs already live; and/or add a retry=/fallback= kwarg to Chat.__init__ in chatlas/_chat.py so it also covers plain .chat()/.stream() calls. A shared transient-error classifier (429, 502/503/504, connection errors — explicitly not generic 4xx) could live near chatlas/_provider.py's Provider ABC so all providers classify consistently.

Caution: LiteLLM's default behavior of retrying on some 4xx client errors has caused documented retry-storm cost spikes for users; chatlas should default to retrying only clearly transient errors (429 with backoff honoring Retry-After, 5xx, connection resets) and never blanket-retry 4xx.

Alternatives / prior art

  • ellmer: ellmer_req_robustify() (R/httr2.R), options(ellmer_max_tries=), closed via tidyverse/ellmer#722.
  • Pydantic AI: FallbackModel (https://ai.pydantic.dev/api/models/fallback/).
  • LangChain: Runnable.with_retry() / .with_fallbacks().
  • LiteLLM: Router with fallbacks=/num_retries=.

Open questions

  • Does this belong on Chat.__init__, as a per-call kwarg to .chat()/.stream(), or scoped only to parallel_chat/batch_chat where unattended runs happen? (Could start scoped to parallel_chat, per ellmer's parallel_chat_structured_robust discussion in tidyverse/ellmer#628, and expand later.)
  • Should fallback trigger only on transient errors, or also on content-policy refusals / empty completions?
  • Should retry state/attempts be surfaced anywhere (e.g. a warning, a field on the returned Chat), given the "operator wants a stop reason" feedback seen on the related ellmer tool-loop budget issue (tidyverse/ellmer#958)?

Drafted from a competitive review of llm / Pydantic AI / LangChain / LiteLLM (July 2026); filed via Claude Code on behalf of @cpsievert.

主要语言
Python
星标
179
派生
32
平均合并
1 天 3 小时
30 天内合并 PR
10

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

posit-dev/chatlas 的其他 Issue

查看 posit-dev/chatlas 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。