PAYATHON 2026

Azure Mistral OCR 为何拒绝 confidence_scores_granularity?

AI 小顾

结论

HTTP 422 响应表明,当前 Azure endpoint 在请求校验时不接受 confidence_scores_granularity,因此这次请求无法获取词级置信度。仅凭这条错误,不能认定所有 Azure-hosted Mistral OCR 模型和部署都不支持 confidence scores。

对于当前 mistral-document-ai-2512 部署,还无法确认是否存在可直接替换的参数,或能启用该功能的 api-version。应查看实际部署对应的 API 文档或向 Azure 支持确认,不能直接套用原生 Mistral API 的参数。

为什么会返回 extra_forbidden?

错误响应中的这两个字段指出了问题位置:

{
  "type": "extra_forbidden",
  "loc": ["body", "confidence_scores_granularity"]
}

服务端将请求体中的 confidence_scores_granularity 判定为不允许的额外字段,请求因此在输入校验阶段被拒绝,还未进入返回 OCR confidence scores 的处理流程。

把值从 "word" 改成 "page" 无法解决字段本身不被接受的问题。对于这条错误,也没有依据表明需要修改 PDF 的 Base64 编码。

原生 Mistral OCR 文档列出了 confidence_scores_granularity:"page" 返回页级汇总分数,"word" 还会返回 word_confidence_scores。但文档中的示例使用 mistral-ocr-latest,不能据此认定 Azure 上的 mistral-document-ai-2512 具有相同的请求接口和功能。Mistral OCR confidence scores 文档

Azure 模型目录将 mistral-document-ai-2512 描述为由 mistral-ocr-2512 驱动的 Document AI 模型,目录说明中没有给出启用 confidence scores 的参数或 API version。Azure 模型目录

当前 Azure 请求应如何调整?

先移除被拒绝的字段,验证基础 OCR 请求,并补充错误处理。下面的示例沿用现有 endpoint、认证方式和 pdfBuffer:

const endpoint = process.env.MISTRAL_BASE_URL;
const apiKey = process.env.MISTRAL_API_KEY;
const modelName = "mistral-document-ai-2512";

if (!endpoint || !apiKey) {
  throw new Error("MISTRAL_BASE_URL and MISTRAL_API_KEY are required");
}

const base64Pdf = pdfBuffer.toString("base64");

const response = await fetch(endpoint, {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    Authorization: `Bearer ${apiKey}`,
  },
  body: JSON.stringify({
    model: modelName,
    document: {
      type: "document_url",
      document_url: `data:application/pdf;base64,${base64Pdf}`,
    },
    include_image_base64: true,
  }),
});

if (!response.ok) {
  const errorBody = await response.text();
  throw new Error(`OCR request failed: ${response.status}\n${errorBody}`);
}

const result = await response.json();

移除已知被拒绝的字段不会启用 confidence scores,也不代表其他字段已经通过实际验证。

接下来可以按以下步骤确认:

  1. 在 Azure 部署页面核对实际模型版本、endpoint 路径和调用示例。原代码没有展示 MISTRAL_BASE_URL 的值,无法仅凭代码确定完整路由和版本。
  2. 查看该部署对应的请求参数和响应结构,确认是否明确列出 confidence scores。只有部署文档支持时,才添加相应参数。
  3. 如果文档没有说明,向 Azure 支持提供 Japan East、模型名称、部署版本、脱敏后的请求路径、完整 422 错误及响应中的请求标识,请其确认支持范围和可用升级路径。

不要随意添加其他 Azure 服务使用的 api-version,也不要假定更换 SDK 就能让服务端接受额外字段。

必须获取词级置信度时有哪些方案?

使用 Azure AI Document Intelligence Read

如果业务要求在 Azure 内获取扫描 PDF 的词级置信度和位置,可以评估 Azure AI Document Intelligence 的 prebuilt-read。官方文档说明,识别出的词包含 confidence 和位置坐标 polygon,可用于筛选和标记低置信度词。Document Intelligence Read 文档

这是独立服务,需要使用对应的 endpoint、凭据和调用流程,不能将它的模型名称或参数直接填入现有 Mistral 请求。

完成 Document Intelligence 分析并从 REST 响应中取得 analyzeResult 后,可以用下面的函数筛选结果。threshold 由业务调用方提供:

function findLowConfidenceWords(analyzeResult, threshold) {
  if (!Number.isFinite(threshold) || threshold < 0 || threshold > 1) {
    throw new Error("threshold must be between 0 and 1");
  }

  return (analyzeResult.pages ?? []).flatMap((page) =>
    (page.words ?? [])
      .filter(
        (word) =>
          Number.isFinite(word.confidence) &&
          word.confidence < threshold
      )
      .map((word) => ({
        pageNumber: page.pageNumber,
        content: word.content,
        confidence: word.confidence,
        polygon: word.polygon,
      }))
  );
}

这些分数来自 Document Intelligence 的识别结果。如果同时保留 Mistral 生成的 Markdown,需要另行对齐两套结果中的文本和位置,不能将这些分数直接称为 Mistral 的 confidence scores。

使用支持该参数的原生 Mistral OCR

如果业务允许使用原生 Mistral API,可以按官方文档选择支持 confidence scores 的模型和接口。调用时需要同时核对 endpoint、凭据和模型名称,不能只把 Azure 请求中的 model 改成 mistral-ocr-latest。

如何将分数用于扫描 PDF 质量验证?

词级分数可用来筛选需要复核的内容。定位低置信度区域还需要对应的位置数据;如果使用 Document Intelligence,可以结合 polygon 在页面图像上标记这些词。

需要页级质量指标时,可以自行统计低置信度词占比或词级分数均值,但应标明这是业务计算的指标,不能冒充服务原生返回的 page confidence。

阈值应根据实际扫描样本和人工标注结果确定。置信度缺失时应按“未提供分数”处理,不能自动设为 0 或 1。被 OCR 完全漏掉的文字也不会进入词级分数列表,因此仍需结合漏字检查和人工抽检评估质量。

备注:内容仅供参考。