Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 9 additions & 2 deletions src/smolagents/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,15 @@ def convert(obj):
return convert(obj)


def remove_content_after_stop_sequences(content: str, stop_sequences: list[str]) -> str:
"""Remove content after any stop sequence is encountered."""
def remove_content_after_stop_sequences(content: str | None, stop_sequences: list[str] | None) -> str | None:
"""Remove content after any stop sequence is encountered.

Some providers may return ``None`` content (for example when responding purely with tool calls),
so we skip processing in that case.
"""
if content is None or not stop_sequences:
return content

for stop_seq in stop_sequences:
split = content.split(stop_seq)
content = split[0]
Expand Down
11 changes: 11 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,17 @@ def test_remove_content_after_stop_sequences():
assert removed_content == "Hello"


def test_remove_content_after_stop_sequences_handles_none():
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please merge these tests into one with several asserts! After that lgtm !

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks a lot for the feedback and the review @aymeric-roucher.
I've merged the tests as suggested.

Happy to make it less verbose or use a different name if you prefer.

# Test with None stop sequence
content = "Hello world!"
removed_content = remove_content_after_stop_sequences(content, None)
assert removed_content == content

# Test with None content
removed_content = remove_content_after_stop_sequences(None, ["<code>"])
assert removed_content is None


@pytest.mark.parametrize(
"convert_images_to_image_urls, expected_clean_message",
[
Expand Down
Loading