Invoking Foundry APIs Γ’β¬β Making AI Calls from Code
Learn how to call deployed AI models from code using the OpenAI SDK and Azure Foundry Tools SDKs. Understand authentication, the Responses API, and purpose-built service SDKs.
βOnce you deploy a model in Microsoft Foundry, it is available via an API endpoint. You could call this endpoint manually with HTTP requests Γ’β¬β but that means managing authentication headers, serialising request bodies, and parsing JSON responses. SDKs abstract all of this. The OpenAI SDK works with any model that supports the OpenAI-compatible API, which covers most language models in Foundry. The Foundry Tools SDKs are purpose-built for specific Azure AI services Γ’β¬β the Language SDK for text analysis, the Speech SDK for audio processing, the Vision SDK for image analysis. Use the right SDK for the right service.β
βEvery call to a Foundry model using the OpenAI SDK follows the same three-step pattern. Step one: authenticate. Create an OpenAI client, passing your endpoint URL and API key. Step two: call the API. Use the client to call responses.create, specifying your deployment name and input prompt. Step three: process the response. The response object contains output_text with the model's answer. This three-step pattern Γ’β¬β authenticate, call, process Γ’β¬β is the foundation of every AI application you will build. Once you understand it, the rest is just what you put in the prompt and how you use the output.β
βThe Responses API is Microsoft Foundry's modern API for interacting with generative AI models. It supports text inputs as well as multimodal inputs that combine text and images. For conversational applications, pass previous_response_id to continue from a previous response Γ’β¬β Foundry manages the conversation history for you. The older Chat Completions API is still supported for compatibility with existing code, but new applications should use the Responses API. The output is in response.output_text Γ’β¬β a simple string containing the model's generated answer.β
βFoundry Tools SDKs are specialised libraries for Azure's purpose-built AI services. While the OpenAI SDK works for general language model calls, Foundry Tools SDKs are optimised for specific, well-defined AI tasks. The Azure Language SDK provides sentiment analysis, entity recognition, key phrase extraction, and language detection. The Azure Speech SDK handles speech-to-text transcription and text-to-speech synthesis. The Vision SDK supports image analysis and OCR. Use these when your task is well-defined and falls into one of these service categories Γ’β¬β they are more efficient and cost-effective than calling a general LLM for predefined tasks.β
βLet us look at a concrete example using the Azure Language SDK. You create a TextAnalyticsClient with your endpoint and API key. Then call detect_language passing a list of text strings Γ’β¬β the response tells you the detected language for each. The same client exposes analyze_sentiment for positive or negative classification, recognize_entities for named entity extraction, recognize_pii_entities for detecting sensitive personal information that should be masked, and extract_key_phrases for surfacing the main topics. These purpose-built methods are simpler to use and more cost-effective than prompting a general LLM for the same tasks.β
βCalling AI from code is straightforward once you understand the patterns. The OpenAI SDK covers most LLM calls in three steps. The Responses API is the modern way to call models in Foundry. And Foundry Tools SDKs give you purpose-built, efficient clients for specific AI services. In the next episode we go deeper on Natural Language Processing Γ’β¬β one of the most widely used AI capabilities in enterprise applications.β
- 1Deploy a GPT model in Foundry
- 2Get the endpoint URL and API key
- 3Write Python code to call the Responses API
- 4Show the OpenAI SDK 3-step pattern
- 5Use the Azure Language SDK to detect language