02 First Model Call
Goal
Confirm your Foundry endpoint, authentication, and deployed model all work from Python.
Estimated time
10 minutes.
Official references
- Microsoft Foundry get-started code quickstart
- Microsoft Foundry SDKs and Endpoints
- Azure SDK for Python responses samples
Exercise
Run the example:
This example is using the Responses API. The script first connects to your Foundry project with AIProjectClient, then asks that project client for an OpenAI-compatible client, and finally sends a responses.create(...) call to your deployed model.
Example file
examples/01-model-call/model_call.py
import os
from azure.ai.projects import AIProjectClient
from azure.identity import DefaultAzureCredential
from dotenv import load_dotenv
def get_env(name: str, fallback: str | None = None) -> str:
value = os.getenv(name) or (os.getenv(fallback) if fallback else None)
if not value:
missing = f"{name}"
if fallback:
missing = f"{name} or {fallback}"
raise ValueError(f"Missing required environment variable: {missing}")
return value
def main() -> None:
load_dotenv()
project_endpoint = get_env("AZURE_AI_PROJECT_ENDPOINT", "PROJECT_ENDPOINT")
model_deployment_name = get_env(
"AZURE_AI_MODEL_DEPLOYMENT_NAME", "MODEL_DEPLOYMENT_NAME"
)
project = AIProjectClient(
endpoint=project_endpoint,
credential=DefaultAzureCredential(),
)
openai = project.get_openai_client()
response = openai.responses.create(
model=model_deployment_name,
input=(
"List three common use cases for PCR in life sciences in one sentence each."
),
)
print("Model response:")
print(response.output_text)
if __name__ == "__main__":
main()
What this lab demonstrates
- Create an
AIProjectClientwithDefaultAzureCredential. - Get the OpenAI-compatible client from the project.
- Send a
responses.create(...)request. - Print the generated answer.
What is happening under the hood
AZURE_AI_PROJECT_ENDPOINTpoints to your Foundry project endpoint, not directly to a raw model endpoint.AIProjectClienthandles the Foundry-side connection and authentication usingDefaultAzureCredential.project.get_openai_client()returns an OpenAI-compatible client that can call model features exposed through your Foundry project.responses.create(...)sends your input prompt to the deployed model named inAZURE_AI_MODEL_DEPLOYMENT_NAME.response.output_textprints the final text returned by the model.
This is a useful first lab because it proves the full path is working: local Python environment, Azure authentication, Foundry project endpoint, deployed model, and the OpenAI-compatible Responses API surface.
Expected result
You should see a short response describing common PCR use cases in life sciences.
Verification
- The script runs without authentication errors.
- The script returns model output.
- The project endpoint in
.envis valid.
Common issues
DefaultAzureCredential failed to retrieve a token: sign in again withaz login.404 Not Found: check your project endpoint format.- SDK mismatch errors: verify
azure-ai-projects>=2.0.0.