Skip to content

BUG: OpenAIModel raises AttributeError when gpt-5 returns tool-only response #1825

@chahn

Description

@chahn

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

  1. Save the script below as bug_stop_seq.py and add your OpenAI API Key: api_key="sk-..."
  2. Run uv run bug_stop_seq.py.
  3. 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.

Environment:
Please complete the following information:

  • OS: macOS 14.6.1 (Apple Silicon)
  • Python version: 3.12.11
  • Package version: 1.22.0

Additional context

  • The crash is specific to models that emit tool-only steps (e.g., gpt-5). gpt-4o returns content strings in the same scenario and therefore does not trigger the bug.
  • Proposed fix: Guard remove_content_after_stop_sequences so it returns immediately when content=None, ignores empty stop strings, and tolerates stop_sequences=None. This keeps existing behavior for normal strings while preventing the crash.

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

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions