Skip to content

fix: support both old and new Bailian Rerank API response formats#7217

Merged
Soulter merged 2 commits intoAstrBotDevs:masterfrom
he-yufeng:fix/bailian-rerank-response
Apr 1, 2026
Merged

fix: support both old and new Bailian Rerank API response formats#7217
Soulter merged 2 commits intoAstrBotDevs:masterfrom
he-yufeng:fix/bailian-rerank-response

Conversation

@he-yufeng
Copy link
Copy Markdown
Contributor

@he-yufeng he-yufeng commented Mar 31, 2026

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

  1. 配置阿里云百炼 qwen3-rerank 模型
  2. 使用新版 API https://dashscope.aliyuncs.com/compatible-api/v1/reranks
  3. 点击测试,验证能正确返回重排序结果

Fixes #7161

Summary by Sourcery

Bug Fixes:

  • Ensure Bailian Rerank results are correctly parsed when using the new compatible API response structure that returns data.results instead of data.output.results.

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
@dosubot dosubot bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Mar 31, 2026
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:

  • 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.
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>

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.

@dosubot dosubot bot added the area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. label Mar 31, 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 _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>
@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Apr 1, 2026
@Soulter Soulter merged commit 4d2791a into AstrBotDevs:master Apr 1, 2026
7 checks passed
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. lgtm This PR has been approved by a maintainer size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] 使用阿里云百炼 qwen3-rerank 重排序模型时报错

3 participants