FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix(provider): 关闭 OpenAI SDK 内建重试,避免与 AstrBot 重试层叠加导致请求数翻倍 | Disable OpenAI SDK built-in retries to prevent request multiplication from stacked retry layers by SweetenedSuzuka · Pull Request #9669 · AstrBotDevs/AstrBot · GitHub

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
6 changes: 6 additions & 0 deletions astrbot/core/provider/sources/openai_source.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,9 @@ def __init__(self, provider_config, provider_settings) -> None:
default_headers=self.custom_headers,
base_url=provider_config.get("api_base", ""),
timeout=self.timeout,
# Retry is handled by retry_provider_request(); disable the
# SDK built-in retry to avoid stacking request attempts.
max_retries=0,
http_client=self._create_http_client(provider_config),
)
else:
Expand All @@ -386,6 +389,9 @@ def __init__(self, provider_config, provider_settings) -> None:
base_url=provider_config.get("api_base", None),
default_headers=self.custom_headers,
timeout=self.timeout,
# Retry is handled by retry_provider_request(); disable the
# SDK built-in retry to avoid stacking request attempts.
max_retries=0,
http_client=self._create_http_client(provider_config),
)

Expand Down
49 changes: 49 additions & 0 deletions tests/test_openai_source.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import httpx
import pytest
from openai import AsyncAzureOpenAI, AsyncOpenAI
from openai.types.chat.chat_completion import ChatCompletion
from openai.types.chat.chat_completion_chunk import ChatCompletionChunk
from PIL import Image as PILImage
Expand Down Expand Up @@ -60,6 +61,54 @@ def _make_groq_provider(overrides: dict | None = None) -> ProviderGroq:
)


@pytest.mark.parametrize(
("overrides", "expected_client"),
[
({}, AsyncOpenAI),
({"api_version": "2024-02-01"}, AsyncAzureOpenAI),
],
)
@pytest.mark.asyncio
async def test_provider_client_disables_sdk_builtin_retries(overrides, expected_client):
provider = _make_provider(overrides)
try:
assert isinstance(provider.client, expected_client)
assert provider.client.max_retries == 0
finally:
await provider.terminate()


@pytest.mark.asyncio
async def test_query_attempts_exactly_request_max_retries_times(monkeypatch):
monkeypatch.setattr(request_retry, "REQUEST_RETRY_WAIT_MIN_S", 0)
monkeypatch.setattr(request_retry, "REQUEST_RETRY_WAIT_MAX_S", 0)

provider = _make_provider()
try:
calls = 0

async def failing_create(**kwargs):
nonlocal calls
calls += 1
raise httpx.ConnectError("temporary connection failure")

monkeypatch.setattr(provider.client.chat.completions, "create", failing_create)

with pytest.raises(httpx.ConnectError):
await provider._query(
payloads={
"model": "gpt-4o-mini",
"messages": [{"role": "user", "content": "hi"}],
},
tools=None,
request_max_retries=2,
)

assert calls == 2
finally:
await provider.terminate()


def test_create_http_client_uses_openai_httpx_module(monkeypatch):
captured: dict[str, object] = {}
fake_httpx_module = object()
Expand Down
Loading

Back | FazBrowse Home | New Git URL