Skip to content

BUG: CodeAgent outputs empty steps that ChatCompletionMessage(content=None,...) #1896

@daviden1013

Description

@daviden1013

Problem
agent.run often generates empty steps (steps with ChatCompletionMessage(content=None)). Results in repeating error message:

Error in code parsing:
expected string or bytes-like object, got 'NoneType'
Make sure to provide correct code blobs.

Steps to reproduce
Please provide a minimal, self-contained, and reproducible example of the bug.

  1. Start vLLM OpenAI compatible server
vllm serve openai/gpt-oss-120b \
   --tensor-parallel-size 4 \
   --enable-prefix-caching \
   --gpu-memory-utilization 0.90 \
   --max-num-seqs 2 \
   --max-model-len 120000 \
   --reasoning-parser openai_gptoss \
   --tool-call-parser openai \
   --enable-auto-tool-choice
  1. Define a simple CodeAgent and run it.
from smolagents import CodeAgent, OpenAIModel

model = OpenAIModel(model_id="openai/gpt-oss-120b",
                    api_key="", 
                    api_base="http://localhost:8000/v1",
                    reasoning_effort="low")

agent = CodeAgent(tools=[], 
                  model=model, executor_type='local', 
                  additional_authorized_imports = ["pandas", "numpy.*", "tableone", "scipy", "scipy.*", "sklearn", "sklearn.*", "statsmodels", "statsmodels.*", "matplotlib", "matplotlib.*", "PIL", "PIL.*"],
                  max_steps=10,
                  stream_outputs=False)

task = "hi"

res = agent.run(task, reset=False)

Actual behavior and error logs
A clear and concise description of what actually happened. Please include the full traceback if an exception was raised.

╭─────────────────────────────────────────────────────────────────────────────────── New run ────────────────────────────────────────────────────────────────────────────────────╮
│                                                                                                                                                                                │
│ hi                                                                                                                                                                             │
│                                                                                                                                                                                │
╰─ OpenAIModel - openai/gpt-oss-120b ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Step 1 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Error in code parsing:
Your code snippet is invalid, because the regex pattern <code>(.*?)</code> was not found in it.
Here is your code snippet:
Hello! How can I assist you today?</code>
Make sure to include code with the correct pattern, for instance:
Thoughts: Your thoughts
<code>
# Your python code here
</code>
Make sure to provide correct code blobs.
[Step 1: Duration 0.38 seconds| Input tokens: 2,089 | Output tokens: 26]
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Step 2 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Error in code parsing:
expected string or bytes-like object, got 'NoneType'
Make sure to provide correct code blobs.
[Step 2: Duration 0.51 seconds| Input tokens: 4,309 | Output tokens: 105]
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Step 3 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Error in code parsing:
expected string or bytes-like object, got 'NoneType'
Make sure to provide correct code blobs.
[Step 3: Duration 0.54 seconds| Input tokens: 6,582 | Output tokens: 189]
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Step 4 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Error in code parsing:
expected string or bytes-like object, got 'NoneType'
Make sure to provide correct code blobs.
[Step 4: Duration 0.48 seconds| Input tokens: 8,908 | Output tokens: 262]
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Step 5 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Error in code parsing:
expected string or bytes-like object, got 'NoneType'
Make sure to provide correct code blobs.
[Step 5: Duration 0.70 seconds| Input tokens: 11,287 | Output tokens: 369]
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Step 6 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Error in code parsing:
expected string or bytes-like object, got 'NoneType'
Make sure to provide correct code blobs.
[Step 6: Duration 0.73 seconds| Input tokens: 13,719 | Output tokens: 479]
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Step 7 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Error in code parsing:
expected string or bytes-like object, got 'NoneType'
Make sure to provide correct code blobs.
[Step 7: Duration 0.76 seconds| Input tokens: 16,204 | Output tokens: 594]
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Step 8 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Error in code parsing:
expected string or bytes-like object, got 'NoneType'
Make sure to provide correct code blobs.
[Step 8: Duration 0.81 seconds| Input tokens: 18,742 | Output tokens: 716]
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Step 9 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Error in code parsing:
expected string or bytes-like object, got 'NoneType'
Make sure to provide correct code blobs.
[Step 9: Duration 0.78 seconds| Input tokens: 21,333 | Output tokens: 833]
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Step 10 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Error in code parsing:
expected string or bytes-like object, got 'NoneType'
Make sure to provide correct code blobs.
[Step 10: Duration 0.61 seconds| Input tokens: 23,977 | Output tokens: 922]
Reached max steps.
[Step 11: Duration 0.34 seconds| Input tokens: 24,712 | Output tokens: 950]

