Skip to content

Commit 6cd2173

Browse files
committed
fix:重新加入空回复处理 已验证并测试
1 parent 20ca527 commit 6cd2173

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

astrbot/core/agent/runners/tool_loop_agent_runner.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,19 @@ async def _iter_llm_responses_with_fallback(
241241
has_stream_output = False
242242
try:
243243
async for resp in self._iter_llm_responses(include_model=idx == 0):
244+
# 如果回复为空且无工具调用 且不是最后一个回退渠道 则引发fallback(仅适配非流)
245+
# 此处不应判断整个消息链是否为空 因为消息链包含整个对话流 而空回复可能发生在任何阶段
246+
if (
247+
not resp.completion_text
248+
and not resp.tools_call_args
249+
and not is_last_candidate
250+
):
251+
logger.warning(
252+
"Chat Model %s returns empty response, trying fallback to next provider.",
253+
candidate_id,
254+
)
255+
break
256+
244257
if resp.is_chunk:
245258
has_stream_output = True
246259
yield resp

astrbot/core/provider/sources/openai_source.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ async def _query_stream(
315315
if not choice.delta or not choice.delta.tool_calls:
316316
continue
317317
for tool_call in choice.delta.tool_calls:
318-
if getattr(tool_call, "type", None) in (None, ""):
318+
if hasattr(tool_call, "type") and tool_call.type in (None, ""):
319319
tool_call.type = "function"
320320

321321
try:
@@ -326,8 +326,8 @@ async def _query_stream(
326326
exc_info=True,
327327
)
328328
logger.warning(f"Saving chunk state error: {e}")
329-
330-
if not chunk.choices or len(chunk.choices) == 0:
329+
330+
if not chunk.choices:
331331
continue
332332
delta = chunk.choices[0].delta
333333
# logger.debug(f"chunk delta: {delta}")

0 commit comments

Comments
 (0)