Skip to content

feat: add provider context to retry logs - #10177

Open
iuiu-py wants to merge 1 commit into
AstrBotDevs:masterfrom
iuiu-py:feat/detailed-provider-retry-logs
Open

iuiu-py wants to merge 1 commit into
AstrBotDevs:masterfrom
iuiu-py:feat/detailed-provider-retry-logs

Conversation

@iuiu-py

@iuiu-py iuiu-py commented Sep 21, 2026

Copy link
Copy Markdown

Motivation

Fixes #9453

Retryable provider request failures currently identify the adapter label, such as OpenAI or Gemini, but not the configured provider instance or model. This makes it hard to decide which model to switch when several providers use the same adapter.

Modifications / 改动点

  • Added optional provider_id and model context to provider retry logging.

  • Added a reusable provider metadata helper without changing provider behavior.

  • Passed request-specific model and configured provider IDs through OpenAI Chat Completions, OpenAI Responses, Gemini, Anthropic, and SSYCloud retry calls.

  • Preserved the existing log format when no metadata is supplied.

  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果

  • uv run pytest -q tests/test_request_retry.py tests/test_openai_source.py tests/test_gemini_source.py tests/test_anthropic_kimi_code_provider.py: 114 passed.
  • uv run pytest -q: 3527 passed, 82 skipped.
  • uv run ruff format --check .: passed.
  • uv run ruff check .: passed.
  • git diff --check: passed.

Checklist / 检查清单

  • This enhancement was discussed in issue [Feature]模型重试日志可以更详细 #9453.
  • My changes have been well-tested, and verification results have been provided above.
  • I checked the affected WebUI instructions and screenshots; no documentation update is needed because log fields do not change any user-facing workflow.
  • I have ensured that no new dependencies are introduced.
  • My changes do not introduce malicious code.

Summary by Sourcery

Identify the configured provider instance and model in retry logs to make provider request failures easier to diagnose.

New Features:

  • Add provider instance and model context to retry warning logs across supported provider integrations.

Enhancements:

  • Preserve the existing retry log format when no provider metadata is available.

Tests:

  • Add coverage for retry log metadata and backward-compatible log formatting.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="astrbot/core/provider/provider.py" line_range="43-45" />
<code_context>
             provider_config.get("custom_headers")
         )

+    def _retry_log_metadata(self, model: str | None = None) -> dict[str, str]:
+        """Build identifying context for provider request retry logs."""
+        provider_config = getattr(self, "provider_config", None) or {}
+        metadata = {"provider_id": str(provider_config.get("id", "default"))}
+        resolved_model = (
+            model if model is not None else getattr(self, "model_name", None)
+        )
+        if resolved_model:
+            metadata["model"] = str(resolved_model)
+        return metadata
+
     def set_model(self, model_name: str) -> None:
</code_context>
<issue_to_address>
**nitpick (bug_risk):** The helper falls back to `self.model_name` whenever no request model is supplied, and all `get_models()` calls invoke it without a model. When a provider has a configured model, a retry from `client.models.list()` is therefore logged as if it were a request for that model, even though the model-list request does not target it.

**Triggers:** When the model catalog request fails for a provider whose `model_name` is configured, which is the normal provider configuration.

**Suggested fix:** Pass `model=None` without falling back to `self.model_name` for model-catalog requests, or add a separate metadata helper that includes only the provider ID for `get_models()`.

```suggestion
        resolved_model = model
```
</issue_to_address>

Sourcery assessment

Approved.


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment on lines +43 to +45
resolved_model = (
model if model is not None else getattr(self, "model_name", None)
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick (bug_risk): The helper falls back to self.model_name whenever no request model is supplied, and all get_models() calls invoke it without a model. When a provider has a configured model, a retry from client.models.list() is therefore logged as if it were a request for that model, even though the model-list request does not target it.

Triggers: When the model catalog request fails for a provider whose model_name is configured, which is the normal provider configuration.

Suggested fix: Pass model=None without falling back to self.model_name for model-catalog requests, or add a separate metadata helper that includes only the provider ID for get_models().

Suggested change
resolved_model = (
model if model is not None else getattr(self, "model_name", None)
)
resolved_model = model

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]模型重试日志可以更详细

1 participant