fix(ocr): read Azure Document Intelligence query options from extra_body

This commit is contained in:
mateo-berri 2026-10-29 10:01:46 -07:00
parent 6cdb9959df
commit 84898ff2e5
2 changed files with 67 additions and 4 deletions

View file

@ -72,6 +72,8 @@ async fn pages_features_and_extra_options_map_to_the_analyze_call() {
#[case(json!({"features": "languages&pages=1"}), Error::Features)]
#[case(json!({"req_format": "azure"}), Error::RequestFormat)]
#[case(json!({"outputContentFormat": "html"}), Error::OutputContentFormat)]
#[case(json!({"extra_body": {"outputContentFormat": "html"}}), Error::OutputContentFormat)]
#[case(json!({"extra_body": {"pages": [-1]}}), Error::Pages(PagesFault::Negative))]
#[tokio::test]
async fn invalid_pages_features_and_format_are_rejected_before_sending(
#[case] options: Value,
@ -263,6 +265,51 @@ async fn the_requested_output_content_format_reaches_azure_in_its_query_spelling
assert_eq!(response.pages[0].markdown, "hello");
}
#[rstest]
#[case::output_content_format(
json!({"extra_body": {"outputContentFormat": "markdown"}}),
[None, None, Some("markdown"), Some("unicodeCodePoint")]
)]
#[case::pages_and_features(
json!({"extra_body": {"pages": "2", "features": ["languages"]}}),
[Some("2"), Some("languages"), None, None]
)]
#[case::over_the_top_level(
json!({"pages": [0], "outputContentFormat": "text", "extra_body": {"pages": "2-3", "outputContentFormat": "markdown"}}),
[Some("2-3"), None, Some("markdown"), Some("unicodeCodePoint")]
)]
#[case::dropped(
json!({"extra_body": {"outputContentFormat": "html"}, "drop_params": true}),
[None, None, None, None]
)]
#[tokio::test]
async fn azure_query_params_in_extra_body_reach_the_analyze_query(
#[case] options: Value,
#[case] expected: [Option<&str>; 4],
) {
let upstream = upstream([json_response(json!({
"status": "succeeded",
"analyzeResult": {"pages": [{"pageNumber": 1, "lines": [{"content": "hello"}]}]}
}))])
.await;
perform(ocr_request(LAYOUT_MODEL, &upstream.uri(), options))
.await
.unwrap();
let sent = only_request(&upstream).await;
assert_eq!(
[
"pages",
"features",
"outputContentFormat",
"stringIndexType"
]
.map(|name| sent.query(name)),
expected.map(|value| value.map(String::from))
);
}
#[tokio::test]
async fn native_format_normalizes_pages_and_keeps_the_provider_response() {
let operation = json!({

View file

@ -38,6 +38,7 @@ const AZURE_DI_API_KEY_ENV: &str = "AZURE_DOCUMENT_INTELLIGENCE_API_KEY";
const AZURE_DI_ENDPOINT_ENV: &str = "AZURE_DOCUMENT_INTELLIGENCE_ENDPOINT";
const OUTPUT_CONTENT_FORMAT_PARAM: &str = "outputContentFormat";
const OUTPUT_CONTENT_FORMAT_ALIAS: &str = "output_content_format";
const QUERY_PARAMS: [&str; 3] = ["pages", "features", OUTPUT_CONTENT_FORMAT_PARAM];
const CODE_POINT_STRING_INDEX: &str = "unicodeCodePoint";
const STRING_INDEX_TYPE_FIELD: &str = "stringIndexType";
const MARKDOWN_MODEL_ID: &str = "prebuilt-layout";
@ -217,13 +218,14 @@ impl BaseOcrConfig for AzureDocumentIntelligenceOcrConfig {
non_default_params: &CallArguments,
model: &str,
) -> Result<DocumentIntelligenceParams, Error> {
let output_content_format = requested_output_content_format(non_default_params)?;
let params = with_query_params_from_extra_body(non_default_params);
let output_content_format = requested_output_content_format(&params)?;
let slices_markdown_pages = output_content_format == Some(OutputContentFormat::Markdown)
&& response_format(non_default_params)? != OcrResponseFormat::Native
&& response_format(&params)? != OcrResponseFormat::Native
&& model_id(model).is_ok_and(|id| id == MARKDOWN_MODEL_ID);
Ok(DocumentIntelligenceParams {
pages: normalize_pages_param(non_default_params.get("pages"))?,
features: normalize_features_param(non_default_params.get("features"))?,
pages: normalize_pages_param(params.get("pages"))?,
features: normalize_features_param(params.get("features"))?,
output_content_format,
string_index_type: slices_markdown_pages.then_some(CODE_POINT_STRING_INDEX),
})
@ -404,6 +406,20 @@ fn normalize_features_param(features: Option<&Value>) -> Result<Option<String>,
Ok(Some(normalized.join(",")))
}
fn with_query_params_from_extra_body(arguments: &CallArguments) -> CallArguments {
let overrides = arguments
.get("extra_body")
.and_then(Value::as_object)
.into_iter()
.flatten()
.filter(|(name, _)| QUERY_PARAMS.contains(&name.as_str()));
arguments
.iter()
.chain(overrides)
.map(|(name, value)| (name.clone(), value.clone()))
.collect()
}
fn requested_output_content_format(
params: &CallArguments,
) -> Result<Option<OutputContentFormat>, Error> {