Lesson 2 of 26 · see all lessons
Start here: the exam, Python basics and responsible AI (skill area 1, 40-45%)
AI models: how they work, choosing and deploying (skill area 1)
AI workloads: text, speech, vision and extraction (skill area 1)
Generative AI apps and agents in Foundry (skill area 2, 55-60%)
Text, speech, vision and extraction apps in Foundry (skill area 2)
- 17. Building a text analysis app
- 18. Building speech apps: spoken prompts and Azure Speech
- 19. Building vision apps: images in prompts and generated images
- 20. Extracting information with Content Understanding: documents and forms
- 21. Content Understanding for images, audio and video, and a small extraction app
Final review and four mock exams
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.
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 see | It 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.
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
labelthat 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:
- Install it once, from a terminal:
pip install azure-ai-projects. pip is Python's package installer. - Import it at the top of each program:
from azure.ai.projects import AIProjectClient.
Two packages appear in almost every Foundry app:
| Package | What it gives you |
|---|---|
azure-ai-projects | The Foundry SDK: AIProjectClient, which connects to a Foundry project. |
azure-identity | Sign-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.
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>
Match
Match each Azure term to what it does
Pick an item on the left, then its match on the right.
Signing in: Microsoft Entra ID or API keys
Exam depth: know it cold.
Foundry accepts two ways to prove who you are:
| Microsoft Entra ID | API 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.
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.
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 undertry. If it fails, Python jumps toexceptinstead of crashing.
Foundry SDK calls raise an HttpResponseError when the service says no. A wrong sign-in, for example, gives status code 401 (Unauthorized).
from azure.core.exceptions import HttpResponseErrortry:for deployment in project.deployments.list():print(deployment.name)except HttpResponseError as e:print(f"Status code: {e.status_code} ({e.reason})")
- 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.
You run the code above, but your account has no access to the project. What happens?
Pick one answer.
Read the code: connect to a Foundry project
import osfrom azure.identity import DefaultAzureCredentialfrom azure.ai.projects import AIProjectClientendpoint = 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)
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:
modelis the deployment name,inputis 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.
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)
project = AIProjectClient(
endpoint=endpoint,
credential=DefaultAzureCredential(),
)
openai = ____________
response = openai.responses.create(model=model, input=prompt)
Pick one answer.
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.
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.
A developer's app fails because the endpoint is wrong. Which value has the right shape for FOUNDRY_PROJECT_ENDPOINT?
Pick one answer.
Worth keeping
- Read Python by its shapes: lists use [ ], dictionaries use { : }, f-strings fill in {name}, indentation groups lines.
- pip install once, import in each file. Foundry apps use azure-ai-projects 2.x and azure-identity.
- Subscription > resource group > Foundry resource > project. Code reaches the project by its endpoint.
- Use Microsoft Entra ID via DefaultAzureCredential. AIProjectClient doesn't take API keys.
- 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.
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.
- [2]Microsoft Learn: Study guide for Exam AI-901: Microsoft Azure AI Fundamentals
- [6]Microsoft Learn: Exam scoring and score reports
- [17]Microsoft Learn: Quickstart: Set up Microsoft Foundry resources
- [18]Microsoft Learn: Quickstart: Get started with Microsoft Foundry SDK
- [19]Microsoft Learn: Azure AI Projects client library for Python
- [20]Microsoft Learn: DeploymentsOperations Class
- [21]Microsoft Learn: What is Azure Resource Manager?
- [22]Microsoft Learn: Azure resource management fundamentals
- [23]Microsoft Learn: Authentication and authorization in Microsoft Foundry
- [24]Microsoft Learn: Credential chains in the Azure Identity library for Python
- [25]Microsoft Learn: Foundry tools authentication and authorization using .NET