refactor(ocr): move Vertex implementation to stacked branch

This commit is contained in:
Yujong Lee 2026-09-09 15:47:56 -07:00
parent 9e03b5322f
commit e33f7fdb88
14 changed files with 433 additions and 530 deletions

View file

@ -60,5 +60,3 @@ pub(crate) const AZURE_AI_OCR_PATH: &str = "/providers/mistral/azure/ocr";
pub(crate) const MISTRAL_OCR_API_BASE: &str = "https://api.mistral.ai/v1";
pub(crate) const REDUCTO_OCR_API_BASE: &str = "https://platform.reducto.ai";
pub(crate) const REDUCTO_ID_PREFIX: &str = "reducto://";
pub(crate) const VERTEX_OCR_DEFAULT_LOCATION: &str = "us-central1";
pub(crate) const VERTEX_DEEPSEEK_API_BASE: &str = "https://aiplatform.googleapis.com";

View file

@ -12,8 +12,6 @@ use crate::providers::mistral::ocr::transformation::MistralOcrConfig;
use crate::providers::reducto::ocr::transformation::{
ReductoParseLegacyConfig, ReductoParseV3Config,
};
use crate::providers::vertex_ai::ocr::deepseek::transformation::VertexAiDeepSeekOcrConfig;
use crate::providers::vertex_ai::ocr::transformation::VertexAiOcrConfig;
struct OcrExecution<'a> {
http_client: &'a reqwest::Client,
@ -60,12 +58,6 @@ pub(crate) async fn perform_ocr_request(
OcrProviderRequest::AzureDocumentIntelligence(params) => {
dispatch!(AzureDocumentIntelligenceOcrConfig, params)
}
OcrProviderRequest::VertexAi(params) => {
dispatch!(VertexAiOcrConfig, params)
}
OcrProviderRequest::VertexAiDeepSeek(params) => {
dispatch!(VertexAiDeepSeekOcrConfig, params)
}
OcrProviderRequest::ReductoV3(params) => {
dispatch!(ReductoParseV3Config, params)
}

View file

@ -4,7 +4,6 @@ use crate::Error;
use crate::providers::azure_ai::ocr::document_intelligence::types::DocumentIntelligenceInputParams;
use crate::providers::mistral::ocr::types::MistralOcrParams;
use crate::providers::reducto::ocr::types::{ReductoLegacyParams, ReductoV3Params};
use crate::providers::vertex_ai::ocr::deepseek::types::DeepSeekOcrParams;
use serde::Serialize;
use strum::EnumString;
@ -14,8 +13,6 @@ pub enum OcrProviderRequest {
Mistral(MistralOcrParams),
AzureAi(MistralOcrParams),
AzureDocumentIntelligence(DocumentIntelligenceInputParams),
VertexAi(MistralOcrParams),
VertexAiDeepSeek(DeepSeekOcrParams),
ReductoV3(ReductoV3Params),
ReductoLegacy(ReductoLegacyParams),
}
@ -25,8 +22,6 @@ pub enum OcrProviderKind {
Mistral,
AzureAi,
AzureDocumentIntelligence,
VertexAi,
VertexAiDeepSeek,
ReductoV3,
ReductoLegacy,
}
@ -36,7 +31,6 @@ pub enum OcrProviderKind {
pub enum OcrProvider {
Mistral,
AzureAi,
VertexAi,
Reducto,
}
@ -65,7 +59,6 @@ impl OcrProviderKind {
match self {
Self::Mistral => "mistral",
Self::AzureAi | Self::AzureDocumentIntelligence => "azure_ai",
Self::VertexAi | Self::VertexAiDeepSeek => "vertex_ai",
Self::ReductoV3 | Self::ReductoLegacy => "reducto",
}
}
@ -76,8 +69,6 @@ impl OcrProviderRequest {
Self::Mistral(_) => OcrProviderKind::Mistral,
Self::AzureAi(_) => OcrProviderKind::AzureAi,
Self::AzureDocumentIntelligence(_) => OcrProviderKind::AzureDocumentIntelligence,
Self::VertexAi(_) => OcrProviderKind::VertexAi,
Self::VertexAiDeepSeek(_) => OcrProviderKind::VertexAiDeepSeek,
Self::ReductoV3(_) => OcrProviderKind::ReductoV3,
Self::ReductoLegacy(_) => OcrProviderKind::ReductoLegacy,
}
@ -95,10 +86,6 @@ pub fn ocr_provider_config(provider: OcrProvider, model: &OcrModel) -> OcrProvid
OcrProviderKind::AzureDocumentIntelligence
}
(OcrProvider::AzureAi, _) => OcrProviderKind::AzureAi,
(OcrProvider::VertexAi, _) if lower.contains("deepseek") => {
OcrProviderKind::VertexAiDeepSeek
}
(OcrProvider::VertexAi, _) => OcrProviderKind::VertexAi,
(OcrProvider::Reducto, OcrModel::ReductoLegacy) => OcrProviderKind::ReductoLegacy,
(OcrProvider::Reducto, _) => OcrProviderKind::ReductoV3,
}

View file

@ -143,19 +143,12 @@ impl OcrResponseData {
}
}
#[derive(Clone, Default)]
pub struct VertexOcrSettings {
pub project: Option<String>,
pub location: Option<String>,
}
#[derive(Clone)]
pub struct OcrConnection {
pub api_key: Option<String>,
pub api_base: Option<String>,
pub extra_headers: Vec<(String, String)>,
pub azure_auth: Option<AzureAuthInputs>,
pub vertex: VertexOcrSettings,
pub timeout: Duration,
pub poll_timeout: Duration,
pub max_download_bytes: u64,
@ -168,7 +161,6 @@ impl Default for OcrConnection {
api_base: None,
extra_headers: Vec::new(),
azure_auth: None,
vertex: VertexOcrSettings::default(),
timeout: Duration::from_secs(OCR_HTTP_TIMEOUT_SECS),
poll_timeout: Duration::from_secs(crate::constants::OCR_POLL_TIMEOUT_SECS),
max_download_bytes: OCR_DOWNLOAD_MAX_BYTES,

View file

@ -8,7 +8,7 @@ use super::hooks::{OcrDuringCallRequest, OcrPreCallRequest};
use super::prepare::{
OcrModel, OcrProvider, OcrProviderKind, OcrProviderRequest, ocr_provider_config,
};
use super::types::{OcrConnection, OcrDocument, OcrRequest, OcrRequestFormat, VertexOcrSettings};
use super::types::{OcrConnection, OcrDocument, OcrRequest, OcrRequestFormat};
use crate::Error;
use crate::auth::azure::AzureAuthInputs;
use crate::routing_utils::provider::{CustomLlmProvider, get_custom_llm_provider};
@ -64,7 +64,6 @@ pub fn decode_request_with_env(
OcrProviderKind::Mistral => "MISTRAL_API_KEY",
OcrProviderKind::AzureAi => "AZURE_AI_API_KEY",
OcrProviderKind::AzureDocumentIntelligence => "AZURE_DOCUMENT_INTELLIGENCE_API_KEY",
OcrProviderKind::VertexAi | OcrProviderKind::VertexAiDeepSeek => "VERTEX_AI_API_KEY",
OcrProviderKind::ReductoV3 | OcrProviderKind::ReductoLegacy => "REDUCTO_API_KEY",
};
let base_env = match kind {
@ -103,19 +102,6 @@ pub fn decode_request_with_env(
})
})
.transpose()?;
let vertex = VertexOcrSettings {
project: string_setting(
&wire.optional_params,
&["vertex_project", "vertex_ai_project"],
)?
.or_else(|| nonblank(env("VERTEXAI_PROJECT"))),
location: string_setting(
&wire.optional_params,
&["vertex_location", "vertex_ai_location"],
)?
.or_else(|| nonblank(env("VERTEXAI_LOCATION")))
.or_else(|| nonblank(env("VERTEX_LOCATION"))),
};
let defaults = OcrConnection::default();
let max_download_bytes = env("MAX_IMAGE_URL_DOWNLOAD_SIZE_MB")
.and_then(|value| value.parse::<f64>().ok())
@ -123,20 +109,10 @@ pub fn decode_request_with_env(
.unwrap_or(defaults.max_download_bytes);
let mut request = OcrRequest::new(model.as_str().to_string(), document, params);
request.connection = OcrConnection {
api_key: nonblank(wire.api_key)
.or_else(|| nonblank(env(key_env)))
.or_else(|| {
matches!(
kind,
OcrProviderKind::VertexAi | OcrProviderKind::VertexAiDeepSeek
)
.then(|| nonblank(env("VERTEXAI_API_KEY")))
.flatten()
}),
api_key: nonblank(wire.api_key).or_else(|| nonblank(env(key_env))),
api_base: nonblank(wire.api_base).or_else(|| base_env.and_then(|name| nonblank(env(name)))),
extra_headers: headers,
azure_auth,
vertex,
timeout: timeout.unwrap_or(defaults.timeout),
poll_timeout: timeout.unwrap_or(defaults.poll_timeout),
max_download_bytes,
@ -149,25 +125,6 @@ fn nonblank(value: Option<String>) -> Option<String> {
.map(|s| s.trim().to_string())
.filter(|s| !s.is_empty())
}
fn string_setting(
params: &Map<String, Value>,
keys: &[&str],
) -> Result<Option<String>, OcrRequestError> {
for key in keys {
if let Some(value) = params.get(*key).filter(|value| !value.is_null()) {
let value = value
.as_str()
.ok_or_else(|| OcrRequestError::RequestField {
path: (*key).into(),
})?;
if let Some(value) = nonblank(Some(value.into())) {
return Ok(Some(value));
}
}
}
Ok(None)
}
pub fn decode_params(
kind: OcrProviderKind,
params: Map<String, Value>,
@ -207,12 +164,6 @@ pub fn decode_params(
"optional_params",
)?)
}
OcrProviderKind::VertexAi => {
OcrProviderRequest::VertexAi(decode_request_value(value, "optional_params")?)
}
OcrProviderKind::VertexAiDeepSeek => {
OcrProviderRequest::VertexAiDeepSeek(decode_request_value(value, "optional_params")?)
}
OcrProviderKind::ReductoV3 => {
OcrProviderRequest::ReductoV3(decode_request_value(value, "optional_params")?)
}
@ -396,19 +347,3 @@ pub fn reducto_page<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Option
_ => None,
})
}
pub(crate) fn decode_deepseek_content(
text: &str,
) -> Result<
Option<crate::providers::vertex_ai::ocr::deepseek::types::DeepSeekOcrResult>,
OcrResponseError,
> {
match serde_json::from_str::<Value>(text) {
Err(_) => Ok(None),
Ok(value) => serde_path_to_error::deserialize(value.into_deserializer())
.map(Some)
.map_err(|error| OcrResponseError::ResponseField {
path: format!("choices[0].message.content.{}", error.path()),
}),
}
}

