Skip to content

fix: cannot use tools in siliconflow provider#6829

Merged
Soulter merged 2 commits intomasterfrom
fix/siliconflow-tool-call
Mar 23, 2026
Merged

fix: cannot use tools in siliconflow provider#6829
Soulter merged 2 commits intomasterfrom
fix/siliconflow-tool-call

Conversation

@Soulter
Copy link
Member

@Soulter Soulter commented Mar 23, 2026

#6439 also fixed this bug, but it introduces lots of variables, not that elegant.

Modifications / 改动点

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

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


Checklist / 检查清单

  • 😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
    / 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。

  • 👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
    / 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”

  • 🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in requirements.txt and pyproject.toml.
    / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txtpyproject.toml 文件相应位置。

  • 😮 My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

@auto-assign auto-assign bot requested review from LIghtJUNction and anka-afk March 23, 2026 04:50
@dosubot dosubot bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Mar 23, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses an issue preventing the proper use of tools within the siliconflow provider. It introduces a targeted fix to ensure that tool calls are correctly identified and processed during streaming, thereby restoring full tool functionality for users of this provider. Additionally, it refines error logging for stream processing to better highlight critical issues.

Highlights

  • Siliconflow Tool Call Fix: Implemented a specific workaround for the siliconflow provider to correctly process tool calls by explicitly setting the type of tool calls to 'function' when function arguments are present in the delta.
  • Logging Improvement: Elevated the log level from warning to error for exceptions encountered while saving chunk state during stream processing, indicating a more critical issue.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@dosubot dosubot bot added the area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. label Mar 23, 2026
Copy link
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:

  • Accessing chunk.choices[0] before the if not chunk.choices: continue guard can raise an IndexError on empty choices; move that guard above the new choice = chunk.choices[0] line.
  • Consider whether changing the log level from warning to error for Saving chunk state error is appropriate here, as this might flood error logs for recoverable or expected streaming issues.
  • The Siliconflow-specific tool_calls workaround is embedded directly in the generic OpenAI streaming path; it might be cleaner to encapsulate this provider-specific adjustment behind a feature flag or provider check so it doesn’t affect other providers.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Accessing `chunk.choices[0]` before the `if not chunk.choices: continue` guard can raise an `IndexError` on empty choices; move that guard above the new `choice = chunk.choices[0]` line.
- Consider whether changing the log level from `warning` to `error` for `Saving chunk state error` is appropriate here, as this might flood error logs for recoverable or expected streaming issues.
- The Siliconflow-specific `tool_calls` workaround is embedded directly in the generic OpenAI streaming path; it might be cleaner to encapsulate this provider-specific adjustment behind a feature flag or provider check so it doesn’t affect other providers.

## Individual Comments

### Comment 1
<location path="astrbot/core/provider/sources/openai_source.py" line_range="332-341" />
<code_context>
         state = ChatCompletionStreamState()

         async for chunk in stream:
+            choice = chunk.choices[0]
+            delta = choice.delta
+
+            # siliconflow workaround
+            if dtcs := delta.tool_calls:
+                for tc in dtcs:
+                    if tc.function and tc.function.arguments:
+                        tc.type = "function"
             try:
                 state.handle_chunk(chunk)
             except Exception as e:
-                logger.warning("Saving chunk state error: " + str(e))
+                logger.error("Saving chunk state error: " + str(e))
             if not chunk.choices:
                 continue
-            choice = chunk.choices[0]
</code_context>
<issue_to_address>
**issue (bug_risk):** Accessing chunk.choices[0] before checking for emptiness can raise an IndexError.

The previous `if not chunk.choices: continue` guard ran before `chunk.choices[0]`. With the new ordering, `choice = chunk.choices[0]` can run when `chunk.choices` is empty (which does occur in some streaming scenarios), causing an `IndexError`. Move the emptiness check back before the indexing so empty chunks are skipped first.
</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.

Copy link
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 adds a workaround for the SiliconFlow provider to enable tool usage. While the core change for the workaround seems correct, the refactoring has introduced a critical bug that could cause an IndexError when processing streaming chunks. I've provided a review comment with a code suggestion that fixes this bug and also includes a minor improvement to the error logging.

@Soulter Soulter merged commit b7e8b33 into master Mar 23, 2026
6 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. size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant