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
30 changes: 29 additions & 1 deletion crates/switchyard-server/src/sse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,18 @@ fn error_event(target_format: WireFormat, message: String, redactor: &Redactor)
),
),
WireFormat::AnthropicMessages | WireFormat::OpenAiResponses => {
let error_type = if target_format == WireFormat::AnthropicMessages {
"api_error"
} else {
"SwitchyardError"
};
Comment thread
grahamking marked this conversation as resolved.
Event::default().event("error").data(
redactor.json(
json!({
"type": "error",
"error": {
"message": message,
"type": "SwitchyardError",
"type": error_type,
}
})
.to_string(),
Expand Down Expand Up @@ -181,4 +186,27 @@ mod tests {
assert!(!body.contains("[DONE]"));
Ok(())
}

#[tokio::test]
async fn anthropic_stream_error_uses_api_error_type() -> TestResult {
let failure = LlmClientError::General("boom".to_string());
let stream: RawEventStream =
Box::pin(stream::iter(vec![Err(LlmStreamError::Client(failure))]));
let response = frame_stream(
stream,
WireFormat::AnthropicMessages,
Arc::new(Redactor::default()),
)
.into_response();
let body = String::from_utf8(to_bytes(response.into_body(), usize::MAX).await?.to_vec())?;
let data = body
.lines()
.find_map(|line| line.strip_prefix("data: "))
.ok_or("missing data line")?;
assert_eq!(
serde_json::from_str::<Value>(data)?,
json!({"type": "error", "error": {"type": "api_error", "message": "boom"}})
);
Ok(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ fn encode_anthropic_stream(
// An in-band error is terminal: emit the error, then nothing further.
state.finished = true; // finish() adds no success events
state.errored = true; // the entry guard drops any later chunk
vec![json!({"type": "error", "error": {"message": message}})]
vec![json!({"type": "error", "error": {"type": "api_error", "message": message}})]
}
}
}
Expand Down
39 changes: 39 additions & 0 deletions crates/switchyard-translation/tests/stream_translation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,45 @@ fn preserved_same_format_replay_stops_after_an_error() -> TestResult {
Ok(())
}

#[test]
fn anthropic_errors_include_type_and_terminate_stream() -> TestResult {
let engine = TranslationEngine::default();
let target = WireFormat::AnthropicMessages;
let message = "stream failed";
for chunk in [
LlmResponseChunk::StreamError {
message: message.into(),
},
LlmResponseChunk::DecodeError {
message: message.into(),
},
] {
let mut state = StreamTranslationState::new(WireFormat::OpenAiChat, target);
let events = engine.encode_stream_event(
&mut state,
target,
LlmResponseStreamEvent::new(vec![chunk]),
)?;
assert_eq!(
events,
vec![json!({"type": "error", "error": {"type": "api_error", "message": message}})]
);
assert!(
engine
.encode_stream_event(
&mut state,
target,
LlmResponseStreamEvent::new(vec![LlmResponseChunk::MessageStop {
reason: None
}]),
)?
.is_empty()
);
assert!(engine.finish_stream(&mut state, target)?.is_empty());
}
Ok(())
}

#[test]
fn same_format_decode_failure_emits_terminal_error() -> TestResult {
let cases = [
Expand Down
Loading