ReActChat crashes on empty model output, including after a completed tool call

Author: icearia0219Created Sep 28, 2026Updated Sep 29, 2026

Problem

ReActChat._run() indexes output[-1] after the model stream finishes, even when the stream produced no messages. This raises IndexError both on the first round and on a later round after a tool has run.

For run_nonstream(), the exception prevents the caller from receiving the tool observation already produced by an earlier round. For run(), it terminates the iterator with an exception after any previously yielded output.

Reproduction

Source: main at 31a4d36d123688581a9e9744427272b33ce940e0 (version 0.0.34). Python 3.12.14, Windows, OpenAI SDK 3.19.2.

This replaces only the model API request with an empty stream; the real ReAct agent and OpenAI-compatible model pipeline still run. No API key or network call is required:

python
from unittest.mock import patch
from qwen_agent.agents import ReActChat
from qwen_agent.llm.oai import TextChatAtOAI
from qwen_agent.llm.schema import Message

model = TextChatAtOAI({
    'model': 'test',
    'api_key': 'test-only',
    'generate_cfg': {'use_raw_api': False},
})
agent = ReActChat(llm=model)

with patch.object(model, '_chat_complete_create', return_value=iter([])):
    print(agent.run_nonstream([Message(role='user', content='Hello')]))

Actual:

qwen_agent/agents/react_chat.py:91
    has_action, action, action_input, thought = self._detect_tool(output[-1].content)
IndexError: list index out of range

The same result occurs when the API returns a terminal chunk with no content, which is filtered out by the model pipeline. The error also reproduces when a normal tool-call round is followed by an empty model round.

Expected behavior / proposed fix

End the ReAct loop cleanly when there is no model output. On the first round, return an empty message list. After a tool round, preserve the accumulated response, including the tool observation. Ensure the empty first-round case still yields a value so run_nonstream() can unpack its result.

A focused patch and nine offline regression cases are prepared. Against the base code, eight empty-response cases fail and the normal-response control passes; with the fix, all nine pass. The tests cover both public run interfaces and verify that a completed tool is not executed again.

Related: #645 reports empty model output in FnCallAgent; #971 fixes VirtualMemoryAgent. This report concerns the separate ReActChat loop.

AI assistance

Prepared, implemented and locally tested with Codex under the authorization of icearia0219. No live model or manual end-to-end validation was performed.