Your profile

One profile for every MSDEVBUILD site. Only you can see it, and the site owner if you write in.

Lesson 18 of 30
❮Prev Next❯

Build a Chat App with the Foundry SDK

Module 2: Microsoft Foundry ⏱ 9 min read Updated

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    30-second answer

    A lightweight chat client uses the Foundry SDK (azure-ai-projects) in a few lines of Python. You create an AIProjectClient with your project endpoint (read from an environment variable) and DefaultAzureCredential, call get_openai_client(), then send prompts with responses.create, passing your deployment name as model. To keep a multi-turn chat, you create a conversation and pass its id each turn, or pass previous_response_id.

    Simulate it » No Azure account needed

    Exam objective: 2.1.3 Create a lightweight chat client application by using the Foundry SDK. (Skills measured as of April 15, 2026)

    Simulations

    Feel it before you read it. No account needed, nothing leaves this page.

    Azure portal: Build a chat client with the Foundry SDK Copy the endpoint, view the code and run a chat client.
    </> Embed

    Embed this simulation

    Free for trainers, teachers and bloggers. Paste this into your page:

    Loading the portal…

    Copy the <strong>project endpoint</strong> from the project overview.

    👆 Click the glowing item in the portal to continue

    Read the steps as text
    1. Copy the project endpoint from the project overview.
    2. In a terminal, install the SDK and sign in.
    3. The whole app: create the client, get an OpenAI-compatible client, call responses.create with the deployment name, and pass previous_response_id to remember earlier turns.
    4. Run it. The second question works because the app sent the first response's id.

    🧪 Simulation for learning. Screens are simplified versions of the Azure portal; names, prices and IDs are examples. Nothing here touches real Azure.

    😟 THE PROBLEM

    The playground works, but customers need it inside your app.

    😊 THE SOLUTION

    A 15-line Python client with the Foundry SDK.

    • Secrets in code✓ DefaultAzureCredential
    • Which URL to call?✓ The project endpoint
    • Forgets the last question✓ previous_response_id
    🛠️ Solved with: Foundry SDK

    Start with a question

    The model works in the playground. How do you put it inside your own app?

    The playground is great for testing, but your customers won’t log in to ai.azure.com. They’ll use your website, your WhatsApp bot or your internal tool. Somewhere in that app, a few lines of code have to send the question to your model and show the answer.

    That’s what the Foundry SDK is for. With about fifteen lines of Python you get a working chat client that remembers the conversation.

    Explain it like I’m 10

    The technical explanation

    You need four things in place before the code runs:

    1. A Foundry project with a deployed model (for example a deployment named gpt-4.1-mini).
    2. The project endpoint, from the project’s overview page, saved in an environment variable.
    3. The packages: pip install "azure-ai-projects>=2.3.0" azure-identity
    4. A signed-in identity with the Foundry User role. Locally that’s az login; in Azure it’s a managed identity.
    export FOUNDRY_PROJECT_ENDPOINT="https://<resource-name>.services.ai.azure.com/api/projects/<project-name>"
    az login

    A one-shot question

    import os
    from azure.identity import DefaultAzureCredential
    from azure.ai.projects import AIProjectClient
    
    # Connect to the project with Microsoft Entra ID (no API key)
    project = AIProjectClient(
        endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
        credential=DefaultAzureCredential(),
    )
    
    # An OpenAI-compatible client that already points at your project
    openai = project.get_openai_client()
    
    response = openai.responses.create(
        model="gpt-4.1-mini",  # the DEPLOYMENT name, not the catalog name
        instructions="You are a friendly travel assistant. Keep answers short.",
        input="What's a good time of year to visit Munnar?",
    )
    print(response.output_text)

    instructions is the system prompt. You can also pass input as a list of messages with system and user roles if you prefer.

    Making it multi-turn

    On its own, each call forgets the last one. Create a conversation once and pass its id on every turn so the service keeps the history for you:

    conversation = openai.conversations.create()
    
    while True:
        question = input("You: ")
        if question.lower() in ("quit", "exit"):
            break
        response = openai.responses.create(
            model="gpt-4.1-mini",
            conversation=conversation.id,  # earlier turns are included automatically
            instructions="You are a friendly travel assistant. Keep answers short.",
            input=question,
        )
        print("Assistant:", response.output_text)

    The other option is to chain responses: store response.id and pass it as previous_response_id= on the next call.

    1. 1 Sign in

      DefaultAzureCredential gets an Entra ID token.

    2. 2 Call

      responses.create sends the input to your deployment through the project endpoint.

    3. 3 Remember

      The conversation id adds earlier turns.

    4. 4 Reply

      Your app prints response.output_text.

    What happens on each turn
    Architect level Why Entra ID instead of keys

    Keys are shared secrets: anyone who copies one has full access until it’s rotated. With DefaultAzureCredential, the same code signs in as you on your laptop (Azure CLI), as a managed identity in Azure App Service or Container Apps, or as a workload identity in a pipeline, and access is controlled by the Foundry User role. Tokens expire on their own, and removing a role assignment cuts access instantly.

    Real-world business scenario

    Travel

    Tailspin Travel (fictional)

    The problem
    The team had tested a trip-planning assistant in the playground, but the web app kept forgetting what the traveller said one message earlier.
    What they used
    A Python backend using AIProjectClient with DefaultAzureCredential and a managed identity, calling responses.create with one conversation per traveller session.
    Why that choice
    The conversation keeps history on the service side, and Entra ID removed the API key the developers had been passing around in chat.
    The result
    Travellers can ask follow-ups like "and what about hotels there?" and get answers that remember the destination.

    When to use it and when not to

    ✓ Use it when…

    • You're building your own app, script or backend that needs to call a Foundry model.
    • You want Entra ID sign-in and role-based access instead of keys.
    • You need multi-turn chat with history kept by the service.

    ✕ Think twice when…

    • You only want to test prompts. The playground is quicker.
    • You need tools like file search or web search. Build an agent and call it with the same SDK.

    ↔ Alternatives to consider

    • Foundry agents when the assistant needs tools and fixed instructions stored in Foundry.
    • Microsoft Agent Framework for larger code-first agent apps.
    Ready for more?The full lesson adds the technical explanation, when to use it, interview questions and revision notes.

    Test yourself with exercises

    5 original practice questions in the AI-901 style. Pick your answers, then check them. You will see why each option is right or wrong. Score 75% or more and this topic is marked complete.

    Question 1

    Question 1 of 5

    You are writing a Python chat client by using the Foundry SDK. The client must authenticate to the Foundry project without storing any secrets in code or configuration. Which credential should you pass to AIProjectClient?

    Question 2

    Question 2 of 5

    A developer has created an AIProjectClient object named project.

    Which method should the developer call to obtain a client that can send prompts to a deployed model by using responses.create?

    Question 3

    Question 3 of 5 · Select 2

    A chat application must remember the earlier messages in a conversation when it calls responses.create. Which TWO approaches meet this requirement?

    Question 4

    Question 4 of 5

    In a Foundry SDK chat client, which property of the object returned by responses.create contains the text of the model's reply?

    Question 5

    Question 5 of 5

    A chat client runs successfully on one developer's computer but returns an authorization error on another developer's computer. Both use the same code and the same project endpoint. What is the most likely cause?

    Interview preparation

    Say your answer out loud first, then open the model answer. Interviewers care more about your reasoning than the exact words.

    Beginner What do you need before your Python app can chat with a Foundry model?

    Model answer

    A Foundry project with a deployed model, the project endpoint, the azure-ai-projects and azure-identity packages, and a signed-in identity with the Foundry User role. Locally I run az login so DefaultAzureCredential can pick it up.

    Interviewer follows up

    Why not just use an API key?

    You

    The AIProjectClient only supports Microsoft Entra ID. That's a good thing: no secret sits in my code or config, and access can be removed by changing a role.

    Intermediate What does the model parameter in responses.create refer to?

    Model answer

    The deployment name I chose in Foundry, not the catalog model name. If I deployed gpt-4.1-mini as "chat-prod", I pass model="chat-prod".

    Experienced developer How do you make the chat remember earlier messages?

    Model answer

    Models are stateless, so history has to be supplied. With the Responses API I either create a conversation once and pass conversation=conversation.id on every call, or pass previous_response_id=response.id from the last turn. Both let the service send the earlier turns for me, instead of me rebuilding the message list.

    Scenario The app works on your laptop but fails with an authorisation error on a colleague's machine. What would you check?

    Model answer

    That they've run az login with an account that has the Foundry User role on the project, and that FOUNDRY_PROJECT_ENDPOINT is set to the right project. DefaultAzureCredential finds their identity, but the role assignment is what grants access.

    Quick revision

    Key points

    • Install azure-ai-projects and azure-identity.
    • Read the project endpoint from an environment variable such as FOUNDRY_PROJECT_ENDPOINT.
    • AIProjectClient plus DefaultAzureCredential signs in with Microsoft Entra ID (no keys).
    • get_openai_client() then responses.create, with model set to the deployment name, returns output_text.
    • Multi-turn chat uses a conversation id or previous_response_id.

    Important terminology

    Foundry SDK
    The Azure AI Projects client library (azure-ai-projects) used to work with a Foundry project from code.
    AIProjectClient
    The SDK class that connects to a Foundry project using its endpoint and a credential.
    DefaultAzureCredential
    An azure-identity class that signs in with Microsoft Entra ID, using az login locally or a managed identity in Azure.
    get_openai_client()
    An AIProjectClient method that returns an OpenAI-compatible client already pointed at your project.
    Responses API
    The API called with responses.create to send input to a model and get output_text back.
    Conversation
    A server-side object that stores chat history so each new request remembers earlier turns.

    Common mistakes

    • Passing the catalog model name instead of the deployment name.
    • Expecting the model to remember the last message without a conversation or previous_response_id.

    Exam tips

    • "Authenticate the Foundry SDK without storing secrets" = DefaultAzureCredential (Entra ID).
    • "Read the model's reply" = response.output_text.
    • "Keep context across turns" = conversation or previous_response_id.

    Five quick revision questions

    1. 1.Which package is the Foundry SDK?Show

      azure-ai-projects.

    2. 2.Which credential class does the sample use?Show

      DefaultAzureCredential.

    3. 3.Which method returns an OpenAI-compatible client?Show

      get_openai_client().

    4. 4.Which property holds the reply text?Show

      output_text.

    5. 5.Name two ways to keep a multi-turn chat.Show

      Pass a conversation id, or pass previous_response_id.

    Official references

    Finished this lesson? Tick it off. Your progress is saved in this browser.

    Next up: Create and Test an Agent in the Foundry Portal

    Studying with a friend? Send them this lesson

    It's free and needs no sign-up. Their progress saves in their own browser.

    Lesson done? Make it stick with a quick gameSort It, Match the Pairs, Lightning Round and Who Am I? Earn XP and badges. Play ❯

    Install MSDEVBUILD Azure

    Free · no account · works offline

    Save the whole course for offline ›

    Hi, I’m your Nimbus

    Your study buddy. Pick what we do next.

    MSDEVBUILD · Multi-Stack Dev Build

    Sign in to MSDEVBUILD

    One account for Azure certifications and the blog: sign in here and you are signed in on both. Your XP, streak, practice deck and certificate stay in sync on every device. Optional: everything works without an account.