huggingface / huggingface/smolagents
BUG: TypeError: 'ChatMessage' object is not iterable when use **ChatOpenAI & CodeAgent**
- Dominant language
- Python
- Stars
- 29.3k
- Forks
- 3k
- Avg merge
- 17m
- Merged PRs (30d)
- 2
Description
**Problem**
simple test code,while meeting something worong, when use **ChatOpenAI & CodeAgent**
```python
from langchain_openai import ChatOpenAI
from smolagents import (
CodeAgent,
LiteLLMModel,
OpenAIModel,
WebSearchTool
)
model = ChatOpenAI(
model_name="Qwen/Qwen3-30B-A3B-thinking-2507",
api_key=os.getenv("MODELSCOPE_API_KEY"),
base_url=os.getenv("MODELSCOPE_BASE_URL"),
temperature=0.2, # 随机性,0-1之间,值越高回复越多样
max_tokens=1024, # 最大生成token数
streaming=True,
)
print(model.invoke("你好"))
agent = CodeAgent(
model=model,
tools=[],
planning_interval=3
)
print(agent.run("你好"))
```
**Actual behavior and error logs**
print(agent.run("你好"))
^^^^^^^^^^^^^^^
File "D:\miniconda3\envs\agent_mcp_skill\Lib\site-packages\smolagents\agents.py", line 498, in run
steps = list(self._run_stream(task=self.task, max_steps=max_steps, images=images))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\miniconda3\envs\agent_mcp_skill\Lib\site-packages\smolagents\agents.py", line 554, in _run_stream
for element in self._generate_planning_step(
File "D:\miniconda3\envs\agent_mcp_skill\Lib\site-packages\smolagents\agents.py", line 671, in _generate_planning_step
plan_message = self.model.generate(input_messages, stop_sequences=[""])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\miniconda3\envs\agent_mcp_skill\Lib\site-packages\langchain_core\language_models\chat_models.py", line 913, in generate
_format_for_tracing(message_list) for message_list in messages
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\miniconda3\envs\agent_mcp_skill\Lib\site-packages\langchain_core\language_models\chat_models.py", line 129, in _format_for_tracing
for message in messages:
TypeError: 'ChatMessage' object is not iterable
```shell
```
**Expected behavior**
A clear and concise description of what you expected to happen.
**Environment:**
Please complete the following information:
- OS: [e.g., Linux, macOS, Windows]
- Python version: [e.g., 3.10]
- Package version: (output of `pip show smolagents`)
```
```
**Additional context (optional)**
Add any other context, screenshots, or links about the bug here.
---
### Checklist
- [ ] I have searched the existing issues and have not found a similar bug report.
- [ ] I have provided a minimal, reproducible example.
- [ ] I have provided the full traceback of the error.
- [ ] I have provided my environment details.
- [ ] I am willing to work on this issue and submit a pull request. (optional)
Contributor guide
Research direction
Start with smolagents/agents.py at _generate_planning_step and compare the input passed to model.generate with langchain_core/language_models/chat_models.py:913. Reproduce the ChatOpenAI and CodeAgent example, then verify that planning completes without the reported TypeError and that the existing agent behavior remains intact.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- ai
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100