View file

@ -5,4 +5,3 @@ pub mod bedrock;
pub mod mistral;
pub mod openai;
pub mod reducto;
pub mod vertex_ai;

View file

@ -1,5 +0,0 @@
#[cfg(test)]
#[path = "../../../../../tests/vertex_ai_deepseek_ocr.rs"]
mod tests;
pub mod transformation;
pub mod types;

View file

@ -1,142 +0,0 @@
use super::super::transformation::{authenticate_vertex, location, project};
use super::types::*;
use crate::auth::AuthError;
use crate::constants::VERTEX_DEEPSEEK_API_BASE;
use crate::ocr::error::OcrError;
use crate::ocr::error::OcrRequestError;
use crate::ocr::error::OcrResponseError;
use crate::ocr::transformation::OcrProviderConfig;
use crate::ocr::types::{OcrConnection, OcrDocument, OcrPage, OcrResponseData};
pub struct VertexAiDeepSeekOcrConfig;
pub const VERTEX_AI_DEEPSEEK_OCR_CONFIG: VertexAiDeepSeekOcrConfig = VertexAiDeepSeekOcrConfig;
impl OcrProviderConfig for VertexAiDeepSeekOcrConfig {
type InputParams = DeepSeekOcrParams;
type MappedParams = DeepSeekOcrParams;
type PreparedDocument = OcrDocument;
type RequestBody = DeepSeekOcrRequest;
type ResponseBody = DeepSeekOcrResponse;
#[tracing::instrument(target = "litellm::function_trace", level = "trace", skip_all)]
fn map_ocr_params(
&self,
params: Self::InputParams,
) -> Result<Self::MappedParams, OcrRequestError> {
Ok(params)
}
#[tracing::instrument(target = "litellm::function_trace", level = "trace", skip_all)]
fn transform_ocr_request(
&self,
model: &str,
document: OcrDocument,
params: &Self::MappedParams,
) -> Result<Self::RequestBody, OcrRequestError> {
if document.source().is_empty() {
return Err(OcrRequestError::MissingField("document URL"));
}
let model = model.strip_prefix("vertex_ai/").unwrap_or(model);
let model = if model.contains('/') {
model.to_string()
} else {
format!("deepseek-ai/{model}")
};
Ok(DeepSeekOcrRequest {
model,
messages: vec![DeepSeekOcrMessage {
role: UserRole::User,
content: vec![document],
}],
params: params.clone(),
})
}
#[tracing::instrument(target = "litellm::function_trace", level = "trace", skip_all)]
fn transform_ocr_response(
&self,
model: &str,
response: Self::ResponseBody,
_params: &Self::MappedParams,
) -> Result<OcrResponseData, OcrResponseError> {
let content = response
.choices
.into_iter()
.next()
.and_then(|choice| choice.message.content)
.ok_or(OcrResponseError::EmptyContent)?;
let (result, fallback) = match content {
DeepSeekContent::Text(text) if text.is_empty() => {
return Err(OcrResponseError::EmptyContent);
}
DeepSeekContent::Text(text) => {
let result = if text.trim_start().starts_with('{') {
crate::ocr::wire::decode_deepseek_content(&text)?
} else {
None
};
(result, text)
}
DeepSeekContent::Object(object) => {
let fallback = serde_json::to_string(&object).map_err(|_| {
OcrResponseError::ResponseField {
path: "choices[0].message.content".into(),
}
})?;
(Some(object), fallback)
}
};
let result = result.unwrap_or_default();
let pages = match result.pages {
Some(pages) if !pages.is_empty() => pages
.into_iter()
.map(|page| OcrPage {
images: page.images,
dimensions: page.dimensions,
..OcrPage::text(page.index, page.markdown)
})
.collect(),
_ => vec![OcrPage::text(0, fallback)],
};
Ok(OcrResponseData {
document_annotation: result.document_annotation,
usage_info: result.usage_info.or(response.usage),
..OcrResponseData::new(result.model.unwrap_or_else(|| model.to_string()), pages)
})
}
#[tracing::instrument(target = "litellm::function_trace", level = "trace", skip_all)]
fn complete_url(
&self,
connection: &OcrConnection,
_model: &str,
_params: &Self::MappedParams,
) -> Result<String, OcrError> {
let base = connection
.api_base
.as_deref()
.unwrap_or(VERTEX_DEEPSEEK_API_BASE)
.trim_end_matches('/');
Ok(format!(
"{base}/v1/projects/{}/locations/{}/endpoints/openapi/chat/completions",
project(connection)?,
location(connection)
))
}
async fn prepare_document(
&self,
_http_client: &reqwest::Client,
document: OcrDocument,
_connection: &OcrConnection,
_headers: &[(String, String)],
) -> Result<OcrDocument, OcrError> {
Ok(document)
}
async fn authenticate(
&self,
connection: &OcrConnection,
) -> Result<Vec<(String, String)>, AuthError> {
authenticate_vertex(connection).await
}
}

