Skip to content
Open
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
36 changes: 36 additions & 0 deletions crates/switchyard-translation/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ where
source: Some(source_format.clone()),
..StreamTranslationState::default()
};
let mut saw_anthropic_message_stop = false;
let mut frame = String::new();
let stream = Box::pin(try_stream! {
futures::pin_mut!(lines);
Expand All @@ -210,6 +211,8 @@ where
sse::SseFrame::Empty => {}
sse::SseFrame::Done => break,
sse::SseFrame::Data(value) => {
saw_anthropic_message_stop |= source == WireFormat::AnthropicMessages
&& value.get("type").and_then(Value::as_str) == Some("message_stop");
let normalized = codec.decode_event(&mut state, &value);
yield LlmResponseStreamEvent::preserved(
source_format.clone(),
Expand All @@ -231,10 +234,18 @@ where
let parsed = sse::parse_json_sse_frame(&frame, marker)
.map_err(|error| LlmClientError::ResponseTranslation(error.to_string()))?;
if let sse::SseFrame::Data(value) = parsed {
saw_anthropic_message_stop |= source == WireFormat::AnthropicMessages
&& value.get("type").and_then(Value::as_str) == Some("message_stop");
let normalized = codec.decode_event(&mut state, &value);
yield LlmResponseStreamEvent::preserved(source_format, value, normalized);
}
}

if source == WireFormat::AnthropicMessages && !saw_anthropic_message_stop {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could we treat Anthropic error events as terminal too? Otherwise EOF emits a second misleading ended before message_stop error after the upstream error.

Err(LlmClientError::ResponseTranslation(
"anthropic stream ended before message_stop".to_string(),
))?;
}
});
Ok(stream)
}
Expand Down Expand Up @@ -570,6 +581,31 @@ mod tests {
Ok(())
}

#[test]
fn anthropic_stream_without_message_stop_is_an_error() -> Result<(), BoxError> {
let sse = b"event: message_start\ndata: {\"type\":\"message_start\",\"message\":{\"id\":\"msg_1\",\"model\":\"claude\"}}\n\n".to_vec();
let bytes = stream::once(async move { Ok::<Vec<u8>, LlmClientError>(sse) });
let decoded = decode_stream(bytes, WireFormat::AnthropicMessages)?;
let results = block_on(
encode_stream(decoded, WireFormat::AnthropicMessages, None)?.collect::<Vec<_>>(),
);

let Some(Err(error)) = results.last() else {
panic!("expected incomplete Anthropic stream to fail");
};
assert_eq!(
error.to_string(),
"response translation failed: anthropic stream ended before message_stop"
);
assert!(
results
.iter()
.filter_map(|result| result.as_ref().ok())
.all(|event| event.get("type").and_then(Value::as_str) != Some("message_stop"))
);
Ok(())
}

#[test]
fn decode_stream_decodes_crlf_delimited_frames() -> Result<(), BoxError> {
// CRLF framing: blank lines are `\r\n\r\n` and the bare `\r` must not
Expand Down
Loading