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
7 changes: 6 additions & 1 deletion crates/braintrust-llm-router/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ mod tests {

// Client errors
assert!(TransformError::UnableToDetectFormat.is_client_error());
assert!(TransformError::UnableToDetectRequestFormat.is_client_error());
assert!(TransformError::UnableToDetectResponseFormat.is_client_error());
assert!(TransformError::UnableToDetectStreamFormat.is_client_error());
assert!(TransformError::ValidationFailed {
target: ProviderFormat::ChatCompletions,
reason: "test".into()
Expand Down Expand Up @@ -233,7 +236,9 @@ mod tests {
assert!(Error::UnknownModel("gpt-5".into()).is_client_error());
assert!(Error::NoProvider(ProviderFormat::ChatCompletions).is_client_error());
assert!(Error::InvalidRequest("bad".into()).is_client_error());
assert!(Error::Lingua(lingua::TransformError::UnableToDetectFormat).is_client_error());
assert!(
Error::Lingua(lingua::TransformError::UnableToDetectRequestFormat).is_client_error()
);

// Auth errors
assert!(Error::NoAuth("openai".into()).is_auth_error());
Expand Down
2 changes: 1 addition & 1 deletion crates/braintrust-llm-router/src/providers/bedrock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ where
.find(|adapter| adapter.detect_request(&payload))
{
Some(adapter) => adapter,
None => return Err(TransformError::UnableToDetectFormat.into()),
None => return Err(TransformError::UnableToDetectRequestFormat.into()),
};

if source_adapter.format() == format {
Expand Down
2 changes: 1 addition & 1 deletion crates/braintrust-llm-router/src/streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl Stream for SessionTransformStream {
}
continue;
}
Err(lingua::TransformError::UnableToDetectFormat) => {
Err(lingua::TransformError::UnableToDetectStreamFormat) => {
#[cfg(feature = "tracing")]
log_stream_transform_detection_failure(
&data,
Expand Down
2 changes: 1 addition & 1 deletion crates/lingua/src/processing/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,7 @@ mod tests {

assert!(matches!(
session.push(full_response),
Err(TransformError::UnableToDetectFormat)
Err(TransformError::UnableToDetectStreamFormat)
));
}

Expand Down
42 changes: 41 additions & 1 deletion crates/lingua/src/processing/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ pub enum TransformError {
#[error("Unable to detect source format")]
UnableToDetectFormat,

#[error("Unable to detect request source format")]
UnableToDetectRequestFormat,

#[error("Unable to detect response source format")]
UnableToDetectResponseFormat,

#[error("Unable to detect stream source format")]
UnableToDetectStreamFormat,

#[error("Validation failed for target format {target:?}: {reason}")]
ValidationFailed {
target: ProviderFormat,
Expand Down Expand Up @@ -78,6 +87,9 @@ impl TransformError {
matches!(
self,
TransformError::UnableToDetectFormat
| TransformError::UnableToDetectRequestFormat
| TransformError::UnableToDetectResponseFormat
| TransformError::UnableToDetectStreamFormat
| TransformError::ValidationFailed { .. }
| TransformError::DeserializationFailed(_)
| TransformError::UnsupportedTargetFormat(_)
Expand Down Expand Up @@ -603,7 +615,15 @@ fn detect_adapter_with_kind(
}
}

Err(TransformError::UnableToDetectFormat)
Err(unable_to_detect_format_error(kind))
}

fn unable_to_detect_format_error(kind: DetectKind) -> TransformError {
match kind {
DetectKind::Request => TransformError::UnableToDetectRequestFormat,
DetectKind::Response => TransformError::UnableToDetectResponseFormat,
DetectKind::Stream => TransformError::UnableToDetectStreamFormat,
}
}

fn detect_adapter_exact(payload: &Value, kind: DetectKind) -> Option<&'static dyn ProviderAdapter> {
Expand Down Expand Up @@ -942,6 +962,16 @@ mod tests {
));
}

#[test]
fn test_transform_request_unable_to_detect_mentions_request() {
let input = Bytes::from_static(br#"{"not":"a supported request shape"}"#);

let err = transform_request(input, ProviderFormat::ChatCompletions, None).unwrap_err();

assert!(matches!(err, TransformError::UnableToDetectRequestFormat));
assert_eq!(err.to_string(), "Unable to detect request source format");
}

#[test]
#[cfg(all(feature = "openai", feature = "anthropic"))]
fn test_transform_response() {
Expand Down Expand Up @@ -973,6 +1003,16 @@ mod tests {
assert!(output.get("content").is_some() || output.get("choices").is_some());
}

#[test]
fn test_transform_response_unable_to_detect_mentions_response() {
let input = Bytes::from_static(br#"{"not":"a supported response shape"}"#);

let err = transform_response(input, ProviderFormat::ChatCompletions).unwrap_err();

assert!(matches!(err, TransformError::UnableToDetectResponseFormat));
assert_eq!(err.to_string(), "Unable to detect response source format");
}

#[test]
#[cfg(all(feature = "openai", feature = "anthropic"))]
fn test_stream_detection_falls_back_to_full_response() {
Expand Down
Loading