Using a model in Azure AI Foundry from a python project in VSCode

By Ivana Tilca · September 17, 2025 · 4 min read

A step-by-step guide to connecting a Python project in VS Code to a model you've deployed in Azure AI Foundry — setting up your environment, authenticating securely with the Azure CLI (no keys in your code), sending your first prompt, and fixing the errors you're most likely to hit.

Once you've deployed a model in Azure AI Foundry, the Playground is fun for about five minutes — then you want to call it from your own code. In this guide I'll connect a Python project in VS Code to a deployed model and send it a prompt, the clean way: using Azure identity for authentication so you never paste an API key into your source. If you've deployed a model already, this takes about ten minutes.

What you'll need

VS Code — your editor.

Python — 3.9 or newer.

Azure CLI — this is how we'll authenticate without keys.

A deployed model in an Azure AI Foundry project. (If you haven't deployed one yet, do that first — see my deployment walkthrough.)

Step 1 — Open your project folder in VS Code

Open VS Code and go to File → Open Folder, then pick (or create) the folder where your project will live. Working inside a dedicated folder keeps your virtual environment and files tidy.

Step 2 — Open a terminal

From the top menu, choose Terminal → New Terminal. This opens a terminal already pointed at your project folder.

Step 3 — Create a virtual environment

A virtual environment keeps this project's packages isolated from the rest of your system, so nothing you install here breaks another project:

Step 4 — Activate the environment

You'll know it worked when you see (env_name) at the start of your terminal prompt.

Step 5 — Install the packages

A quick tour of what each one does, because it's worth knowing:

`azure-ai-projects` — the Foundry SDK; it connects to your project and knows about your deployments.

`azure-identity` — handles authentication, including DefaultAzureCredential (more on that in a second).

`openai` — the client you'll actually use to send chat completions, since Foundry exposes an OpenAI-compatible interface.

Step 6 — Log in to Azure

This opens a browser, you sign in, and the CLI stores your credentials locally. This is the piece that lets you skip API keys entirely — your code will authenticate as you.

Step 7 — Write the code

Create a file, e.g. main.py, and add:

How the authentication actually works

The line doing the quiet magic is DefaultAzureCredential(). Instead of a hard-coded key, it walks through a chain of possible credentials until one works — your az login session on your laptop, a managed identity when the code runs in Azure, environment variables in CI, and so on. The payoff: the same code runs locally and in production with no secrets in your source and nothing to rotate or leak. This is the single best habit to build early when working with Azure AI.

For it to succeed, your signed-in identity needs a role like Azure AI Developer (or Cognitive Services OpenAI User) on the resource. If you get a permissions error, that's almost always the fix.

Where do I find the endpoint?

In your project in the portal, open Overview from the left menu. The value you want is labeled "Azure AI Foundry project endpoint" — copy that into the endpoint= argument.

Where do I get the API version?

Under My assets → Models + endpoints in the left menu, click the model you deployed. At the end of the endpoint URL you'll see api-version=.... In my case it was 2025-04-01-preview. Use the value that matches your deployment.

One thing worth calling out: the model= argument takes your deployment name, not the underlying model name. If you named your deployment gpt-5-main, that's what goes here — using the raw model name is the most common reason people get a "deployment not found" error.

Step 8 — Run it

If everything's wired up correctly, you'll get your model's response printed to the terminal. That's it — your Python app is now talking to your own Azure AI deployment.

Common errors and how to fix them

`DefaultAzureCredential failed to retrieve a token` — you're not logged in, or your session expired. Run az login again.

403 / permission denied — your identity is authenticated but lacks a role on the resource. Ask for the Azure AI Developer role.

Deployment / model not found — you passed the model name instead of the deployment name, or the api-version doesn't match your deployment.

Connection / endpoint errors — double-check the project endpoint from the Overview page; it's easy to grab the resource endpoint by mistake instead of the project endpoint.

Where to go next

You've now got the core loop: authenticate, connect, send a prompt, read the response. From here the natural next steps are streaming responses for a chat-like feel, adding tool/function calling so the model can act, and moving your endpoint and settings into environment variables for a cleaner setup. But the foundation is the same every time — and now it's yours.

Happy coding!