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
5 changes: 5 additions & 0 deletions crates/libsy-llm-client/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const OPENAI_OVERFLOW_PHRASES: &[&str] = &[
"context window",
"context length is only",
"please reduce the length of the input",
"exceeds the maximum allowed input length",
];

// Anthropic has no structured `error.code`, so detection is phrase-based only.
Expand Down Expand Up @@ -310,6 +311,10 @@ mod tests {
r#"{"error":{"message":"the model's context length is only 131072 tokens"}}"#
));
assert!(!backend.is_context_overflow(r#"{"error":{"code":"invalid_api_key"}}"#));
// Hub GLM (LiteLLM-wrapped): code is "400", detection relies on phrase match.
assert!(backend.is_context_overflow(
r#"{"error":{"message":"Input length 877338 exceeds the maximum allowed input length of 639968 tokens","code":"400"}}"#
));
}

#[test]
Expand Down
9 changes: 9 additions & 0 deletions crates/switchyard-components/src/backends/openai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ const OPENAI_OVERFLOW_PHRASES: &[&str] = &[
"context window",
"context length is only",
"please reduce the length of the input",
"exceeds the maximum allowed input length",
];

fn is_context_overflow(body: &str) -> bool {
Expand Down Expand Up @@ -759,6 +760,14 @@ mod tests {
assert!(!is_context_overflow(body));
}

#[test]
fn context_overflow_hub_glm_matches() {
// Hub GLM error via LiteLLM: code is "400" (not context_length_exceeded),
// so detection relies on phrase matching.
let body = r#"{"error":{"message":"Input length 877338 exceeds the maximum allowed input length of 639968 tokens","code":"400"}}"#;
assert!(is_context_overflow(body));
}

#[test]
fn context_overflow_nvidia_litellm_wrap_matches() {
// Body shape observed from inference-api.nvidia.com's LiteLLM proxy
Expand Down
Loading