Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions docs/source/en/tutorials/building_good_agents.md
Original file line number Diff line number Diff line change
Expand Up @@ -422,3 +422,16 @@ result = agent.run(
"How long would a cheetah at full speed take to run the length of Pont Alexandre III?",
)
```

### 5. Increase maximum output tokens

If you see warnings indicating that the Code or JSON data is missing in the output, It might be because the output tokens configured for your model might be too less. The model is not generating a code snippet or a tool call that can be parsed by the framework. Please try increasing the model output token configuration (`num_ctx`, `max_tokens` etc)

For example in a CodeAgent's logs you should see
```
Warning: The model's output is missing a Code output. This could be due to an insufficient number of output tokens set in the model configuration.

and for a ToolCallingAgent
```
Warning: The model's output is missing JSON data. This could be due to an insufficient number of output tokens set in the model configuration
```
8 changes: 8 additions & 0 deletions src/smolagents/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -1302,6 +1302,10 @@ def _step_stream(
try:
chat_message = self.model.parse_tool_calls(chat_message)
except Exception as e:
if isinstance(e, ValueError) and "The model output does not contain any JSON blob" in str(e):
self.logger.log_warning(
"The model's output is missing JSON data. This could be due to an insufficient number of output tokens set in the model configuration."
)
raise AgentParsingError(f"Error while parsing tool call from model output: {e}", self.logger)
else:
for tool_call in chat_message.tool_calls:
Expand Down Expand Up @@ -1682,6 +1686,10 @@ def _step_stream(
code_action = fix_final_answer_code(code_action)
memory_step.code_action = code_action
except Exception as e:
if isinstance(e, ValueError) and "Make sure to include code with the correct pattern" in str(e):
self.logger.log_warning(
"The model's output is missing a Code output. This could be due to an insufficient number of output tokens set in the model configuration."
)
error_msg = f"Error in code parsing:\n{e}\nMake sure to provide correct code blobs."
raise AgentParsingError(error_msg, self.logger)

Expand Down
3 changes: 3 additions & 0 deletions src/smolagents/monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ def log_code(self, title: str, content: str, level: int = LogLevel.INFO) -> None
level=level,
)

def log_warning(self, title: str, level: int = LogLevel.INFO) -> None:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warnings will look like the following
500122585-7798d17e-ed11-482a-81bb-6501ff2ea8ac

self.log("Warning: " + title, style=YELLOW_HEX, level=LogLevel.INFO)

def log_rule(self, title: str, level: int = LogLevel.INFO) -> None:
self.log(
Rule(
Expand Down
25 changes: 25 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,18 @@ def test_multiple_code_blobs(self):
result = parse_code_blobs(test_input, ("<code>", "</code>"))
assert result == "Foo\n\ncode_a\n\ncode_b"

@pytest.mark.parametrize(
"raw_text",
[
"This is just some conversational text.",
"Here is an invalid code snippet `x = 10",
],
)
def test_parse_code_blobs_without_valid_code(raw_text):
Copy link
Contributor Author

@suryabdev suryabdev Oct 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some simple tests to check the exact string. If the error message is changed in parse_code_blobs or parse_json_blob the note in the failing test will serve as a reminder to change the logic in agents.py

# Note: If the exact string is changed a parsing warning needs to be modified in agents.py
with pytest.raises(ValueError, match="Make sure to include code with the correct pattern"):
parse_code_blobs(raw_text, ("<code>", "</code>"))


@pytest.fixture(scope="function")
def ipython_shell():
Expand Down Expand Up @@ -485,6 +497,19 @@ def test_parse_json_blob_with_invalid_json(raw_json):
with pytest.raises(Exception):
parse_json_blob(raw_json)

@pytest.mark.parametrize(
"raw_json",
[
"this string has no json blob",
"this string has an opening brace { but no closing one",
"",
"some text {",
],
)
def test_parse_json_blob_without_json_blob(raw_json):
# Note: If the exact string is changed a parsing warning needs to be modified in agents.py
with pytest.raises(ValueError, match="The model output does not contain any JSON blob."):
parse_json_blob(raw_json)

@pytest.mark.parametrize(
"name,expected",
Expand Down