Lesson 1 of 2 Building a Chat Client with the Foundry SDK

Connecting to Foundry from Python with Keyless Authentication

This lesson explains how a Python application reaches Foundry through the Foundry SDK: the packages, the endpoint, keyless authentication and the client objects that every Foundry application uses.

Learning outcomes

After completing this lesson, you will be able to:

  • Identify the packages and the project endpoint a Foundry application needs.
  • Explain how DefaultAzureCredential provides keyless authentication.
  • Describe the roles of AIProjectClient and the OpenAI client it returns.
  • Diagnose common connection problems.

Packages and endpoint

The Foundry SDK for Python is the azure-ai-projects package. It works with azure-identity for authentication and with the openai package, which provides the model-calling API. The application needs the project endpoint, copied from the project's overview page in the portal, usually supplied through an environment variable or configuration file rather than hard-coded.

pip install azure-ai-projects azure-identity openai

Keyless authentication

Foundry supports API keys, but Microsoft recommends Microsoft Entra ID authentication. DefaultAzureCredential from azure-identity tries a sequence of credential sources and uses the first that works: environment variables, a managed identity when the code runs in Azure, and developer sign-ins such as the Azure CLI (az login) or Visual Studio Code. The same code therefore runs on a developer's laptop and in production without storing secrets. The signed-in identity needs a role such as Foundry User on the resource.

The following diagram shows how DefaultAzureCredential finds an identity.

Keyless authentication with DefaultAzureCredential. The credential tries a sequence of sources and uses the first that works: environment variables, a managed identity when the code runs in Azure, and developer sign-ins such as the Azure CLI or Visual Studio Code. It obtains a Microsoft Entra ID token for that identity, and Foundry accepts the request if the identity has a role such as Foundry User. The same code runs on a laptop and in production without storing a key.
The same code works on a laptop and in Azure.
True or false

Keyless authentication

Exam skills 2.1.3

Check four statements about authenticating a Python client to Foundry.

Log in to try this activity Interactive activities are free with an account.

The project and OpenAI clients

AIProjectClient represents the project. Its get_openai_client() method returns an OpenAI client that is configured for the project and authenticated with the same credential, so the application can call deployed models with the familiar OpenAI API.

import os
from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient

project = AIProjectClient(
    endpoint=os.environ["PROJECT_ENDPOINT"],   # https://<resource>.services.ai.azure.com/api/projects/<project>
    credential=DefaultAzureCredential(),
)
openai = project.get_openai_client()
Spot the bug

A client that cannot connect

Exam skills 2.1.3

Find the lines that stop this chat client from reaching its model.

Log in to try this activity Interactive activities are free with an account.

Common connection problems

Most problems when connecting to Foundry have simple causes. If DefaultAzureCredential cannot find any credential, the developer has usually not signed in with az login, or the code is running somewhere without a managed identity. If authentication succeeds but requests are refused with an access error, the signed-in identity does not have a role such as Foundry User on the resource.

If the project cannot be found, the endpoint is usually wrong, for example a resource endpoint used instead of the project endpoint. When calling a model, the application must use the deployment name that was chosen when the model was deployed, which may differ from the model's own name. Checking these four things, sign-in, role, endpoint and deployment name, resolves most first-run errors.

Troubleshooting

First-run errors

Exam skills 2.1.3

Find the fix for the most common errors when a client first connects to Foundry.

Log in to try this activity Interactive activities are free with an account.
Worked example

Removing a key from code

A developer's script contains the Foundry resource's API key in plain text, and the security team asks for it to be removed without breaking the script on developers' laptops or in Azure.

Replace the key with DefaultAzureCredential() when creating the client, sign in with az login on laptops, enable a managed identity for the Azure host, and assign the Foundry User role to each identity. Moving the key to an environment variable would keep a shared secret in use, which the security team's request is meant to avoid.

Check yourself

Which Python package is the Foundry SDK, and which class connects to a project?

Show answer

The azure-ai-projects package, with the AIProjectClient class.

Exam Tips

Remember for the exam

  • The Foundry SDK package is azure-ai-projects; AIProjectClient connects to the project endpoint.
  • DefaultAzureCredential uses developer sign-in locally and managed identity in Azure.
  • get_openai_client() returns an authenticated OpenAI client for the project.