mirror of
https://github.com/BerriAI/litellm.git
synced 2026-12-01 01:18:34 +00:00
* feat(rust): one typed decline from core up to each host Rust core says it cannot serve a request with one Decline that travels unchanged through llms and inference up to the host. The Python bridge raises RustBridgeDeclined for it in route_failure before the public exception mapping, so dispatch falls back to legacy Python instead of failing the call, and the gateway answers 501. Every route collapses errors after the send with Error::after_send, which keeps a provider answer and a response fault as they are and nests anything else as the source of an invalid response, so a billed call can never read as a decline. Response-side cases that used to decline are now ErrorDetail::Unhandled response faults. The rust-error skill documents declined() as the first question both boundaries ask, and the Python suites pin that a native decline reaches the dispatcher before any provider IO. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(rust): keep post-provider hook failures terminal --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
163 lines
5.4 KiB
Rust
163 lines
5.4 KiB
Rust
use litellm_host::observation::ObservationSender;
|
|
pub use litellm_inference::Error;
|
|
|
|
pub mod ids;
|
|
pub mod route;
|
|
pub mod types;
|
|
pub mod websocket;
|
|
|
|
mod handler;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use litellm_auth::AuthServices;
|
|
use litellm_core_utils::decline::Decline;
|
|
use litellm_core_utils::get_llm_provider_logic::{
|
|
CustomLlmProvider, LlmProviders, get_custom_llm_provider,
|
|
};
|
|
use litellm_host::interceptors::Interceptors;
|
|
use litellm_host::interceptors::ProviderIdentity;
|
|
use litellm_llms::{
|
|
base_llm::responses::transformation::BaseResponsesApiConfig,
|
|
openai::responses::transformation::OpenAiResponsesApiConfig,
|
|
openrouter::responses::transformation::OPENROUTER_RESPONSES_CONFIG,
|
|
};
|
|
use litellm_secrets::source::SecretSource;
|
|
use types::{ResponsesCall, ResponsesOutput, ResponsesProvider};
|
|
|
|
const NOT_NATIVE: Decline = Decline::new("native HTTP responses provider");
|
|
|
|
fn responses_provider(provider: LlmProviders) -> Option<&'static dyn BaseResponsesApiConfig> {
|
|
match provider {
|
|
LlmProviders::Openai => Some(&OpenAiResponsesApiConfig),
|
|
LlmProviders::Openrouter => Some(&OPENROUTER_RESPONSES_CONFIG),
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub struct ResponsesRoute {
|
|
http: litellm_http::Client,
|
|
auth: Arc<AuthServices>,
|
|
secrets: Arc<dyn SecretSource>,
|
|
cache: Option<litellm_cache_response::ScopedCache>,
|
|
}
|
|
|
|
impl ResponsesRoute {
|
|
pub fn new(
|
|
http: litellm_http::Client,
|
|
auth: Arc<AuthServices>,
|
|
secrets: Arc<dyn SecretSource>,
|
|
) -> Self {
|
|
Self {
|
|
http,
|
|
auth,
|
|
secrets,
|
|
cache: None,
|
|
}
|
|
}
|
|
|
|
pub fn with_cache(self, cache: litellm_cache_response::ScopedCache) -> Self {
|
|
Self {
|
|
cache: Some(cache),
|
|
..self
|
|
}
|
|
}
|
|
|
|
pub async fn execute(
|
|
&self,
|
|
call: ResponsesCall,
|
|
interceptors: &impl Interceptors<Error>,
|
|
options: impl Into<litellm_inference::CallOptions>,
|
|
) -> Result<ResponsesOutput, Error> {
|
|
let litellm_inference::CallOptions {
|
|
cache: cache_options,
|
|
observers,
|
|
} = options.into();
|
|
litellm_host::lifecycle::observe_call(
|
|
observers.clone(),
|
|
self.run(call, cache_options, interceptors, observers.as_ref()),
|
|
)
|
|
.await
|
|
}
|
|
|
|
#[tracing::instrument(name = "litellm.route", skip_all, fields(
|
|
route = "responses",
|
|
model = %call.model,
|
|
provider,
|
|
resolved_model,
|
|
stream = call.optional_params.get("stream").and_then(serde_json::Value::as_bool).unwrap_or(false),
|
|
outcome
|
|
))]
|
|
async fn run(
|
|
&self,
|
|
call: ResponsesCall,
|
|
cache_options: Option<litellm_cache_response::CachePolicy>,
|
|
interceptors: &impl litellm_host::interceptors::Interceptors<Error>,
|
|
observers: Option<&ObservationSender>,
|
|
) -> Result<ResponsesOutput, Error> {
|
|
litellm_inference::diagnostic::call(async {
|
|
let explicit = call
|
|
.custom_llm_provider
|
|
.as_deref()
|
|
.filter(|provider| !provider.is_empty());
|
|
let (provider_name, resolved_model) =
|
|
match get_custom_llm_provider(&call.model, explicit) {
|
|
Some(CustomLlmProvider {
|
|
model,
|
|
custom_llm_provider,
|
|
}) => (custom_llm_provider, model),
|
|
None => ("openai", call.model.as_str()),
|
|
};
|
|
if resolved_model.is_empty() {
|
|
return Err(Error::InvalidProvider(call.model));
|
|
}
|
|
let provider = provider_name
|
|
.parse::<LlmProviders>()
|
|
.map_err(|_| Error::Declined(NOT_NATIVE))?;
|
|
let config = responses_provider(provider).ok_or(Error::Declined(NOT_NATIVE))?;
|
|
let wire_model = match provider {
|
|
LlmProviders::Openai if resolved_model.contains('/') => {
|
|
return Err(Error::InvalidProvider(call.model));
|
|
}
|
|
LlmProviders::Openai => resolved_model.into(),
|
|
LlmProviders::Openrouter if explicit.is_some() && !resolved_model.contains('/') => {
|
|
format!("openrouter/{resolved_model}")
|
|
}
|
|
LlmProviders::Openrouter => resolved_model.into(),
|
|
_ => return Err(Error::Declined(NOT_NATIVE)),
|
|
};
|
|
let provider = ResponsesProvider {
|
|
identity: ProviderIdentity {
|
|
model: wire_model,
|
|
provider: provider_name.into(),
|
|
},
|
|
config,
|
|
};
|
|
litellm_inference::diagnostic::provider(
|
|
&provider.identity.model,
|
|
&provider.identity.provider,
|
|
);
|
|
let secrets = self
|
|
.secrets
|
|
.resolve(
|
|
provider
|
|
.config
|
|
.secret_names(call.api_key.as_deref(), call.api_base.as_deref()),
|
|
)
|
|
.await?;
|
|
let execute: futures_util::future::BoxFuture<'_, Result<ResponsesOutput, Error>> =
|
|
Box::pin(handler::execute(
|
|
self,
|
|
provider,
|
|
call,
|
|
secrets,
|
|
cache_options,
|
|
interceptors,
|
|
observers,
|
|
));
|
|
execute.await
|
|
})
|
|
.await
|
|
}
|
|
}
|