Skip to content

fix(openai-embedding): enhance stability, empty query handling, and SiliconFlow domain compatibility#8547

Open
Rat0323 wants to merge 3 commits into
AstrBotDevs:masterfrom
Rat0323:fix-siliconflow-embedding-dimensions
Open

fix(openai-embedding): enhance stability, empty query handling, and SiliconFlow domain compatibility#8547
Rat0323 wants to merge 3 commits into
AstrBotDevs:masterfrom
Rat0323:fix-siliconflow-embedding-dimensions

Conversation

@Rat0323
Copy link
Copy Markdown
Contributor

@Rat0323 Rat0323 commented Jun 3, 2026

This PR introduces several robustness and stability improvements to the openai_text_embedding provider to prevent crashes, handle empty queries gracefully, and correctly support both SiliconFlow domain configurations.

Key Changes

  1. SiliconFlow Domain Check: Refactored the dimensions removal logic to support both .cn and .com domains case-insensitively (using urlparse to safely extract the hostname) to prevent 400 Bad Request errors (code 20015).
  2. Fail-Fast Key Check: Added initialization validation to verify that embedding_api_key is configured, failing fast with a descriptive error instead of throwing a generic error later.
  3. Safe Timeout Parsing: Wrapped timeout parsing in a try-except block to fall back to 20 if the value is empty or invalid, avoiding ValueError crashes.
  4. Empty Query Protection: Guarded single and batch retrieval entry points to return/raise errors early if input texts are empty or whitespace-only, preventing 400 Bad Request failures.
  5. Exception Handling: Wrapped API exceptions in standard openai.OpenAIError blocks to print descriptive logs during network or API request failures.
  6. Resource Cleanup: Protected client termination by checking getattr(self, 'client', None) inside terminate() to avoid attribute error exceptions during teardown.

@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. labels Jun 3, 2026
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request updates the SiliconFlow API base check in openai_embedding_source.py to be more flexible by checking if 'siliconflow' is present in the lowercase API base URL, rather than strictly matching the start of the URL. There are no review comments to address, and I have no additional feedback to provide.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

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, and left some high level feedback:

  • The new "siliconflow" in provider_api_base.lower() condition is quite broad and could trigger on non-SiliconFlow URLs that happen to contain that string; consider parsing and checking the hostname (e.g. via urlparse) instead of a raw substring match.
  • The previous code used provider_api_base.strip() while the new code does not; if leading/trailing whitespace is still possible in this value, consider applying .strip().lower() to preserve the original robustness.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The new `"siliconflow" in provider_api_base.lower()` condition is quite broad and could trigger on non-SiliconFlow URLs that happen to contain that string; consider parsing and checking the hostname (e.g. via `urlparse`) instead of a raw substring match.
- The previous code used `provider_api_base.strip()` while the new code does not; if leading/trailing whitespace is still possible in this value, consider applying `.strip().lower()` to preserve the original robustness.

## Individual Comments

### Comment 1
<location path="astrbot/core/provider/sources/openai_embedding_source.py" line_range="82" />
<code_context>
             provider_api_base
             # Hard-code SiliconFlow API Base Prefix and Model Name, as it's just a temporary workaround.
-            and provider_api_base.strip().startswith("https://api.siliconflow.cn")
+            and "siliconflow" in provider_api_base.lower()
             and not self.model.lower().startswith("qwen")
         ):
</code_context>
<issue_to_address>
**issue (bug_risk):** The broadened SiliconFlow check may now match unintended API bases.

Previously this only matched `https://api.siliconflow.cn`; now any base containing `"siliconflow"` (e.g., `https://foo-siliconflow-proxy.com`) will match. If you need to support more official SiliconFlow endpoints, consider a stricter host/domain check (e.g., URL parsing or matching explicit allowed prefixes) instead of a simple substring, to avoid affecting unrelated third‑party endpoints.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread astrbot/core/provider/sources/openai_embedding_source.py Outdated
@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:XS This PR changes 0-9 lines, ignoring generated files. labels Jun 3, 2026
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Jun 3, 2026
@Rat0323 Rat0323 changed the title fix(openai-embedding): support both .cn and .com domains case-insensitively for SiliconFlow embedding models fix(openai-embedding): enhance stability, empty query handling, and SiliconFlow domain compatibility Jun 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant