Skip to content

[Bug]:ReActAgent 调用不存在的工具时直接退出 ReAct 循环,未给模型重试机会 #3102

Open

Description

@LilG123

ReActAgent 调用不存在的工具时直接退出 ReAct 循环,未给模型重试机会

问题描述

当 LLM 在流式响应中尝试调用一个不在 tools schema 里的工具(如把 Skill 名 metric_condition_generation 当工具调用),LLM 服务端会返回 error。agentscope 的 OpenAIClient 把这个 error 包装为 OpenAIException 抛出,ReActAgent.reasoning() 只捕获 InterruptedException,其他异常直接传播终止整个 streamEvents 流。

模型没有机会纠正错误、换一个工具或改用纯文本回复——整个对话直接以 error 结束。

复现场景

  1. 配置了一个 Agent,有 tools 但没有 metric_condition_generation 这个工具
  2. 模型在 reasoning 阶段幻觉式调用了 metric_condition_generation(这是一个 Skill 名,不是工具名)
  3. LLM 服务端在流式响应中返回 error:LLM is trying to invoke a non-exist tool: "metric_condition_generation"
  4. OpenAIClient 把 error 包装为 OpenAIException 并抛出
  5. ReActAgent.reasoning() 的 onErrorResume 只捕获 InterruptedException,OpenAIException 直接传播
  6. 整个 streamEvents 流以 error 终止,对话直接结束

实际结果

OpenAI API error in streaming response: LLM is trying to invoke a non-exist tool: "metric_condition_generation", you can add some few shots examples or adjust the prompt.

对话直接终止,用户看到错误,没有结果。

期望结果

当 LLM 尝试调用不存在的工具时,框架应该:

  1. 捕获这个 error,不终止整个 ReAct 循环
  2. 构造一个 ToolResultBlock.error("工具 X 不存在,请使用已注册的工具或直接回复用户") 作为该 tool_call 的结果
  3. 把这个 error 结果加入上下文,继续下一轮 reasoning——让模型看到错误后自行纠正
  4. 如果模型多次(如 2~3 次)仍然调用不存在的工具,才终止循环

根因分析

错误产生路径

LLM 返回 tool_call(name="metric_condition_generation")
  → LLM 服务端发现该工具不在 tools schema 里
    → 流式响应返回 error
      → OpenAIClient/OpenAIResponseParser 解析 error
        → 抛出 OpenAIException("OpenAI API error in streaming response: LLM is trying to invoke a non-exist tool: ...")
          → ReActAgent.reasoning() 的 model.stream() 报错
            → onErrorResume 只捕获 InterruptedException
              → OpenAIException 直接传播
                → streamEvents 流终止

涉及的代码

OpenAIClient(agentscope-extensions-model-openai):

  • OpenAIClient.stream() 解析流式响应时,发现 OpenAIResponse.isError() 为 true
  • 构造 OpenAIException 并通过 SynchronousSink.error() 抛出

OpenAIResponseParser(agentscope-extensions-model-openai):

  • 同样有 "OpenAI API error in streaming response" 字符串

ReActAgent.CallExecution.reasoning()(agentscope-core):

  • model.stream().doOnNext(this::publishEvent).then(...) 的 stream 因 error 终止
  • onErrorResume(InterruptedException.class, ...) 只捕获 InterruptedException
  • 其他异常(如 OpenAIException)直接传播,终止整个 reasoning 循环

对比 ToolExecutor 的处理

ToolExecutor.invokeTool() 在工具不存在时(toolRegistry.getTool(name) 返回 null),会构造 ToolResultBlock.error("Tool not found: " + name) 并正常返回,不抛异常。这给了模型重试的机会。

但 LLM 服务端在流式阶段就拦截了——工具名不在 tools schema 里,服务端直接返回 error,不走 agentscope 的 ToolExecutor。所以 ToolExecutor 的兜底逻辑在这个场景下不生效。

建议修复

方案 A:在 reasoning 的 onErrorResume 中捕获 OpenAIException

// ReActAgent.CallExecution.reasoning() 里
.onErrorResume(OpenAIException.class, error -> {
    // 构造一个 error ToolResultBlock,让模型在下一轮看到错误后自行纠正
    Msg errorMsg = Msg.builder()
        .role(MsgRole.ASSISTANT)
        .content(TextBlock.builder().text("工具调用失败: " + error.getMessage()).build())
        .build();
    state.contextMutable().add(errorMsg);
    // 继续下一轮 reasoning,给模型纠正的机会
    return reasoning(iter + 1, true);  // ignoreMaxIters=true 保证至少重试一次
})

方案 B:在 OpenAIClient 里不抛异常,改为返回 error ChatResponse

