Fix/prune empty assistant messages #1907
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Prevent
ActionStep.to_messages()from emitting empty assistant messages whenmodel_outputis empty or whitespace-only.Motivation / Problem
In tool-calling flows, models may return tool calls without any assistant text (tool-call-only responses). In these cases
ActionStep.model_outputcan be""or whitespace. The previous implementation always appended an assistantChatMessagebuilt frommodel_output.strip(), which could result in an assistant message with empty text content.This causes:
grok-code-fast-1), consecutive empty assistant messages were correlated with a reproducible provider-side token counting failure (“maximum prompt length” / bogus token count), despite otherwise reasonable payload sizes.Reproduction
ActionStep.model_outputmay be""or whitespace.ActionStep.to_messages()appended an assistant message usingmodel_output.strip().strip()produced an empty string, an empty assistant message was added to the conversation history.grok-code-fast-1setup, these empty assistant messages were sufficient to reliably trigger a provider token-counting failure.Change
In
src/smolagents/memory.py,ActionStep.to_messages()now:text = self.model_output.strip()ChatMessagewhentextis non-emptyThis preserves behavior for normal non-empty outputs while preventing informationless assistant messages.
Tests
tests/test_memory.py::test_action_step_to_messages_ignores_blank_model_outputmodel_outputdoes not produce an assistant message.Validation / Test Plan
make test) and quality checks (make quality).grok-code-1that the previously reproducible token computation error no longer occurs after this change.