Expected behavior
The agent should generate string contents for Thought and Code.

Environment:
Please complete the following information:

  • OS: Ubuntu 22.04.5
  • Python version: 3.11.8
  • Package version: (output of pip show smolagents)
Name: smolagents
Version: 1.23.0
Summary: 🤗 smolagents: a barebones library for agents. Agents write python code to call tools or orchestrate other agents.
Home-page:
Author:
Author-email: Aymeric Roucher <[email protected]>
License:
Location: /home/daviden1013/miniconda3/envs/NLP/lib/python3.11/site-packages
Requires: huggingface-hub, jinja2, pillow, python-dotenv, requests, rich

Additional context (optional)
I looked into the model.py OpenAIModel.generate method. I think the bug is related to stop_sequences sent to the self.client.chat.completions.create (line 1742)

completion_kwargs = self._prepare_completion_kwargs(
            messages=messages,
            stop_sequences=stop_sequences,
            response_format=response_format,
            tools_to_call_from=tools_to_call_from,
            model=self.model_id,
            custom_role_conversions=self.custom_role_conversions,
            convert_images_to_image_urls=True,
            **kwargs,
        )
        self._apply_rate_limit()
        response = self.retryer(self.client.chat.completions.create, **completion_kwargs)

I tried replacing the stop_sequences=stop_sequences with stop_sequences=[] and the issue is gone. I am not sure if this causes other issues.

I use this debugging class, and the issue is gone.

class DebuggingOpenAIModel(OpenAIModel):
    def generate(
    self,
    messages: list[ChatMessage | dict],
    stop_sequences: list[str] | None = None,
    response_format: dict[str, str] | None = None,
    tools_to_call_from: list[Tool] | None = None,
    **kwargs,
    ) -> ChatMessage:
        completion_kwargs = self._prepare_completion_kwargs(
            messages=messages,
            stop_sequences=[], # Remove stop sequences
            response_format=response_format,
            tools_to_call_from=tools_to_call_from,
            model=self.model_id,
            custom_role_conversions=self.custom_role_conversions,
            convert_images_to_image_urls=True,
            **kwargs,
        )
        self._apply_rate_limit()
        response = self.retryer(self.client.chat.completions.create, **completion_kwargs)
        print("Raw model response:", response)
        content = response.choices[0].message.content
        if stop_sequences is not None and not self.supports_stop_parameter:
            content = remove_content_after_stop_sequences(content, stop_sequences)
        return ChatMessage(
            role=response.choices[0].message.role,
            content=content,
            tool_calls=response.choices[0].message.tool_calls,
            raw=response,
            token_usage=TokenUsage(
                input_tokens=response.usage.prompt_tokens,
                output_tokens=response.usage.completion_tokens,
            ),
        )

model = DebuggingOpenAIModel(model_id="openai/gpt-oss-120b",
                    api_key="", 
                    api_base="http://localhost:8000/v1",
                    reasoning_effort="low")

agent = CodeAgent(tools=[], 
                  model=model, executor_type='local', 
                  additional_authorized_imports = ["pandas", "numpy.*", "tableone", "scipy", "scipy.*", "sklearn", "sklearn.*", "statsmodels", "statsmodels.*", "matplotlib", "matplotlib.*", "PIL", "PIL.*"],
                  max_steps=10,
                  stream_outputs=False)

task = "hi"

res = agent.run(task, reset=False)

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