View file

@ -1,88 +0,0 @@
use crate::ocr::types::{OcrDocument, OcrPageDimensions, OcrPageImage, OcrUsageInfo};
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct DeepSeekOcrParams {
#[serde(skip_serializing_if = "Option::is_none")]
pub stream: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub temperature: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub max_tokens: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub top_p: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub n: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub stop: Option<StopSequences>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum StopSequences {
One(String),
Many(Vec<String>),
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct DeepSeekOcrRequest {
pub model: String,
pub messages: Vec<DeepSeekOcrMessage>,
#[serde(flatten)]
pub params: DeepSeekOcrParams,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct DeepSeekOcrMessage {
pub role: UserRole,
pub content: Vec<OcrDocument>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum UserRole {
User,
}
#[derive(Clone, Debug, Deserialize)]
pub struct DeepSeekOcrResponse {
#[serde(default)]
pub choices: Vec<DeepSeekChoice>,
pub usage: Option<OcrUsageInfo>,
}
#[derive(Clone, Debug, Deserialize)]
pub struct DeepSeekChoice {
pub message: DeepSeekResponseMessage,
}
#[derive(Clone, Debug, Deserialize)]
pub struct DeepSeekResponseMessage {
pub content: Option<DeepSeekContent>,
}
#[derive(Clone, Debug, Deserialize)]
#[serde(untagged)]
pub enum DeepSeekContent {
Text(String),
Object(DeepSeekOcrResult),
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct DeepSeekOcrResult {
#[serde(skip_serializing_if = "Option::is_none")]
pub pages: Option<Vec<DeepSeekPage>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub model: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub usage_info: Option<OcrUsageInfo>,
#[serde(skip_serializing_if = "Option::is_none")]
pub document_annotation: Option<Value>,
#[serde(flatten)]
pub extra_fields: Map<String, Value>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct DeepSeekPage {
#[serde(default, deserialize_with = "crate::ocr::wire::required_i64")]
pub index: i64,
#[serde(default)]
pub markdown: String,
pub images: Option<Vec<OcrPageImage>>,
pub dimensions: Option<OcrPageDimensions>,
}

View file

@ -1,5 +1 @@
pub mod deepseek;
#[cfg(test)]
#[path = "../../../../tests/vertex_ai_ocr.rs"]
mod tests;
pub mod transformation;

View file

@ -1,120 +1,467 @@
use crate::auth::AuthError;
use crate::auth::{CredentialPlacement, http::apply_credential};
use crate::constants::VERTEX_OCR_DEFAULT_LOCATION;
use crate::ocr::error::OcrError;
use crate::ocr::error::OcrRequestError;
use crate::ocr::error::OcrResponseError;
use crate::error::{AuthError, Error, json_type_name};
use crate::ocr::transformation::OcrProviderConfig;
use crate::ocr::types::{OcrConnection, OcrDocument, OcrResponseData};
use crate::ocr::types::{OcrRequestData, OcrResponseData};
use serde_json::{Map, Value, json};
use crate::providers::mistral::ocr::transformation::MISTRAL_OCR_CONFIG;
use crate::providers::mistral::ocr::types::{
MistralOcrParams, MistralOcrRequest, MistralOcrResponse,
};
const VERTEX_DEFAULT_LOCATION: &str = "us-central1";
const VERTEX_DEFAULT_DEEPSEEK_API_BASE: &str = "https://aiplatform.googleapis.com";
const VERTEX_AI_API_KEY_ENV: &str = "VERTEX_AI_API_KEY";
const VERTEXAI_API_KEY_ENV: &str = "VERTEXAI_API_KEY";
const VERTEXAI_PROJECT_ENV: &str = "VERTEXAI_PROJECT";
const VERTEXAI_LOCATION_ENV: &str = "VERTEXAI_LOCATION";
const VERTEX_LOCATION_ENV: &str = "VERTEX_LOCATION";
#[rustfmt::skip]
const DEEPSEEK_SUPPORTED_OCR_PARAMS: &[&str] = &[
"stream",
"temperature",
"max_tokens",
"top_p",
"n",
"stop",
];
pub struct VertexAiOcrConfig;
pub struct VertexAiDeepSeekOcrConfig;
pub const VERTEX_AI_OCR_CONFIG: VertexAiOcrConfig = VertexAiOcrConfig;
pub const VERTEX_AI_DEEPSEEK_OCR_CONFIG: VertexAiDeepSeekOcrConfig = VertexAiDeepSeekOcrConfig;
pub(super) fn project(connection: &OcrConnection) -> Result<&str, OcrRequestError> {
connection
.vertex
.project
.as_deref()
.ok_or(OcrRequestError::MissingField("vertex_project"))
}
pub(super) fn location(connection: &OcrConnection) -> &str {
connection
.vertex
.location
.as_deref()
.unwrap_or(VERTEX_OCR_DEFAULT_LOCATION)
fn string_param<'a>(params: &'a Map<String, Value>, keys: &[&str]) -> Option<&'a str> {
keys.iter()
.find_map(|key| params.get(*key).and_then(Value::as_str))
.map(str::trim)
.filter(|value| !value.is_empty())
}
pub(super) async fn authenticate_vertex(
connection: &OcrConnection,
) -> Result<Vec<(String, String)>, AuthError> {
if crate::http_utils::has_header(&connection.extra_headers, "Authorization") {
return Ok(connection.extra_headers.clone());
pub fn is_deepseek_model(model: &str) -> bool {
model.to_ascii_lowercase().contains("deepseek")
}
pub fn resolve_vertex_api_key(
api_key: Option<&str>,
env_lookup: &dyn Fn(&str) -> Option<String>,
) -> Result<String, Error> {
api_key
.map(str::trim)
.filter(|key| !key.is_empty())
.map(str::to_string)
.or_else(|| env_lookup(VERTEX_AI_API_KEY_ENV).filter(|key| !key.trim().is_empty()))
.or_else(|| env_lookup(VERTEXAI_API_KEY_ENV).filter(|key| !key.trim().is_empty()))
.ok_or_else(|| {
Error::Auth(AuthError::Message(
"Missing Vertex AI access token - pass api_key or provide Authorization via extra_headers"
.to_string(),
))
})
}
fn vertex_project(
params: &Map<String, Value>,
env_lookup: &dyn Fn(&str) -> Option<String>,
) -> Result<String, Error> {
string_param(params, &["vertex_project", "vertex_ai_project"])
.map(str::to_string)
.or_else(|| env_lookup(VERTEXAI_PROJECT_ENV).filter(|value| !value.trim().is_empty()))
.ok_or_else(|| {
Error::InvalidRequest(
"Missing vertex_project - Set VERTEXAI_PROJECT environment variable or pass vertex_project parameter"
.to_string(),
)
})
}
fn vertex_location(
params: &Map<String, Value>,
env_lookup: &dyn Fn(&str) -> Option<String>,
) -> String {
string_param(params, &["vertex_location", "vertex_ai_location"])
.map(str::to_string)
.or_else(|| env_lookup(VERTEXAI_LOCATION_ENV).filter(|value| !value.trim().is_empty()))
.or_else(|| env_lookup(VERTEX_LOCATION_ENV).filter(|value| !value.trim().is_empty()))
.unwrap_or_else(|| VERTEX_DEFAULT_LOCATION.to_string())
}
fn vertex_mistral_api_base(api_base: Option<&str>, location: &str) -> String {
api_base
.map(str::trim)
.filter(|value| !value.is_empty())
.map(str::to_string)
.unwrap_or_else(|| format!("https://{location}-aiplatform.googleapis.com"))
.trim_end_matches('/')
.to_string()
}
pub fn complete_vertex_mistral_url(
api_base: Option<&str>,
model: &str,
optional_params: &Map<String, Value>,
env_lookup: &dyn Fn(&str) -> Option<String>,
) -> Result<String, Error> {
let project = vertex_project(optional_params, env_lookup)?;
let location = vertex_location(optional_params, env_lookup);
let base = vertex_mistral_api_base(api_base, &location);
Ok(format!(
"{base}/v1/projects/{project}/locations/{location}/publishers/mistralai/models/{model}:rawPredict"
))
}
pub fn complete_vertex_deepseek_url(
api_base: Option<&str>,
optional_params: &Map<String, Value>,
env_lookup: &dyn Fn(&str) -> Option<String>,
) -> Result<String, Error> {
let project = vertex_project(optional_params, env_lookup)?;
let location = vertex_location(optional_params, env_lookup);
let base = api_base
.map(str::trim)
.filter(|value| !value.is_empty())
.unwrap_or(VERTEX_DEFAULT_DEEPSEEK_API_BASE)
.trim_end_matches('/');
Ok(format!(
"{base}/v1/projects/{project}/locations/{location}/endpoints/openapi/chat/completions"
))
}
fn document_content_item(document: &Value) -> Result<Value, Error> {
let object = document.as_object().ok_or_else(|| Error::InvalidType {
expected: "object",
actual: json_type_name(document),
})?;
let doc_type = object
.get("type")
.and_then(Value::as_str)
.ok_or(Error::MissingField("document.type"))?;
let url_field = match doc_type {
"image_url" => "image_url",
"document_url" => "document_url",
other => {
return Err(Error::InvalidRequest(format!(
"Unsupported document type: {other}. Expected 'image_url' or 'document_url'"
)));
}
};
let url = object
.get(url_field)
.and_then(Value::as_str)
.filter(|value| !value.is_empty())
.ok_or(Error::MissingField(url_field))?;
Ok(json!({
"type": "image_url",
"image_url": url,
}))
}
fn deepseek_model_name(model: &str) -> String {
if model.starts_with("deepseek-ai/") {
model.to_string()
} else {
format!("deepseek-ai/{model}")
}
}
fn first_choice_content(response: &Value) -> Result<Value, Error> {
response
.get("choices")
.and_then(Value::as_array)
.and_then(|choices| choices.first())
.and_then(|choice| choice.get("message"))
.and_then(|message| message.get("content"))
.cloned()
.filter(|content| match content {
Value::String(value) => !value.is_empty(),
Value::Object(_) => true,
_ => false,
})
.ok_or_else(|| Error::InvalidResponse("No content in DeepSeek OCR response".to_string()))
}
fn ocr_data_from_content(content: Value, usage: Option<Value>, model: &str) -> Value {
match content {
Value::String(content) => {
if content.trim_start().starts_with('{') {
serde_json::from_str(&content).unwrap_or_else(|_| {
json!({
"pages": [{"index": 0, "markdown": content}],
"model": model,
"usage_info": usage.unwrap_or_else(|| json!({})),
})
})
} else {
json!({
"pages": [{"index": 0, "markdown": content}],
"model": model,
"usage_info": usage.unwrap_or_else(|| json!({})),
})
}
}
Value::Object(_) => content,
other => json!({
"pages": [{"index": 0, "markdown": other.to_string()}],
"model": model,
"usage_info": usage.unwrap_or_else(|| json!({})),
}),
}
let key = connection
.api_key
.as_deref()
.ok_or(crate::AuthError::MissingApiKey {
provider: "Vertex AI",
})?;
apply_credential(
connection.extra_headers.clone(),
key,
CredentialPlacement::Bearer,
)
}
impl OcrProviderConfig for VertexAiOcrConfig {
type InputParams = MistralOcrParams;
type MappedParams = MistralOcrParams;
type PreparedDocument = OcrDocument;
type RequestBody = MistralOcrRequest;
type ResponseBody = MistralOcrResponse;
fn map_ocr_params(
&self,
params: MistralOcrParams,
) -> Result<MistralOcrParams, OcrRequestError> {
MISTRAL_OCR_CONFIG.map_ocr_params(params)
fn supported_ocr_params(&self) -> &'static [&'static str] {
MISTRAL_OCR_CONFIG.supported_ocr_params()
}
#[tracing::instrument(target = "litellm::function_trace", level = "trace", skip_all)]
fn transform_ocr_request(
&self,
model: &str,
document: OcrDocument,
params: &MistralOcrParams,
) -> Result<MistralOcrRequest, OcrRequestError> {
MISTRAL_OCR_CONFIG.transform_ocr_request(model, document, params)
document: Value,
optional_params: Map<String, Value>,
) -> Result<OcrRequestData, Error> {
MISTRAL_OCR_CONFIG.transform_ocr_request(model, document, optional_params)
}
fn transform_ocr_response(
&self,
model: &str,
response: MistralOcrResponse,
params: &MistralOcrParams,
) -> Result<OcrResponseData, OcrResponseError> {
MISTRAL_OCR_CONFIG.transform_ocr_response(model, response, params)
response_json: Value,
) -> Result<OcrResponseData, Error> {
MISTRAL_OCR_CONFIG.transform_ocr_response(model, response_json)
}
#[tracing::instrument(target = "litellm::function_trace", level = "trace", skip_all)]
fn complete_url(
&self,
connection: &OcrConnection,
api_base: Option<&str>,
model: &str,
_params: &MistralOcrParams,
) -> Result<String, OcrError> {
let location = location(connection);
let default_base = format!("https://{location}-aiplatform.googleapis.com");
let base = connection
.api_base
.as_deref()
.unwrap_or(&default_base)
.trim_end_matches('/');
Ok(format!(
"{base}/v1/projects/{}/locations/{location}/publishers/mistralai/models/{model}:rawPredict",
project(connection)?
))
optional_params: &Map<String, Value>,
env_lookup: &dyn Fn(&str) -> Option<String>,
) -> Result<String, Error> {
complete_vertex_mistral_url(api_base, model, optional_params, env_lookup)
}
async fn prepare_document(
fn resolve_api_key(
&self,
http_client: &reqwest::Client,
document: OcrDocument,
connection: &OcrConnection,
_headers: &[(String, String)],
) -> Result<OcrDocument, OcrError> {
crate::ocr::client::convert_document_url_to_data_uri(http_client, document, connection)
.await
api_key: Option<&str>,
env_lookup: &dyn Fn(&str) -> Option<String>,
) -> Result<String, Error> {
resolve_vertex_api_key(api_key, env_lookup)
}
async fn authenticate(
&self,
connection: &OcrConnection,
) -> Result<Vec<(String, String)>, AuthError> {
authenticate_vertex(connection).await
fn requires_data_uri_document(&self) -> bool {
true
}
}
impl OcrProviderConfig for VertexAiDeepSeekOcrConfig {
#[tracing::instrument(target = "litellm::function_trace", level = "trace", skip_all)]
fn supported_ocr_params(&self) -> &'static [&'static str] {
DEEPSEEK_SUPPORTED_OCR_PARAMS
}
#[tracing::instrument(target = "litellm::function_trace", level = "trace", skip_all)]
fn map_ocr_params(&self, non_default_params: &Map<String, Value>) -> Map<String, Value> {
non_default_params
.iter()
.filter(|(name, _)| DEEPSEEK_SUPPORTED_OCR_PARAMS.contains(&name.as_str()))
.map(|(name, value)| (name.clone(), value.clone()))
.collect()
}
#[tracing::instrument(target = "litellm::function_trace", level = "trace", skip_all)]
fn transform_ocr_request(
&self,
model: &str,
document: Value,
optional_params: Map<String, Value>,
) -> Result<OcrRequestData, Error> {
let mut data = Map::new();
data.insert(
"model".to_string(),
Value::String(deepseek_model_name(model)),
);
data.insert(
"messages".to_string(),
json!([{"role": "user", "content": [document_content_item(&document)?]}]),
);
for (key, value) in optional_params {
if DEEPSEEK_SUPPORTED_OCR_PARAMS.contains(&key.as_str()) {
data.insert(key, value);
}
}
Ok(OcrRequestData {
data: Value::Object(data),
files: None,
})
}
#[tracing::instrument(target = "litellm::function_trace", level = "trace", skip_all)]
fn transform_ocr_response(
&self,
model: &str,
response_json: Value,
) -> Result<OcrResponseData, Error> {
let response = response_json
.as_object()
.ok_or_else(|| Error::InvalidType {
expected: "object",
actual: json_type_name(&response_json),
})?;
let usage = response.get("usage").cloned();
let content = first_choice_content(&response_json)?;
let mut ocr_data = ocr_data_from_content(content.clone(), usage.clone(), model);
if !ocr_data.get("pages").is_some_and(Value::is_array) {
ocr_data = json!({
"pages": [{
"index": 0,
"markdown": match content {
Value::String(value) => value,
other => other.to_string(),
}
}],
"model": ocr_data.get("model").and_then(Value::as_str).unwrap_or(model),
"usage_info": ocr_data.get("usage_info").cloned().or(usage).unwrap_or_else(|| json!({})),
});
}
let object = ocr_data.as_object().ok_or_else(|| Error::InvalidType {
expected: "object",
actual: json_type_name(&ocr_data),
})?;
let pages = object
.get("pages")
.and_then(Value::as_array)
.cloned()
.unwrap_or_default();
let usage_info = object
.get("usage_info")
.cloned()
.or_else(|| response.get("usage").cloned());
Ok(OcrResponseData {
pages,
model: object
.get("model")
.and_then(Value::as_str)
.unwrap_or(model)
.to_string(),
document_annotation: object.get("document_annotation").cloned(),
usage_info,
object: "ocr".to_string(),
extra_fields: Map::new(),
provider_native_response: None,
})
}
#[tracing::instrument(target = "litellm::function_trace", level = "trace", skip_all)]
fn complete_url(
&self,
api_base: Option<&str>,
_model: &str,
optional_params: &Map<String, Value>,
env_lookup: &dyn Fn(&str) -> Option<String>,
) -> Result<String, Error> {
complete_vertex_deepseek_url(api_base, optional_params, env_lookup)
}
fn resolve_api_key(
&self,
api_key: Option<&str>,
env_lookup: &dyn Fn(&str) -> Option<String>,
) -> Result<String, Error> {
resolve_vertex_api_key(api_key, env_lookup)
}
}
#[cfg(test)]
mod tests {
use super::*;
use rstest::rstest;
#[test]
fn vertex_mistral_url_uses_project_location_and_model() {
let params = Map::from_iter([
("vertex_project".to_string(), json!("proj-1")),
("vertex_location".to_string(), json!("europe-west4")),
]);
let url = complete_vertex_mistral_url(None, "mistral-ocr-maas", &params, &|_| None)
.expect("url builds");
assert_eq!(
url,
"https://europe-west4-aiplatform.googleapis.com/v1/projects/proj-1/locations/europe-west4/publishers/mistralai/models/mistral-ocr-maas:rawPredict"
);
}
#[test]
fn vertex_mistral_reuses_mistral_body_transform() {
let body = VERTEX_AI_OCR_CONFIG
.transform_ocr_request(
"mistral-ocr-maas",
json!({"type": "image_url", "image_url": "data:image/png;base64,abc"}),
Map::new(),
)
.expect("request transforms")
.data;
assert_eq!(body["model"], "mistral-ocr-maas");
assert_eq!(body["document"]["image_url"], "data:image/png;base64,abc");
}
#[test]
fn vertex_deepseek_request_uses_ocr_endpoint_shape() {
let body = VERTEX_AI_DEEPSEEK_OCR_CONFIG
.transform_ocr_request(
"deepseek-ocr-maas",
json!({"type": "document_url", "document_url": "gs://bucket/doc.pdf"}),
Map::from_iter([("temperature".to_string(), json!(0.1))]),
)
.expect("request transforms")
.data;
assert_eq!(body["model"], "deepseek-ai/deepseek-ocr-maas");
assert_eq!(body["temperature"], 0.1);
assert_eq!(
body["messages"][0]["content"][0],
json!({"type": "image_url", "image_url": "gs://bucket/doc.pdf"})
);
}
#[rstest]
#[case::bare_model("deepseek-ocr-maas")]
#[case::namespaced_model("deepseek-ai/deepseek-ocr-maas")]
fn vertex_deepseek_request_uses_single_provider_namespace(#[case] model: &str) {
let body = VERTEX_AI_DEEPSEEK_OCR_CONFIG
.transform_ocr_request(
model,
json!({"type": "image_url", "image_url": "data:image/png;base64,AA=="}),
Map::new(),
)
.expect("request transforms")
.data;
assert_eq!(body["model"], "deepseek-ai/deepseek-ocr-maas");
}
#[test]
fn vertex_deepseek_response_wraps_markdown_content() {
let response = VERTEX_AI_DEEPSEEK_OCR_CONFIG
.transform_ocr_response(
"deepseek-ocr-maas",
json!({
"choices": [{"message": {"content": "# OCR text"}}],
"usage": {"prompt_tokens": 1}
}),
)
.expect("response transforms");
assert_eq!(
response.pages,
vec![json!({"index": 0, "markdown": "# OCR text"})]
);
assert_eq!(response.model, "deepseek-ocr-maas");
assert_eq!(response.usage_info, Some(json!({"prompt_tokens": 1})));
}
}

View file

@ -10,12 +10,6 @@ fn typed_provider_and_model_select_the_request_contract() {
OcrProviderKind::AzureDocumentIntelligence,
),
("azure_ai", "future-model", OcrProviderKind::AzureAi),
(
"vertex_ai",
"deepseek-ocr",
OcrProviderKind::VertexAiDeepSeek,
),
("vertex_ai", "future-model", OcrProviderKind::VertexAi),
("reducto", "parse-legacy", OcrProviderKind::ReductoLegacy),
("reducto", "parse-v3", OcrProviderKind::ReductoV3),
("reducto", "future-model", OcrProviderKind::ReductoV3),

View file

@ -1,57 +0,0 @@
use super::transformation::VERTEX_AI_DEEPSEEK_OCR_CONFIG as CONFIG;
use crate::ocr::tests::{body, transform};
use rstest::rstest;
use serde_json::json;
#[rstest]
#[case("deepseek-ocr-maas")]
#[case("deepseek-ai/deepseek-ocr-maas")]
#[case("vertex_ai/deepseek-ai/deepseek-ocr-maas")]
#[tokio::test]
async fn vertex_deepseek_request_uses_single_provider_namespace(#[case] model: &str) {
let result = body(
&CONFIG,
model,
json!({"type":"image_url","image_url":"gs://bucket/a.png"}),
json!({"temperature":0.1,"max_tokens":1024,"ignored":true}),
)
.await
.unwrap();
assert_eq!(result["model"], "deepseek-ai/deepseek-ocr-maas");
assert_eq!(
result["messages"][0]["content"][0],
json!({"type":"image_url","image_url":"gs://bucket/a.png"})
);
assert_eq!(result["temperature"], 0.1);
assert!(result.get("ignored").is_none());
}
#[rstest]
#[case(json!("# hello"),"# hello")]
#[case(json!("{broken"),"{broken")]
#[case(json!("{\"pages\":[{\"markdown\":\"json text\"}]}"),"json text")]
#[case(json!({"pages":[{"markdown":"object"}]}),"object")]
fn vertex_deepseek_response_wraps_markdown_content(
#[case] content: serde_json::Value,
#[case] expected: &str,
) {
let result = transform(
&CONFIG,
"model",
json!({"choices":[{"message":{"content":content}}],"usage":{"prompt_tokens":1}}),
json!({}),
)
.unwrap();
assert_eq!(result["pages"][0]["markdown"], expected);
assert_eq!(result["pages"][0]["index"], 0);
assert_eq!(result["usage_info"]["prompt_tokens"], 1);
}
#[test]
fn deepseek_rejects_missing_content_and_invalid_structured_pages() {
for response in [
json!({"choices":[]}),
json!({"choices":[{"message":{"content":""}}]}),
json!({"choices":[{"message":{"content":{"pages":[{"markdown":42}]}}}]}),
] {
assert!(transform(&CONFIG, "model", response, json!({})).is_err());
}
}

View file

@ -1,45 +0,0 @@
use super::transformation::VERTEX_AI_OCR_CONFIG;
use crate::ocr::tests::body;
use crate::ocr::transformation::OcrProviderConfig;
use crate::ocr::types::{OcrConnection, VertexOcrSettings};
use crate::providers::mistral::ocr::transformation::MISTRAL_OCR_CONFIG;
use serde_json::json;
#[tokio::test]
async fn vertex_mistral_reuses_mistral_body_transform() {
let doc = json!({"type":"image_url","image_url":"data:image/png;base64,YWJj"});
assert_eq!(
body(
&VERTEX_AI_OCR_CONFIG,
"model",
doc.clone(),
json!({"extract_footer":true})
)
.await
.unwrap(),
body(
&MISTRAL_OCR_CONFIG,
"model",
doc,
json!({"extract_footer":true})
)
.await
.unwrap()
);
}
#[test]
fn vertex_mistral_url_uses_project_location_and_model() {
let connection = OcrConnection {
vertex: VertexOcrSettings {
project: Some("proj-1".into()),
location: Some("europe-west4".into()),
},
..Default::default()
};
assert_eq!(
VERTEX_AI_OCR_CONFIG
.complete_url(&connection, "mistral-ocr-maas", &Default::default())
.unwrap(),
"https://europe-west4-aiplatform.googleapis.com/v1/projects/proj-1/locations/europe-west4/publishers/mistralai/models/mistral-ocr-maas:rawPredict"
);
}