How to Extract Entities from Text with Azure AI Language
Extracting entities from text — people, organizations, locations, dates and quantities — is one of the fastest ways to turn comments, emails or free-form descriptions into structured data. With Azure AI Language you can do it with one resource in the portal and a few lines of Python, without training any model.
Prerequisites
- An active Azure subscription.
- Permission to create a Language resource (Azure AI services) in the portal.
- Python 3.8 or later installed.
- Basic Python knowledge (variables,
forloops, lists).
Step 1: Create the Language resource in the Azure portal
In the Azure portal, search for Language and create a Language service resource. Pick the subscription, a resource group, the region closest to your users and a pricing tier — the free tier, where available, is enough for learning.
Once the resource is created, open it and go to Keys and Endpoint. Copy one of the keys and the endpoint: those are the two values you need to authenticate.
Step 2: Store the key and endpoint in environment variables
Never hard-code the key. Keep it in environment variables so it is not exposed in repositories or screenshots.
REM Windows (PowerShell or CMD)
setx LANGUAGE_KEY "your-key"
setx LANGUAGE_ENDPOINT "https://your-resource.cognitiveservices.azure.com/"
# Linux or macOS
export LANGUAGE_KEY="your-key"
export LANGUAGE_ENDPOINT="https://your-resource.cognitiveservices.azure.com/"
On Windows, close and reopen the terminal after setx so the variables become visible.
Step 3: Install the SDK
The official Python SDK is called azure-ai-textanalytics and it is what gives you entity recognition.
pip install azure-ai-textanalytics
Step 4: Authenticate and create the client
The client only needs the endpoint and the key. This block is the foundation for every example below.
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics import TextAnalyticsClient
endpoint = os.environ["LANGUAGE_ENDPOINT"]
key = os.environ["LANGUAGE_KEY"]
client = TextAnalyticsClient(
endpoint=endpoint,
credential=AzureKeyCredential(key)
)
Step 5: Extract entities from a text
The recognize_entities method takes a list of documents (strings) and returns the entities found in each one. Note the language parameter: passing the correct language noticeably improves the quality of the result.
documents = [
"Maria Silva bought 3 Power BI licences in Lisbon for 500 euros."
]
result = client.recognize_entities(documents=documents, language="en")
for doc in result:
if doc.is_error:
print("Error:", doc.error.code, doc.error.message)
continue
for entity in doc.entities:
print(entity.text, "|", entity.category, "|",
entity.subcategory, "|", round(entity.confidence_score, 2))
Each entity carries three useful pieces of information: the detected text, the category (for example Person, Location, Quantity, DateTime, Organization), an optional subcategory, and a confidence score between 0 and 1. That last value is what lets you decide which results to trust.
Step 6: Process several documents and filter by confidence
In practice you rarely analyse a single sentence. Send the texts as a batch and collect everything into a table so you can filter out low-confidence entities.
import pandas as pd
documents = [
"Maria Silva bought 3 Power BI licences in Lisbon for 500 euros.",
"The contract with Contoso ends on December 31.",
"We sent the report to João Barros on Tuesday."
]
result = client.recognize_entities(documents=documents, language="en")
rows = []
for i, doc in enumerate(result):
if doc.is_error:
print("Document", i, "failed:", doc.error.code)
continue
for e in doc.entities:
rows.append({
"document": i,
"text": e.text,
"category": e.category,
"subcategory": e.subcategory,
"confidence": e.confidence_score
})
df = pd.DataFrame(rows)
df_reliable = df[df["confidence"] >= 0.8]
print(df_reliable)
The service caps how many documents you can send per call, so if you have thousands of texts, split the list into small chunks and make one call per chunk.
Step 7: Fix the most common errors
- 401 Unauthorized: the key is wrong or was regenerated. Copy it again from Keys and Endpoint.
- 404 Not Found: the endpoint is incomplete. It must end with a slash and match the right resource and region.
- Few entities detected: check the language code. Sending Portuguese text with
language="en"badly degrades the result. - InvalidDocumentBatch: you sent too many documents (or documents that are too long) in a single call. Reduce the batch size.
Verify the result
Run the script with the sample sentence. You should see Maria Silva recognized as Person, Lisbon as Location, "3" as Quantity and "500 euros" as a Quantity with a currency subcategory. If the list comes back empty, print doc.is_error and doc.error.message: it is almost always the key, the endpoint or the language. A good final test is to feed a real text from your business and confirm that the entities you care about show up with confidence above 0.8.
Conclusion
With one Language resource and fewer than 20 lines of Python you can already turn free text into structured entities, ready to load into a Lakehouse or a Power BI model. The natural next step is to combine this with PII detection to anonymize data before analysis, or to train a custom NER model when the built-in categories are not enough. Which entities from your own business — product codes, contract numbers — would you like to teach the model?