agent: Fix an issue with messages containing trailing whitespace (#33643)
Seeing this come up in our server logs when sending requests to Anthropic: `final assistant content cannot end with trailing whitespace`. Release Notes: - agent: Fixed an issue where Anthropic requests would sometimes fail because of malformed assistant messages
This commit is contained in:
parent
ae6237178c
commit
ca0bd53bed
2 changed files with 6 additions and 0 deletions
|
@ -1343,6 +1343,7 @@ impl Thread {
|
||||||
for segment in &message.segments {
|
for segment in &message.segments {
|
||||||
match segment {
|
match segment {
|
||||||
MessageSegment::Text(text) => {
|
MessageSegment::Text(text) => {
|
||||||
|
let text = text.trim_end();
|
||||||
if !text.is_empty() {
|
if !text.is_empty() {
|
||||||
request_message
|
request_message
|
||||||
.content
|
.content
|
||||||
|
|
|
@ -528,6 +528,11 @@ pub fn into_anthropic(
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter_map(|content| match content {
|
.filter_map(|content| match content {
|
||||||
MessageContent::Text(text) => {
|
MessageContent::Text(text) => {
|
||||||
|
let text = if text.chars().last().map_or(false, |c| c.is_whitespace()) {
|
||||||
|
text.trim_end().to_string()
|
||||||
|
} else {
|
||||||
|
text
|
||||||
|
};
|
||||||
if !text.is_empty() {
|
if !text.is_empty() {
|
||||||
Some(anthropic::RequestContent::Text {
|
Some(anthropic::RequestContent::Text {
|
||||||
text,
|
text,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue