Skip to content
Merged
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
27 changes: 27 additions & 0 deletions crates/goose/src/conversation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ pub fn fix_conversation(conversation: Conversation) -> (Conversation, Vec<String
fn fix_messages(messages: Vec<Message>) -> (Vec<Message>, Vec<String>) {
[
merge_text_content_items,
trim_assistant_text_whitespace,
remove_empty_messages,
fix_tool_calling,
merge_consecutive_messages,
Expand Down Expand Up @@ -257,6 +258,32 @@ fn merge_text_content_items(messages: Vec<Message>) -> (Vec<Message>, Vec<String
)
}

fn trim_assistant_text_whitespace(messages: Vec<Message>) -> (Vec<Message>, Vec<String>) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Interesting. I might just add a fn trim which trims both leading and trailing whitespace of all messages. Would be simpler, and in theory doesn't cause any issues. Only makes messages smaller.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Okay, looked into this a bit and tried out some edge cases. We could get into trouble with messages that contain things like ascii art, tables, charts, or other space-dependent content in user message. So, I'm just going to leave this as trimming only the trailing whitespace on assistant messages

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I could limit the trimming to only the very latest assistant message since that appears to be the only one Anthropic cares about per that message. Any thoughts there @alexhancock ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

this stops things from crashing so that's good and agree, leading whitespace you can't trim in general. do we know where the issue comes from though? it would be better if we could fix it at the source.

Copy link
Collaborator Author

@tlongwell-block tlongwell-block Dec 4, 2025

Choose a reason for hiding this comment

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

If the error is to be believed at face value, it's an assistant message causing the issue.

So that rules out tool responses, the moim, and special/control input from the user/client. So my thought is that the LLM itself is generating a trailing whitespace for some reason? They do get weird sometimes once context gets long enough.

tool call seems likely and fits with what I was seeing

let mut issues = Vec::new();

let fixed_messages = messages
.into_iter()
.map(|mut message| {
if message.role == Role::Assistant {
for content in &mut message.content {
if let MessageContent::Text(text) = content {
let trimmed = text.text.trim_end();
if trimmed.len() != text.text.len() {
issues.push(
"Trimmed trailing whitespace from assistant message".to_string(),
);
text.text = trimmed.to_string();
}
}
}
}
message
})
.collect();

(fixed_messages, issues)
}

fn remove_empty_messages(messages: Vec<Message>) -> (Vec<Message>, Vec<String>) {
let mut issues = Vec::new();
let filtered_messages = messages
Expand Down
Loading