Reference · How-to · ~7 min

How to sketch chat on Azure OpenAI

Last updated

Call your Azure deployment with the same messages JSON as OpenAI — different URL and auth.

Call your Azure deployment with the same messages JSON as OpenAI — different URL and auth.

#Steps

1. **Create Azure OpenAI resource** — note resource name, region, deployment name

2. **Deploy model** — e.g. `gpt-4o-mini` with a custom deployment id

3. **Set env vars** — `AZURE_OPENAI_ENDPOINT`, `AZURE_OPENAI_API_KEY`, `AZURE_OPENAI_DEPLOYMENT`

4. **POST chat completions** — deployment name in URL path, `api-version` query param

5. **Stream optional** — `stream: true` for SSE tokens in UI

6. **Rotate keys** — use Azure Key Vault; never commit keys to repo

#Sketch

from openai import AzureOpenAI
client = AzureOpenAI(
    azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
    api_key=os.environ["AZURE_OPENAI_API_KEY"],
    api_version="2024-02-15-preview",
)
resp = client.chat.completions.create(
    model=os.environ["AZURE_OPENAI_DEPLOYMENT"],
    messages=[{"role": "user", "content": "Hello from Azure"}],
)

#Watch out

`model=` is your **deployment name**, not always the public OpenAI model string.

**Try the lessons:** `llm-api-hello` · `streaming-responses` in Lane D · [API: Azure OpenAI chat](/reference/api/azure-openai-chat)