Using a model in Azure AI Foundry from a python project in VSCode
By Ivana Tilca · September 17, 2025 · 5 min read
This guide explains how to connect to a deployed model in Azure AI Foundry using Python and VSCode. It walks through creating a Python virtual environment, installing the required packages, and authenticating with Azure using the Azure CLI. Then, you create a Python script that connects to your Azure AI Foundry project endpoint, calls a deployed model such as GPT-4o, and sends a prompt using the chat completions API. The tutorial also shows where to find the project endpoint and the API version of the deployed model so you can run the script successfully from the terminal.
Requirements
VSCode
Python
Azure CLI
1 - Open VSCode and open up the folder using "File>Open Folder", in which you want to save your project.
2 - Select "Terminal>New terminal" in the top menu.
3- Create your python environment:
python3 -m venv env_name
4-Activate your environment
mac: source env_name/bin/activate
Win: .\env_name\Scripts\activate
5 - Install packages:
pip install openai azure-ai-projects azure-identity
6 - login into azure
az login
7 - create a python file <name>.py and include the following code.
from azure.ai.projects import AIProjectClient from azure.identity import DefaultAzureCredential
project = AIProjectClient( endpoint="https://your-foundry-resource-name.ai.azure.com/api/projects/project-name", credential=DefaultAzureCredential(), )
models = project.get_openai_client(api_version="2024-10-21") response = models.chat.completions.create( model="gpt-4o", messages=[ {"role": "system", "content": "You are a helpful writing assistant"}, {"role": "user", "content": "Write me a poem about flowers"}, ], )
print(response.choices[0].message.content)