// OpenAIClient.stream() 里解析到 error response 时
if (response.isError()) {
    // 不抛异常,改为构造一个携带错误信息的 ChatResponse
    return Flux.just(ChatResponse.error(response.getEffectiveErrorMessage()));
}

这样 ReActAgent.modelCallStream 的 concatMap 会正常处理这个 error ChatResponse,可以转换为 error event 发给下游,不会终止流。

推荐方案 B

方案 A 在 reasoning 层兜底,但 error 的语义不明确(是工具不存在还是网络错误?);方案 B 在源头处理,让 error 成为流中的一个正常事件,框架可以在 concatMap 里把它转成 error ToolResultBlock,和 ToolExecutor 的 "Tool not found" 处理对齐。

环境信息

  • agentscope-core: 2.0.3
  • agentscope-extensions-model-openai: 2.0.3
  • JDK: 17
  • 模型服务: 百炼 qwen-plus / antchat Qwen3.5-35B-A3B

Activity

  1. added
    bug
    on Sep 11, 2026
  2. CryoThrust commented on Sep 11, 2026

    @CryoThrust
    Contributor

    I traced the proposed paths and agree this is worth treating as a framework-level recovery contract, but I would avoid converting every provider/API exception into a retry. A safer boundary is:

    • classify only a provider error that explicitly means an unknown tool name (or expose a typed capability on ChatResponse); network/auth/rate-limit/schema errors should still fail fast;
    • preserve the original assistant/tool-call intent in context and append a structured tool error tied to the attempted name, rather than a free-form assistant text message;
    • retry through the normal ReAct iteration budget (no ignoreMaxIters=true bypass), with a small configurable cap for protocol-repair attempts so a model cannot loop forever;
    • include the registered tool names or a compact schema hint only if the provider response is safe to expose, and avoid leaking credentials/provider internals;
    • add tests for one recoverable unknown-tool error followed by a valid response, repeated unknown-tool errors exhausting the cap, and non-recoverable OpenAI errors still propagating.

    If maintainers agree on a typed error classification in the OpenAI extension, I can help with the core retry/state-machine test slice without changing normal provider error semantics.

  3. oss-maintainer commented on Sep 11, 2026

    @oss-maintainer
    Collaborator

    Issue Evaluation

    Category: bug | Status: Confirmed

    Verified against main @ c5db8f7.

    Root cause: the provider-side "non-existent tool" error is turned into a terminal stream error, and the ReAct loop does not recover from it.

    • agentscope-extensions/.../model/openai/OpenAIClient.java:417-428 — stream() emits OpenAIException via sink.error(...) when a chunk reports an error.
    • agentscope-extensions/.../model/openai/formatter/OpenAIResponseParser.java:342-354 — same error path for chunk parsing.
    • agentscope-core/.../ReActAgent.java:2309-2438 — reasoning() only resumes on InterruptedException (2406-2423), so OpenAIException propagates and aborts the whole streamEvents flow.
    • agentscope-core/.../tool/ToolExecutor.java:188-189 already handles local "tool not found" as ToolResultBlock.error(...), but that path is unreachable when the LLM service rejects the tool name during streaming.

    Impact: a single hallucinated tool call kills the whole turn; the model never gets a chance to self-correct. Severity: high.

    Suggested fix: map the provider-level tool-name error back into the conversation as a ToolResultBlock.error(...) result (mirroring ToolExecutor's "Tool not found" handling) and continue reasoning, with a small cap (2-3) on consecutive invalid-tool retries before failing the turn.

    An automated fix proposal will be generated. Reply /approve to proceed with PR generation.


    Automated evaluation by github-manager-bot

  4. added
    area/core/agent
    area/core/model
    on Sep 11, 2026
  5. oss-maintainer commented on Sep 14, 2026

    @oss-maintainer
    Collaborator

    Status: 72 h feedback window expired — no automated PR will be generated, this proposal window here is closed

    The fix proposal evaluated above went 72 hours without community feedback, so per our workflow we are not generating a competing PR and the window is closed. No action is needed from anyone.

    For context, this issue is already covered by an open community PR:

    As of this check #3131 is MERGEABLE with reviewDecision=APPROVED and all 6 required checks passing (build ubuntu/windows, Check License, Check Module Sync, codecov/patch, CLA), most recently updated at head 08c630f. It targets the same root cause as the evaluation above — turning a provider-side "non-existent tool" streaming error back into an in-conversation tool result so the ReAct loop keeps going instead of aborting the turn.

    #3131 stays the active path for this issue. If a maintainer would still prefer an independent proposal here, reopen the discussion on the issue and we will re-evaluate.

    Automated by github-manager-bot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/core/agentarea/core/modelbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions