Lesson 2 of 2 Text Analysis Applications in Foundry

Analyzing Text Reliably with Generative Models

This lesson explains how generative models analyze text from a prompt, including analysis that no prebuilt feature offers, how to make that analysis reliable, and when to combine it with Azure Language.

Learning outcomes

After completing this lesson, you will be able to:

  • Write reliable analysis prompts.
  • Explain how structured outputs constrain a response to a JSON schema.
  • Choose between, or combine, Azure Language and a generative model.

Prompting for analysis

A model can classify, extract, summarize and judge tone when instructed. Reliable analysis prompts follow the prompt engineering techniques: precise instructions, the allowed labels or fields, delimiters around the text, a few examples and low randomness. Because the model's general language understanding is broad, a single prompt can return several results at once, such as a summary, the product mentioned, a custom category and whether a follow-up is needed.

Structured outputs

When code processes the result, the response must have a predictable shape. Structured outputs supply a JSON schema with the request, and the model's output is constrained to match it, including enumerations of allowed labels. In Python, the Responses API's responses.parse() accepts a Pydantic model as text_format and returns a parsed object.

from typing import Literal
from pydantic import BaseModel

class Review(BaseModel):
    product: str
    topic: Literal["quality", "delivery", "price", "support", "other"]
    needs_follow_up: bool

r = openai.responses.parse(model="gpt-5.4-mini", text_format=Review,
                           instructions="Extract the fields from the product review.",
                           input="My order arrived two weeks late and nobody answered my emails.")
print(r.output_parsed)
Scenario questions

Predictable output from a model

Exam skills 2.2.1

Make a model's text analysis results dependable for code.

Log in to try this activity Interactive activities are free with an account.

Choosing and combining approaches

Azure Language gives the same result every time, with calibrated confidence scores and character offsets, at a per-text price and with no prompt to maintain. A generative model gives flexible, custom analysis but needs careful prompting and testing, and its output can vary. Applications often combine them, for example by redacting PII with Azure Language before the text reaches the model, or by using prebuilt sentiment for dashboards and a model for custom categories.

The following table recaps the choice.

RequirementBetter fit
Standard sentiment, key phrases or entities with scoresAzure Language
Redaction with entity categories and offsetsAzure Language PII detection
Company-specific categories or fieldsGenerative model with structured outputs
Nuanced tone or intent, such as sarcasm or urgencyGenerative model
Free-form summary in a custom formatGenerative model

The following diagram shows a combined design and the strengths of each approach.

Combining Azure Language with a generative model. Azure Language gives the same result every time, with calibrated confidence scores and character offsets, so it suits PII redaction and standard sentiment, key phrases and entities. A generative model gives flexible, custom analysis, such as company-specific categories or nuanced tone, returned as structured output. A common design redacts personal data with Azure Language before the text reaches the model.
Each approach does what it is best at.
Sort into groups

Azure Language or a model?

Exam skills 2.2.1

Choose the better fit for each text analysis requirement.

Log in to try this activity Interactive activities are free with an account.
Worked example

A hybrid pipeline

An insurer wants each claim email (1) stripped of personal data before analysis and (2) assigned one of the insurer's 12 claim types, with a one-sentence summary, stored as JSON.

Step (1) uses Azure Language PII detection, whose redacted text is then sent to a generative model for step (2), with a JSON schema whose enum lists the 12 claim types and a summary field. Sending the raw email to the model would expose personal data unnecessarily, and prebuilt Azure Language features do not know the insurer's claim types.

Check yourself

When is a generative model better than Azure Language for text analysis?

Show answer

For custom labels, fields and summaries that Azure Language's standard features do not provide.

Exam Tips

Remember for the exam

  • Generative models handle custom labels, fields and summaries.
  • Structured outputs with a JSON schema make model output parseable.
  • Combine: Azure Language for standard analysis and redaction, a model for custom analysis.