Skip to content

Switchyard lets LLM applications route traffic across models and providers while preserving native OpenAI and Anthropic API compatibility - enabling flexible model selection, benchmarking, and cost/performance optimization.

Rust

fix(llm-client): preserve queries in provider endpoint URLs (#757)

Graham Kinge16bcdd0e31d
diff --git a/crates/libsy-llm-client/src/backend.rs b/crates/libsy-llm-client/src/backend.rs
index 2d8852ac1..da3b9aa25 100644
--- a/crates/libsy-llm-client/src/backend.rs
+++ b/crates/libsy-llm-client/src/backend.rs
@@ -148,12 +148,16 @@ impl Backend {
/// Tolerates base URLs that already include the provider path (or a bare
/// `/v1`), matching the join rules of the existing native backends.
pub fn url(&self) -> String {
- let base_url = self.config().base_url.trim_end_matches('/');
- match self {
+ let base_url = &self.config().base_url;
+ let result = match self {
Backend::OpenAiChat(_) => openai_url(base_url, "/chat/completions"),
Backend::OpenAiResponses(_) => openai_url(base_url, "/responses"),
- Backend::Anthropic(_) => anthropic_url(base_url),
- }
+ Backend::Anthropic(_) => anthropic_url(base_url, ""),
+ };
+ result.unwrap_or_else(|error| {
+ tracing::error!(%error, "Unable to build provider endpoint URL");
+ base_url.clone()
+ })
}
/// Applies this backend's configured auth and version headers to a request builder.
@@ -275,8 +279,11 @@ impl Backend {
/// The upstream `/v1/messages/count_tokens` URL, derived from the same base
/// URL join as [`url`](Self::url).
pub fn count_tokens_url(&self) -> String {
- let base_url = self.config().base_url.trim_end_matches('/');
- format!("{}/count_tokens", anthropic_url(base_url))
+ let base_url = &self.config().base_url;
+ anthropic_url(base_url, "/count_tokens").unwrap_or_else(|error| {
+ tracing::error!(%error, "Unable to build Anthropic token-counting URL");
+ base_url.clone()
+ })
}
/// Whether an upstream 400 `body` looks like a context-window overflow for
@@ -326,23 +333,34 @@ fn oauth_beta_header(value: &HeaderValue) -> Option<HeaderValue> {
}
// Accept either a root `/v1` URL or an already-specific OpenAI endpoint URL.
-fn openai_url(base_url: &str, suffix: &str) -> String {
- let base_root = base_url
+pub(crate) fn openai_url(base_url: &str, suffix: &str) -> Result<String> {
+ let mut url = reqwest::Url::parse(base_url).map_err(|error| LlmClientError::Configuration {
+ message: format!("Invalid OpenAI base URL: {error}"),
+ })?;
+ let base_path = url.path().trim_end_matches('/');
+ let base_root = base_path
.strip_suffix("/chat/completions")
- .or_else(|| base_url.strip_suffix("/responses"))
- .unwrap_or(base_url);
- format!("{base_root}{suffix}")
+ .or_else(|| base_path.strip_suffix("/responses"))
+ .unwrap_or(base_path);
+ url.set_path(&format!("{base_root}{suffix}"));
+ Ok(url.into())
}
// Accept a bare host, a `/v1` root, or an already-specific `/v1/messages` URL.
-fn anthropic_url(base_url: &str) -> String {
- if base_url.ends_with("/v1/messages") {
- base_url.to_string()
- } else if base_url.ends_with("/v1") {
- format!("{base_url}/messages")
+fn anthropic_url(base_url: &str, suffix: &str) -> Result<String> {
+ let mut url = reqwest::Url::parse(base_url).map_err(|error| LlmClientError::Configuration {
+ message: format!("Invalid Anthropic base URL: {error}"),
+ })?;
+ let base_path = url.path().trim_end_matches('/');
+ let messages_path = if base_path.ends_with("/v1/messages") {
+ base_path.to_string()
+ } else if base_path.ends_with("/v1") {
+ format!("{base_path}/messages")
} else {
- format!("{base_url}/v1/messages")
- }
+ format!("{base_path}/v1/messages")
+ };
+ url.set_path(&format!("{messages_path}{suffix}"));
+ Ok(url.into())
}
#[cfg(test)]
diff --git a/crates/libsy-llm-client/src/client.rs b/crates/libsy-llm-client/src/client.rs
index 8b13dfd30..948838f48 100644
--- a/crates/libsy-llm-client/src/client.rs
+++ b/crates/libsy-llm-client/src/client.rs
@@ -24,7 +24,7 @@ use switchyard_translation::{
};
use tracing::Instrument;
-use crate::backend::Backend;
+use crate::backend::{Backend, openai_url};
use crate::error::{LlmClientError, Result};
use crate::metrics;
use crate::raw::RawResponse;
@@ -101,11 +101,16 @@ impl AuxiliaryOperation {
}
fn url(self, backend: &Backend) -> String {
- match self {
- Self::AnthropicCountTokens => backend.count_tokens_url(),
- Self::ResponsesInputTokens => format!("{}/input_tokens", backend.url()),
- Self::ResponsesCompact => format!("{}/compact", backend.url()),
- }
+ let suffix = match self {
+ Self::AnthropicCountTokens => return backend.count_tokens_url(),
+ Self::ResponsesInputTokens => "/responses/input_tokens",
+ Self::ResponsesCompact => "/responses/compact",
+ };
+ let base_url = backend.url();
+ openai_url(&base_url, suffix).unwrap_or_else(|error| {
+ tracing::error!(%error, "Unable to build OpenAI auxiliary endpoint URL");
+ base_url
+ })
}
}
@@ -1295,6 +1300,67 @@ mod tests {
}
}
+ #[test]
+ fn provider_endpoint_urls_preserve_query_and_fragment() {
+ let invalid_url = "not a URL/";
+ let backend = Backend::OpenAiResponses(config(invalid_url));
+ assert!(openai_url(invalid_url, "/responses").is_err());
+ assert_eq!(backend.url(), invalid_url);
+ assert_eq!(
+ AuxiliaryOperation::ResponsesCompact.url(&backend),
+ invalid_url
+ );
+
+ let anthropic = Backend::Anthropic(config(invalid_url));
+ assert_eq!(anthropic.url(), invalid_url);
+ assert_eq!(anthropic.count_tokens_url(), invalid_url);
+
+ for path in ["", "/v1/", "/v1/messages"] {
+ for tail in [
+ "",
+ "?api-version=2026-09-01&value=%2F/",
+ "#fragment?value=/",
+ ] {
+ let base_url = format!("https://example.com{path}{tail}");
+ let backend = Backend::Anthropic(config(&base_url));
+ assert_eq!(
+ backend.url(),
+ format!("https://example.com/v1/messages{tail}")
+ );
+ assert_eq!(
+ AuxiliaryOperation::AnthropicCountTokens.url(&backend),
+ format!("https://example.com/v1/messages/count_tokens{tail}")
+ );
+ }
+ }
+
+ for path in ["/v1", "/v1/responses/", "/v1/chat/completions"] {
+ for tail in [
+ "",
+ "?api-version=2026-09-01&value=%2F/",
+ "#fragment?value=/",
+ ] {
+ let base_url = format!("https://example.com{path}{tail}");
+ let responses = Backend::OpenAiResponses(config(&base_url));
+ let chat = Backend::OpenAiChat(config(&base_url));
+ for (actual, endpoint) in [
+ (responses.url(), "/responses"),
+ (chat.url(), "/chat/completions"),
+ (
+ AuxiliaryOperation::ResponsesCompact.url(&responses),
+ "/responses/compact",
+ ),
+ (
+ AuxiliaryOperation::ResponsesInputTokens.url(&responses),
+ "/responses/input_tokens",
+ ),
+ ] {
+ assert_eq!(actual, format!("https://example.com/v1{endpoint}{tail}"));
+ }
+ }
+ }
+ }
+
fn config_with_retries(base_url: &str, max_retries: u32) -> HttpBackendConfig {
HttpBackendConfig {
max_retries,