4.1.3. Building a Chat Client with the Foundry SDK
💡 First Principle: A lightweight chat client is just the two-client pattern in code: create an AIProjectClient with your project endpoint and credential, ask it for an OpenAI-compatible client, then send input and print the response. Recognizing these three moves lets you read almost any Foundry chat snippet on the exam.
Here's the minimal current pattern in Python. You don't need to memorize it line-for-line, but you should recognize each step:
from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient
# 1. Project client: setup & configuration
project = AIProjectClient(
endpoint="https://<resource>.services.ai.azure.com/api/projects/<project>",
credential=DefaultAzureCredential(),
)
# 2. Get an OpenAI-compatible client to do the actual work
openai_client = project.get_openai_client()
# 3. Send input, read the response
response = openai_client.responses.create(
model="gpt-5.2", # your deployment name
input="What is the capital of France?",
)
print(response.output_text)
DefaultAzureCredential is worth knowing by name: it's the standard way to authenticate without hard-coding keys, picking up your signed-in identity (for example, after az login). This keeps secrets out of code — a privacy/security best practice from Phase 2 showing up in real implementation.
⚠️ Exam Trap: Older/Classic samples authenticate with a connection string (AIProjectClient.from_connection_string(...)). Current Foundry projects use an endpoint plus DefaultAzureCredential. If a snippet uses a connection string, it's the older hub-based pattern.
Reflection Question: Why is using DefaultAzureCredential preferable to pasting an API key directly into your chat client code? Connect this to a responsible-AI principle.