fix: support both old and new Bailian Rerank API response formats#7217
Merged
Soulter merged 2 commits intoAstrBotDevs:masterfrom Apr 1, 2026
Merged
fix: support both old and new Bailian Rerank API response formats#7217Soulter merged 2 commits intoAstrBotDevs:masterfrom
Soulter merged 2 commits intoAstrBotDevs:masterfrom
Conversation
The new compatible API (compatible-api/v1/reranks) returns results at the top level as data.results, while the old API returns them nested under data.output.results. The parser only checked the old path, causing qwen3-rerank to always report empty results. Fixes AstrBotDevs#7161
Contributor
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- Using
orto fall back fromdata.get('output', {}).get('results')todata.get('results', [])conflates a legitimately empty list with a missing field; consider explicitly checking foris Noneso that an intentionally emptyoutput.resultsis not silently overridden byresultswhen both are present.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Using `or` to fall back from `data.get('output', {}).get('results')` to `data.get('results', [])` conflates a legitimately empty list with a missing field; consider explicitly checking for `is None` so that an intentionally empty `output.results` is not silently overridden by `results` when both are present.
## Individual Comments
### Comment 1
<location path="astrbot/core/provider/sources/bailian_rerank_source.py" line_range="145-146" />
<code_context>
)
- results = data.get("output", {}).get("results", [])
+ # 兼容旧版 API (output.results) 和新版 compatible API (results)
+ results = data.get("output", {}).get("results") or data.get("results", [])
if not results:
logger.warning(f"百炼 Rerank 返回空结果: {data}")
</code_context>
<issue_to_address>
**question:** Consider whether falling back on `or` should treat an empty legacy `output.results` as "missing".
Using `or` here means an empty `output.results` will be treated the same as `None` and replaced by `data["results"]`, so the warning/early return is no longer driven by the legacy field being empty. If the legacy API can legitimately return `[]` and that should remain authoritative when `results` is also present, consider an explicit `is not None` check instead:
```python
legacy_results = data.get("output", {}).get("results")
if legacy_results is not None:
results = legacy_results
else:
results = data.get("results", [])
```
If you do want `results` to win whenever `output.results` is falsy, the current code is fine.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Contributor
There was a problem hiding this comment.
Code Review
This pull request updates the _parse_results method in the Bailian rerank source to ensure compatibility with both legacy and new API response formats. The feedback identifies a potential AttributeError if the 'output' field is explicitly null and provides a suggestion to improve the robustness of the data extraction logic.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
RC-CHN
approved these changes
Apr 1, 2026
Soulter
approved these changes
Apr 1, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
commit 971bcba 要求 qwen3-rerank 使用新版 compatible API (
/compatible-api/v1/reranks),但没有更新响应解析逻辑。新 API 返回data.results,而解析器只检查旧路径data.output.results,导致 qwen3-rerank 始终返回空结果。改为同时兼容两种格式:优先检查
output.results(旧 API),回退到results(新 API)。Changes
bailian_rerank_source.py:_parse_results()兼容新旧两种响应格式Test
https://dashscope.aliyuncs.com/compatible-api/v1/reranksFixes #7161
Summary by Sourcery
Bug Fixes: