Lesson 2 of 26 · see all lessons
  1. Start here: the exam, Python basics and responsible AI (skill area 1, 40-45%)

    1. 1. Start here: the AI-901 exam, the plan and what you already know
    2. 2. The Python and Azure basics AI-901 expects
    3. 3. Responsible AI: the six principles in practice
  2. AI models: how they work, choosing and deploying (skill area 1)

    1. 4. How generative AI models work
    2. 5. Choosing the right AI model for the job
    3. 6. Deploying a model: deployment options and configuration parameters
  3. AI workloads: text, speech, vision and extraction (skill area 1)

    1. 7. Common AI workloads: from chat and agents to vision and extraction
    2. 8. Text analysis: key phrases, entities, sentiment and summaries
    3. 9. Speech: recognition and synthesis
    4. 10. Computer vision and image generation
    5. 11. Extracting information from documents, images, audio and video
  4. Generative AI apps and agents in Foundry (skill area 2, 55-60%)

    1. 12. Writing system and user prompts
    2. 13. Deploying a model and chatting with it in the Foundry portal
    3. 14. Building a lightweight chat client with the Foundry SDK
    4. 15. Creating and testing a single agent in the Foundry portal
    5. 16. Building a lightweight client app for an agent
  5. Text, speech, vision and extraction apps in Foundry (skill area 2)

    1. 17. Building a text analysis app
    2. 18. Building speech apps: spoken prompts and Azure Speech
    3. 19. Building vision apps: images in prompts and generated images
    4. 20. Extracting information with Content Understanding: documents and forms
    5. 21. Content Understanding for images, audio and video, and a small extraction app
  6. Final review and four mock exams

    1. 22. Final review and exam day: every decision on one page
    2. 23. Mock exam 1
    3. 24. Mock exam 2
    4. 25. Mock exam 3
    5. 26. Mock exam 4
See the leaderboard

18 min

The Python and Azure basics AI-901 expects

Read the short Python and Azure setup that every Foundry app in this course uses: endpoints, credentials, packages and clients.

By the end of this lesson you can

  • Read short Python: variables, f-strings, lists, dictionaries, functions, loops and if statements
  • Explain what pip install, import, with and try/except do in a Foundry app
  • Trace a Foundry project from subscription to endpoint, and keep secrets out of code
  • Read a Foundry SDK program line by line and complete a missing line

From earlier lessons · 1 of 2

Which AI-901 skill area carries the most weight?

Pick one answer.

From earlier lessons · 2 of 2

For this statement, select Yes if it is true. Otherwise, select No. "A wrong answer on AI-901 costs you points."

Pick one answer.

Fernhill's IT contractor emails you a 20-line Python file. "This connects us to Microsoft Foundry," she writes. "Just set the endpoint and run it." You open it and see os.environ, DefaultAzureCredential and get_openai_client(). Before you run anything, you want to know what each line does. So does the exam.

[2]

The Python you must read

Exam depth: know it cold.

Python is the programming language every AI-901 code item uses. You need to read nine building blocks:

You seeIt means
city = "Lisbon"A variable: a name that holds a value.
"Lisbon"A string: text in quotes.
f"Trip to {city}"An f-string: text with a variable filled in at {}.
["Lisbon", "Kyoto"]A list: items in order, in square brackets.
{"Lisbon": 850}A dictionary: key-value pairs in curly braces.
def label(city):A function: a named, reusable step. return hands back a result.
for city in trips:A for loop: runs the indented lines once per item.
if price <= budget:An if statement: runs the indented lines only when the test is true.
print(...)Shows a value on screen.

Indentation matters in Python. The indented lines under def, for or if belong to it.

python
trips = ["Lisbon", "Kyoto", "Quito"]
prices = {"Lisbon": 850, "Kyoto": 2100, "Quito": 1600}
def label(city, budget):
if prices[city] <= budget:
return f"{city}: within budget"
return f"{city}: over budget"
for city in trips:
print(label(city, 1700))

Line by line:

  • Line 1 makes a list of three city names.
  • Line 2 makes a dictionary. Each city is a key, and its price is the value.
  • Line 4 defines a function called label that takes a city and a budget.
  • Line 5 looks up the city's price with prices[city] and compares it to the budget.
  • Lines 6-7 return one of two f-strings. If line 6 runs, the function stops there.
  • Lines 9-10 loop over the list and print the label for each city, with a budget of 1700.

The output is three lines: Lisbon within budget, Kyoto over budget, Quito within budget.

In the code above, you change line 10 to print(label(city, 900)). How many lines say "within budget"?

Pick one answer.

Packages: pip install and import

Exam depth: know it cold.

A package is ready-made code someone else wrote. You use it in two steps:

  1. Install it once, from a terminal: pip install azure-ai-projects. pip is Python's package installer.
  2. Import it at the top of each program: from azure.ai.projects import AIProjectClient.

Two packages appear in almost every Foundry app:

PackageWhat it gives you
azure-ai-projectsThe Foundry SDK: AIProjectClient, which connects to a Foundry project.
azure-identitySign-in classes, such as DefaultAzureCredential.

An SDK (software development kit) is a package that wraps a service so you call it with Python instead of raw web requests. The Foundry SDK needs Python 3.10 or later.

[18][19]

[18]

Azure basics: from subscription to endpoint

Exam depth: know it cold.

Everything you build sits inside a few nested containers:

  • Subscription: the billing and management container. Your costs land here.
  • Resource group: a container for related resources. Delete the group and everything in it goes too.
  • Resource: any item you manage in Azure. A Foundry resource is the one that hosts your AI work.
  • Project: your workspace inside the Foundry resource. It holds models, agents and other assets.
  • Region: the Azure location you pick when you create them, such as East US.

Your code reaches the project through its endpoint, a web address shown on the project's home page:

https://<resource-name>.services.ai.azure.com/api/projects/<project-name>

[17][21][22]

Rendering diagram…
Containers from outside in. Your app talks to the project endpoint and calls a model by its deployment name.

[17]

Match

Match each Azure term to what it does

Pick an item on the left, then its match on the right.

[17][21][22]

Signing in: Microsoft Entra ID or API keys

Exam depth: know it cold.

Foundry accepts two ways to prove who you are:

Microsoft Entra IDAPI key
What it is.Sign-in with an identity, using short-lived tokens.One static secret per resource.
Access control.Fine-grained roles per person or app.All-or-nothing for that resource.
Microsoft's advice.Use it for production.Quick prototypes only.

Microsoft Entra ID is Microsoft's identity service, the one behind your work sign-in. It's also called keyless sign-in, because no secret sits in your code.

In Python you use DefaultAzureCredential from azure-identity. It tries several sign-in methods in turn and uses the first that works. On your laptop, that's usually the account you signed in with by running az login in the Azure CLI. In Azure, it's usually the app's managed identity, an identity Azure gives the app itself.

The Foundry SDK's AIProjectClient supports Entra ID only.

[19][23][24]

DefaultAzureCredential tries sign-in methods in order and stops at the first that worksEnvironment variables (service principal)Managed identity (app running in Azure)Azure CLI (az login on your laptop)Token from Microsoft Entra IDFoundry project accepts the callMost common on a developerlaptop

[24]

[19][25]

Fernhill will run its support app in Azure. Security says the app must not store any secret, and access must be limited per app. How should the app authenticate to Foundry?

Pick one answer.

[23][24]

API keys in code always get found. Usually by the wrong person. Picture: Three-panel comic strip, clean cartoon line art with soft colours.

with blocks and try/except

Exam depth: recognize it.

Two more patterns appear in Microsoft's samples.

  • with ... as name: opens something, such as a client, and closes it cleanly when the indented block ends.
  • try: / except: runs risky code under try. If it fails, Python jumps to except instead of crashing.

Foundry SDK calls raise an HttpResponseError when the service says no. A wrong sign-in, for example, gives status code 401 (Unauthorized).

[19]

python
from azure.core.exceptions import HttpResponseError
try:
for deployment in project.deployments.list():
print(deployment.name)
except HttpResponseError as e:
print(f"Status code: {e.status_code} ({e.reason})")

[19]

  • Line 1 imports the error type from azure-core, a package the Azure SDKs share.
  • Lines 3-5 list the models deployed in the project and print each deployment's name.
  • Line 6 catches the error if the service refuses, and names it e.
  • Line 7 prints the status code and reason, such as 401 (Unauthorized), instead of crashing.

[19][20]

You run the code above, but your account has no access to the project. What happens?

