You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用Azure Document Intelligence的begin_analyze_document接口时出现“Connection string is either blank or malformed”错误求助

Troubleshooting "Connection string is either blank or malformed" Error in Azure Document Intelligence

Let's break down why you're seeing this error and how to fix it—even though your code uses an endpoint + API key instead of a connection string, there are a few common culprits here:

Possible Causes & Fixes

1. Incomplete or Malformed API Key

The key in your code is truncated (ANZ...CzH), which is likely the core issue. API keys are full, unbroken strings—if you copied only part of it, or included extra spaces/newlines by accident, the client can't authenticate properly.

  • Fix:
    • Head back to your Azure portal, navigate to your Document Intelligence resource, and go to the Keys and Endpoint page.
    • Copy the full Key1 or Key2 value exactly (no leading/trailing spaces, no ellipses).
    • Replace the truncated key in your code with the full string:
      key = "your-full-untruncated-api-key-here"
      

2. Invalid Endpoint Format

Double-check your endpoint for typos or formatting issues:

  • A valid Document Intelligence endpoint should look like https://<your-resource-name>.cognitiveservices.azure.com/ (note the trailing slash—while some SDK versions tolerate its absence, it's safest to include it).
  • Make sure di-myService matches the exact resource name in your Azure portal (typos here will break the connection immediately).

3. SDK Version or Import Mismatch

If you're migrating from the old Form Recognizer SDK to the new Document Intelligence SDK, mismatched imports or outdated packages can cause unexpected errors:

  • Update the SDK: Run this in your terminal to get the latest stable version:
    pip install azure-ai-documentintelligence --upgrade
    
  • Verify Imports: Ensure you're using the correct classes for the new SDK:
    from azure.ai.documentintelligence import DocumentIntelligenceClient
    from azure.core.credentials import AzureKeyCredential
    from azure.ai.documentintelligence.models import AnalyzeDocumentRequest, AnalyzeResult
    
    Old Form Recognizer imports (like azure.ai.formrecognizer) won't work with the new DocumentIntelligenceClient, so double-check your code for leftover old references.

4. Accidental Connection String Configuration

Sometimes, an environment variable or hidden config can override your explicit endpoint/key setup:

  • Check if you have an environment variable named AZURE_DOCUMENT_INTELLIGENCE_CONNECTION_STRING set—if so, the client might be trying to use that instead of your coded endpoint/key.
  • If you want to switch to using a connection string, grab the full valid string from the Keys and Endpoint page in Azure (format: Endpoint=https://xxx.cognitiveservices.azure.com/;ApiKey=xxx), and initialize the client like this:
    from azure.ai.documentintelligence import DocumentIntelligenceClient
    
    connection_string = "your-full-connection-string-here"
    client = DocumentIntelligenceClient.from_connection_string(connection_string)
    

Quick Validation Test

First, confirm your client initializes correctly before running the full analysis:

from azure.ai.documentintelligence import DocumentIntelligenceClient
from azure.core.credentials import AzureKeyCredential

endpoint = "https://di-myService.cognitiveservices.azure.com/"
key = "your-full-api-key"

try:
    client = DocumentIntelligenceClient(endpoint=endpoint, credential=AzureKeyCredential(key))
    print("Client initialized successfully!")
except Exception as e:
    print(f"Initialization failed: {str(e)}")

If this works, proceed with your original begin_analyze_document code—if not, the error message here will give you more specific clues to debug.

内容的提问来源于stack exchange,提问作者RT.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 09:43:13