Lesson 14 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
19 min
Building a lightweight chat client with the Foundry SDK
Read and complete a short Python chat app that calls your deployed model through the Foundry SDK.
By the end of this lesson you can
- Create an AIProjectClient and get an OpenAI client from it
- Send a prompt with the Responses API and print the reply
- Keep chat history across turns in a loop
- Spot the common mistakes: deployment name, endpoint and role
From earlier lessons · 1 of 2
You deployed gpt-5-mini under the deployment name support-bot. Which value must your code send as the model?
Pick one answer.
From earlier lessons · 2 of 2
In the new Foundry portal, where do you copy the project endpoint from?
Pick one answer.
Fernhill's support staff loved the playground, but they won't log in to the Foundry portal to answer emails. The IT lead wants a small Python script that runs in a terminal. Staff type a question, the model answers, and the chat remembers what was said.
The flow: four moves
Exam depth: know it cold.
The Foundry SDK for Python is the package azure-ai-projects, version 2.x. Install it with pip install "azure-ai-projects>=2.3.0" azure-identity, then sign in with az login.
Every chat app makes the same four moves:
- Create an AIProjectClient with your project endpoint and a credential.
- Call
get_openai_client()to get an OpenAI client for the project. - Call
responses.create(...)with your deployment name asmodel. - Print
response.output_text.
The Responses API is the current way to get text from a model: you send input, you get a response back.
Older material uses azure-ai-inference or version 1.x of azure-ai-projects. That code doesn't work with 2.x.
Read the code: one question, one answer
import osfrom azure.identity import DefaultAzureCredentialfrom azure.ai.projects import AIProjectClientproject = AIProjectClient(endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],credential=DefaultAzureCredential(),)openai = project.get_openai_client()response = openai.responses.create(model=os.environ["FOUNDRY_MODEL_NAME"],input="Can I change the dates of my Rome trip?",)print(response.output_text)
Exam depth: know it cold.
import oslets the script read environment variables.- The two imports bring in the credential class and the project client.
AIProjectClient(endpoint=..., credential=...)connects to your project. The endpoint looks likehttps://<resource>.services.ai.azure.com/api/projects/<project>.DefaultAzureCredential()signs in with Microsoft Entra ID. On your laptop it uses youraz loginsession.project.get_openai_client()returns an OpenAI client that already knows your endpoint and sign-in.openai.responses.create(model=..., input=...)sends the prompt.modelis the deployment name.response.output_textholds the reply as plain text.
Complete the code. Which value goes in the blank?
response = openai.responses.create(
model=____,
input="List three things to pack for Iceland.",
)
response = openai.responses.create(
model=____,
input="List three things to pack for Iceland.",
)
Pick one answer.
Keep the conversation going
Exam depth: know it cold.
A model remembers nothing between calls. To make a chat, each call must carry the earlier turns. The Responses API gives you two ways in this lesson.
| Way | How | Who stores the history |
|---|---|---|
| Send the history yourself. | Pass a list of messages as input, adding each turn. | Your code. |
| Chain responses. | Pass previous_response_id=response.id on the next call. | The service. |
The third way, a conversation object, comes with agents in lesson 16.
The loop below keeps its own list. It starts with a system message, as in lesson 12.
import osfrom azure.identity import DefaultAzureCredentialfrom azure.ai.projects import AIProjectClientproject = AIProjectClient(endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],credential=DefaultAzureCredential(),)openai = project.get_openai_client()history = [{"role": "system", "content": "You are Fernhill Travel's support assistant. Reply in under 80 words."}]while True:question = input("You: ")if question.lower() == "quit":breakhistory.append({"role": "user", "content": question})response = openai.responses.create(model=os.environ["FOUNDRY_MODEL_NAME"],input=history,)print("Assistant:", response.output_text)history.append({"role": "assistant", "content": response.output_text})
historystarts as a list with one system message: the rules for every reply.while True:repeats until something breaks out of it.input("You: ")waits for the user to type a line.if question.lower() == "quit": breakis the exit word..lower()makes QUIT work too.- The first
appendadds the user's turn to the list. responses.create(..., input=history)sends the whole list, so the model sees every earlier turn.- The last
appendsaves the reply as an assistant message. Without it, the next call wouldn't know what the model said.
Step through
How the history list grows
- system0
Start: only the system message.
A teammate's chat loop forgets every earlier answer, but remembers earlier questions. Which line is missing?
Pick one answer.
Responses API or chat completions?
Exam depth: recognize it.
| Responses API | Chat completions | |
|---|---|---|
| Call | Use openai.responses.create(model=..., input=...). | Use openai.chat.completions.create(model=..., messages=[...]). |
| Read the reply | Use response.output_text. | Use response.choices[0].message.content. |
| Used for | Current Foundry quickstarts, and agents. | Older code, and some partner models. |
Both take the deployment name as model. Both take a list of role-tagged messages.
Decision rule: new code in Foundry uses the Responses API. Recognize chat completions when you see messages= and choices[0].
A developer runs the chat app. az login worked and the endpoint is right, but the call is refused with a permission error. Her teammate's account works. What should you do?
Pick one answer.
Exam-style practice
For the statement, select Yes if it is true. Otherwise, select No. "You can create an AIProjectClient with an API key instead of a Microsoft Entra ID credential."
Pick one answer.
Complete the code so the second call continues the first one's chat, with the service storing the history.
first = openai.responses.create(model=dep, input="Plan 3 days in Porto.")
second = openai.responses.create(
model=dep,
input="Make day 2 rainy-day friendly.",
____,
)
first = openai.responses.create(model=dep, input="Plan 3 days in Porto.")
second = openai.responses.create(
model=dep,
input="Make day 2 rainy-day friendly.",
____,
)
Pick one answer.
You start a new script that calls a Foundry deployment. Which object do you create first?
Pick one answer.
Worth keeping
- Four moves: AIProjectClient, get_openai_client(), responses.create(), output_text.
- model= takes the deployment name; the endpoint is the project endpoint.
- The model has no memory: resend the history or chain with previous_response_id.
- Sign-in is Entra ID only: az login plus a role such as Foundry User.
- Chat completions uses messages= and choices[0]; recognize it, but new code uses Responses.
On the job
Keep endpoints and deployment names in environment variables, as the samples do. Then the same script runs against a test project and a live project with no code change. Long chats grow the history list and the token bill, so cap how many turns you resend.
The chat app answers questions, but it can't do anything. It can't search Fernhill's policy files or check a booking. Next lesson you give a model instructions and tools, and it becomes an agent.
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 (7)
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
- [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
- [51]Microsoft Learn: What is Microsoft Foundry?
- [99]Microsoft Learn: Deploy Microsoft Foundry Models in the Foundry portal
- [102]Microsoft Learn: Use the Azure OpenAI Responses API