调用Azure Document Intelligence的begin_analyze_document接口时出现“Connection string is either blank or malformed”错误求助
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-myServicematches 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:
Old Form Recognizer imports (likefrom azure.ai.documentintelligence import DocumentIntelligenceClient from azure.core.credentials import AzureKeyCredential from azure.ai.documentintelligence.models import AnalyzeDocumentRequest, AnalyzeResultazure.ai.formrecognizer) won't work with the newDocumentIntelligenceClient, 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_STRINGset—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.

