litellm/litellm-rust/crates/inference-ocr/src/handler.rs
Yujong Lee f55f452726 refactor(rust): carry provider credentials as typed ConnectionArguments
Provider credentials and endpoint settings rode inside optional_params, and
every route treated them differently: chat dropped aws_* in the projection
while Bedrock Converse still read them from params, OCR re-admitted them
through its own allowlist, and transcription never filtered them. Each
provider then looked them up by string.

Every route request now carries a ConnectionArguments beside its params,
built only by keeping the connection names out of a call's arguments, and
the AWS, Azure and Vertex parsers read only from it. One row per setting in
auth-types/src/fields.rs names the kwargs, the env vars that back them and
whether the value is secret; is_connection_name and each auth crate's
SECRET_NAMES derive from the rows, and parsers read both the kwarg and its
env fallback through the same row.

secret_fields on chat, Messages and Responses now lists the call's secret
connection names, so caller credentials are redacted in logged kwargs.
Bedrock Messages honors per-call AWS credentials and the runtime endpoint.
Both chat CONFIG_PARAMS lists, OCR's per-provider auth lists and the second
AWS env fallback are gone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 09:21:17 -07:00

87 lines
2.9 KiB
Rust

use futures_util::future::BoxFuture;
use litellm_host::interceptors::{Interceptors, RawResponse, RequestContext, WireRequest};
use litellm_host::{lifecycle::ExecutionEvent, observation::ObservationSender};
use litellm_llms::base_llm::ocr::{
error::Error,
handler::{CallHooks, OcrClient},
transformation::PreparedOcrRequest,
};
use litellm_llms_types::formats::ocr::LiteLLMOcrResponse;
use serde_json::Value;
use super::{prepare::prepare_request, provider_config::OcrConfigKind};
use crate::types::ResolvedOcrRequest;
pub(crate) async fn perform_ocr_request(
client: &OcrClient,
request: ResolvedOcrRequest,
host: &impl Interceptors<Error>,
caller_document: bool,
observers: Option<&ObservationSender>,
) -> Result<LiteLLMOcrResponse, Error> {
request.response_format()?;
let config = request.config;
let secrets = client
.secret_source()
.resolve(&config.secret_names())
.await
.map_err(|error| Error::Secret(std::sync::Arc::new(error)))?;
let request = prepare_request(request, caller_document, client, secrets);
let interceptors = OcrCallHooks::new(host, &request, config, observers);
config.ocr(client, &request, &interceptors).await
}
struct OcrCallHooks<'a, H> {
interceptors: &'a H,
context: RequestContext,
observers: Option<&'a ObservationSender>,
}
impl<'a, H> OcrCallHooks<'a, H> {
fn new(
interceptors: &'a H,
request: &PreparedOcrRequest,
config: OcrConfigKind,
observers: Option<&'a ObservationSender>,
) -> Self {
Self {
interceptors,
observers,
context: RequestContext {
model: request.model.clone(),
custom_llm_provider: <&str>::from(config.provider()).to_owned(),
optional_params: Value::Object(request.optional_params.clone().into()),
secret_fields: request
.connection_arguments
.secret_names()
.map(str::to_string)
.collect(),
api_key: request.connection.api_key.clone(),
},
}
}
}
impl<H: Interceptors<Error>> CallHooks<Error> for OcrCallHooks<'_, H> {
fn before_provider_request(
&self,
wire: WireRequest,
) -> BoxFuture<'_, Result<WireRequest, Error>> {
Box::pin(
self.interceptors
.before_provider_request(wire, self.context.clone()),
)
}
fn response_received<'a>(&'a self, body: &'a [u8]) -> BoxFuture<'a, Result<(), Error>> {
let raw = RawResponse {
body: String::from_utf8_lossy(body).into_owned(),
};
if let Some(observers) = self.observers {
observers.emit(litellm_host::lifecycle::CallEvent::Execution(
ExecutionEvent::ProviderResponseReceived { raw: raw.clone() },
));
}
Box::pin(self.interceptors.after_provider_response(raw))
}
}