Lesson 14 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

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.

[2]

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:

  1. Create an AIProjectClient with your project endpoint and a credential.
  2. Call get_openai_client() to get an OpenAI client for the project.
  3. Call responses.create(...) with your deployment name as model.
  4. 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.

[18][19][51]

Rendering diagram…
Your script talks to one project endpoint. The project client hands you an OpenAI client, which calls the Responses API on your deployment.

[18]

Read the code: one question, one answer

python
import os
from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient
project = 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)
Output: one reply, such as how Fernhill's date-change policy works. The endpoint and deployment name come from environment variables, never from the code.

[18][19]

Exam depth: know it cold.

  • import os lets 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 like https://<resource>.services.ai.azure.com/api/projects/<project>.
  • DefaultAzureCredential() signs in with Microsoft Entra ID. On your laptop it uses your az login session.
  • project.get_openai_client() returns an OpenAI client that already knows your endpoint and sign-in.
  • openai.responses.create(model=..., input=...) sends the prompt. model is the deployment name.
  • response.output_text holds the reply as plain text.

[18][19]

In the code above, what does openai = project.get_openai_client() do?

Pick one answer.

[18][19]

Complete the code. Which value goes in the blank?
response = openai.responses.create(
    model=____,
    input="List three things to pack for Iceland.",
)

Pick one answer.

[19]

"Do you remember what I asked?" Model: "We've never met." Every API call starts fresh. Picture: Three-panel comic strip, clean cartoon line art with soft colors.

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.

WayHowWho 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.

[19][102]

python
import os
from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient
project = 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":
break
history.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})
Output: a chat in the terminal. Ask "Can I change my Rome dates?", then "And for Lisbon?", and the second answer knows you mean dates. Type quit to stop.

[18][102]

  • history starts 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": break is the exit word. .lower() makes QUIT work too.
  • The first append adds the user's turn to the list.
  • responses.create(..., input=history) sends the whole list, so the model sees every earlier turn.
  • The last append saves the reply as an assistant message. Without it, the next call wouldn't know what the model said.

[102]

Step through

How the history list grows

  1. system0

Start: only the system message.

Step 1 of 5

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 APIChat completions
CallUse openai.responses.create(model=..., input=...).Use openai.chat.completions.create(model=..., messages=[...]).
Read the replyUse response.output_text.Use response.choices[0].message.content.
Used forCurrent 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].

[18][51][99]

[17][18][19]

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.

[17][19]

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.

[19]

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.",
    ____,
)

Pick one answer.

[102]

You start a new script that calls a Foundry deployment. Which object do you create first?

Pick one answer.

[18]

FlashcardsIndex cardsFlashcards ⚡

Card 1 of 14

[18][19][102]

Worth keeping

  1. Four moves: AIProjectClient, get_openai_client(), responses.create(), output_text.
  2. model= takes the deployment name; the endpoint is the project endpoint.
  3. The model has no memory: resend the history or chain with previous_response_id.
  4. Sign-in is Entra ID only: az login plus a role such as Foundry User.
  5. 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.

  1. [2]Microsoft Learn: Study guide for Exam AI-901: Microsoft Azure AI Fundamentals
  2. [17]Microsoft Learn: Quickstart: Set up Microsoft Foundry resources
  3. [18]Microsoft Learn: Quickstart: Get started with Microsoft Foundry SDK
  4. [19]Microsoft Learn: Azure AI Projects client library for Python
  5. [51]Microsoft Learn: What is Microsoft Foundry?
  6. [99]Microsoft Learn: Deploy Microsoft Foundry Models in the Foundry portal
  7. [102]Microsoft Learn: Use the Azure OpenAI Responses API