- 
                Notifications
    
You must be signed in to change notification settings  - Fork 2.1k
 
Closed
Labels
bugSomething isn't workingSomething isn't working
Description
Problem
OpenAIModel throws an error when gpt-5 responds with a tool-only step. The OpenAI API returns message.content = None, but remove_content_after_stop_sequences still tries to call .split() on that value, raising AttributeError: 'NoneType' object has no attribute 'split'.
Steps to reproduce
- Save the script below as 
bug_stop_seq.pyand add your OpenAI API Key:api_key="sk-..." - Run 
uv run bug_stop_seq.py. - Observe the stack trace showing the 
AttributeError. 
# How to run with uv:
#   uv run bug_stop_seq.py
#
# /// script
# requires-python = ">=3.10"
# dependencies = [
#   "smolagents[openai,toolkit]",
# ]
# ///
from smolagents import ToolCallingAgent, OpenAIModel, VisitWebpageTool
def main():
    model = OpenAIModel(model_id="gpt-5", api_key="sk-...")
    visit_tool = VisitWebpageTool()
    agent = ToolCallingAgent(tools=[visit_tool], model=model)
    result = agent.run("Could you get me the title of the page at url 'https://huggingface.co/blog'?")
    print(f"\nAgent result:\n{result}")
if __name__ == "__main__":
    main()Actual behavior and error logs
$ uv run bug_stop_seq.py
      Built smolagents @ file:///Users/chahn/tmp/smolagents
Uninstalled 47 packages in 384ms
Installed 47 packages in 71ms
โญโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ New run โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ                                                                                                                                                            โ
โ Could you get me the title of the page at url 'https://huggingface.co/blog'?                                                                               โ
โ                                                                                                                                                            โ
โฐโ OpenAIServerModel - gpt-5 โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Step 1 โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Error while generating output:
'NoneType' object has no attribute 'split'
[Step 1: Duration 5.04 seconds]
Traceback (most recent call last):
  File "/Users/chahn/.cache/uv/environments-v2/bug-30bad21716a0fa20/lib/python3.12/site-packages/smolagents/agents.py", line 1286, in _step_stream
    chat_message: ChatMessage = self.model.generate(
                                ^^^^^^^^^^^^^^^^^^^^
  File "/Users/chahn/.cache/uv/environments-v2/bug-30bad21716a0fa20/lib/python3.12/site-packages/smolagents/models.py", line 1680, in generate
    content = remove_content_after_stop_sequences(content, stop_sequences)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/chahn/.cache/uv/environments-v2/bug-30bad21716a0fa20/lib/python3.12/site-packages/smolagents/models.py", line 78, in remove_content_after_stop_sequences
    split = content.split(stop_seq)
            ^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'split'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "/Users/chahn/tmp/smolagents/bug_stop_seq.py", line 35, in <module>
    main()
  File "/Users/chahn/tmp/smolagents/bug_stop_seq.py", line 29, in main
    result = agent.run("Could you get me the title of the page at url 'https://huggingface.co/blog'?")
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/chahn/.cache/uv/environments-v2/bug-30bad21716a0fa20/lib/python3.12/site-packages/smolagents/agents.py", line 499, in run
    steps = list(self._run_stream(task=self.task, max_steps=max_steps, images=images))
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/chahn/.cache/uv/environments-v2/bug-30bad21716a0fa20/lib/python3.12/site-packages/smolagents/agents.py", line 596, in _run_stream
    raise e
  File "/Users/chahn/.cache/uv/environments-v2/bug-30bad21716a0fa20/lib/python3.12/site-packages/smolagents/agents.py", line 578, in _run_stream
    for output in self._step_stream(action_step):
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/chahn/.cache/uv/environments-v2/bug-30bad21716a0fa20/lib/python3.12/site-packages/smolagents/agents.py", line 1302, in _step_stream
    raise AgentGenerationError(f"Error while generating output:\n{e}", self.logger) from e
smolagents.utils.AgentGenerationError: Error while generating output:
'NoneType' object has no attribute 'split'Expected behavior
When the model responds via tool calls (returning content=None), remove_content_after_stop_sequences should short-circuit instead of calling .split(), allowing the agent run to continue without an exception.
Additional context
- The crash is specific to models that emit tool-only steps (e.g., 
gpt-5).gpt-4oreturnscontentstrings in the same scenario and therefore does not trigger the bug. - Proposed fix: Guard 
remove_content_after_stop_sequencesso it returns immediately whencontent=None, ignores empty stop strings, and toleratesstop_sequences=None. This keeps existing behavior for normal strings while preventing the crash. 
Suggested fix
A fix for this issue could look like this PR: #1826
Environment
- OS: macOS 14.6.1 (Apple Silicon)
 - Python version: 3.12.11
 - Package version: 1.22.0
 
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)
 
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working