fix: streaming token mode cannot work in function calls and will infi… #5396
+1
−1
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.
Fix: Prevent empty messages accumulation in streaming mode
Why are these changes needed?
In new released autogen 0.4.5, there is a bug in streaming token mode that causes the system to hang when agents attempt to use tool calls. This happens because the current condition
if choice.delta.content is not None:
allows empty strings to pass through, causing empty messages to accumulate in the conversation history:By changing the condition to
if choice.delta.content:
, we prevent these empty messages from accumulating, fixing the hanging issue during tool calls.Reproduce Code
Changes Made
Changed in
python/packages/autogen-ext/src/autogen_ext/models/openai/_openai_client.py
:Checks
Note: There are 3 existing test failures in
tests/test_db_manager.py
that are unrelated to this change and were present before this modification:These errors are related to OpenAI API key configuration and are outside the scope of this fix.
FYI, I can't use OpenAI API directly, therefore I can't test them.
Chinese Version:
修复:防止流式模式下空消息累积
为什么需要这些更改?
在 autogen 0.4.5 版本中,当代理使用工具调用时,流式令牌模式会出现卡住的问题。这是因为当前的条件
if choice.delta.content is not None:
允许空字符串通过,导致空消息在对话历史中累积:通过将条件改为
if choice.delta.content:
,我们可以防止这些空消息累积,从而修复工具调用时的卡住问题。复现代码
参见英文部分
代码变更
在
python/packages/autogen-ext/src/autogen_ext/models/openai/_openai_client.py
中修改:注意:在
tests/test_db_manager.py
中有 3 个已存在的测试失败,这些错误与本次修改无关,且在修改前就已存在。这些错误与 OpenAI API 密钥配置相关,超出了本次修复的范围。