Pick one answer.

[19]

Read the code: connect to a Foundry project

python
import os
from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient
endpoint = os.environ["FOUNDRY_PROJECT_ENDPOINT"]
model = os.environ["FOUNDRY_MODEL_NAME"]
project = AIProjectClient(
endpoint=endpoint,
credential=DefaultAzureCredential(),
)
for deployment in project.deployments.list():
print(deployment.name)
openai = project.get_openai_client()
response = openai.responses.create(
model=model,
input="Suggest one day trip from Lisbon.",
)
print(response.output_text)

[18][19]

Line by line:

  • Lines 1-3 import os (to read environment variables), the credential, and the Foundry SDK client.
  • Line 5 reads the project endpoint from an environment variable.
  • Line 6 reads the deployment name, the name you gave the model when you deployed it.
  • Lines 8-11 create the project client. It needs two things: where (endpoint) and who (credential).
  • Lines 13-14 list the project's deployed models and print each name.
  • Line 16 gets an OpenAI client from the project. It sends prompts to deployed models.
  • Lines 17-20 call the Responses API: model is the deployment name, input is the prompt.
  • Line 21 prints the model's reply, which sits in response.output_text.

The quickstart pastes the endpoint as a placeholder string. Reading it from an environment variable, as here, keeps it out of your code.

[18][19][20]

Complete the code so the app can send prompts to a deployed model.
project = AIProjectClient(
    endpoint=endpoint,
    credential=DefaultAzureCredential(),
)
openai = ____________
response = openai.responses.create(model=model, input=prompt)

Pick one answer.

[18]

Exam-style practice

For this statement, select Yes if it is true. Otherwise, select No. "You can create an AIProjectClient by passing the Foundry resource's API key as the credential."

Pick one answer.

[19]

Fernhill deployed a model from the catalog and named the deployment fernhill-chat. A call with model="gpt-5-mini" fails. What should you change?

Pick one answer.

[17][19]

FlashcardsIndex cardsFlashcards ⚡

Card 1 of 17

[17][18][19][21][22][23][24]

Worth keeping

  1. Read Python by its shapes: lists use [ ], dictionaries use { : }, f-strings fill in {name}, indentation groups lines.
  2. pip install once, import in each file. Foundry apps use azure-ai-projects 2.x and azure-identity.
  3. Subscription > resource group > Foundry resource > project. Code reaches the project by its endpoint.
  4. Use Microsoft Entra ID via DefaultAzureCredential. AIProjectClient doesn't take API keys.
  5. Keep endpoints and secrets in environment variables; call models by deployment name.

On the job

When a colleague says "the AI app won't connect", check three things in order. Is FOUNDRY_PROJECT_ENDPOINT set and shaped like a project endpoint? Did they run az login, or does the app have a managed identity with a Foundry role? Does model= match a deployment name in the project? Most connection problems are one of those three.

[17]

Your code can now reach a model. But should Fernhill's model decide which customers get refunds, or read medical notes from travel forms? Lesson 3 covers the six principles Microsoft uses to answer that.

Leaderboard

Loading…

50 XP for each lesson you finish, 10 for each right answer. Names show only for learners who turn on Show me on leaderboards.

Embed this practice on your site

Free for classrooms, blogs and study groups. Paste this into any page that accepts HTML.

Report a copyright problem with this lesson

Sources for this lesson (11)

Each fact in the lesson is marked with the number of the page it comes from.

  1. [2]Microsoft Learn: Study guide for Exam AI-901: Microsoft Azure AI Fundamentals
  2. [6]Microsoft Learn: Exam scoring and score reports
  3. [17]Microsoft Learn: Quickstart: Set up Microsoft Foundry resources
  4. [18]Microsoft Learn: Quickstart: Get started with Microsoft Foundry SDK
  5. [19]Microsoft Learn: Azure AI Projects client library for Python
  6. [20]Microsoft Learn: DeploymentsOperations Class
  7. [21]Microsoft Learn: What is Azure Resource Manager?
  8. [22]Microsoft Learn: Azure resource management fundamentals
  9. [23]Microsoft Learn: Authentication and authorization in Microsoft Foundry
  10. [24]Microsoft Learn: Credential chains in the Azure Identity library for Python
  11. [25]Microsoft Learn: Foundry tools authentication and authorization using .NET