Files
agent-framework/python/samples/getting_started/agents/azure_openai
T
Copilot a427af91a9 Python: Allow AzureOpenAIResponsesClient creation with Foundry project endpoint (#3814)
* Initial plan

* feat: extend AzureOpenAIResponsesClient to support Foundry project endpoints

Add project_client and project_endpoint parameters to allow creating
the client via an Azure AI Foundry project. When provided, the client
uses AIProjectClient.get_openai_client() to obtain the OpenAI client.
The azure-ai-projects package is imported lazily and only required
when using the project endpoint path.

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* fix: address code review - remove duplicate MagicMock imports in tests

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* fix: add type field to Responses API input items and add Foundry sample

- Add 'type: message' to input items in _prepare_message_for_openai
  to comply with the Responses API schema requirement
- Filter out empty dicts from unsupported content types to prevent
  sending items with invalid empty type values
- Add azure_responses_client_with_foundry.py sample demonstrating
  AzureOpenAIResponsesClient with project_endpoint
- Update README and pyrightconfig.samples.json accordingly

* updates to response format and setup

* fix: patch AIProjectClient at correct module path in test

Patch agent_framework.azure._responses_client.AIProjectClient instead of
azure.ai.projects.aio.AIProjectClient since the import is at module level.

* docs: add Foundry sample to READMEs and document AZURE_AI_PROJECT_ENDPOINT env var

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>
Co-authored-by: eavanvalkenburg <github@vanvalkenburg.eu>
a427af91a9 ยท 2026-02-11 15:46:25 +00:00
History
..

Azure OpenAI Agent Examples

This folder contains examples demonstrating different ways to create and use agents with the different Azure OpenAI chat client from the agent_framework.azure package.

Examples

File Description
azure_assistants_basic.py The simplest way to create an agent using Agent with AzureOpenAIAssistantsClient. Shows both streaming and non-streaming responses with automatic assistant creation and cleanup.
azure_assistants_with_code_interpreter.py Shows how to use AzureOpenAIAssistantsClient.get_code_interpreter_tool() with Azure agents to write and execute Python code. Includes helper methods for accessing code interpreter data from response chunks.
azure_assistants_with_existing_assistant.py Shows how to work with a pre-existing assistant by providing the assistant ID to the Azure Assistants client. Demonstrates proper cleanup of manually created assistants.
azure_assistants_with_explicit_settings.py Shows how to initialize an agent with a specific assistants client, configuring settings explicitly including endpoint and deployment name.
azure_assistants_with_function_tools.py Demonstrates how to use function tools with agents. Shows both agent-level tools (defined when creating the agent) and query-level tools (provided with specific queries).
azure_assistants_with_thread.py Demonstrates thread management with Azure agents, including automatic thread creation for stateless conversations and explicit thread management for maintaining conversation context across multiple interactions.
azure_chat_client_basic.py The simplest way to create an agent using Agent with AzureOpenAIChatClient. Shows both streaming and non-streaming responses for chat-based interactions with Azure OpenAI models.
azure_chat_client_with_explicit_settings.py Shows how to initialize an agent with a specific chat client, configuring settings explicitly including endpoint and deployment name.
azure_chat_client_with_function_tools.py Demonstrates how to use function tools with agents. Shows both agent-level tools (defined when creating the agent) and query-level tools (provided with specific queries).
azure_chat_client_with_thread.py Demonstrates thread management with Azure agents, including automatic thread creation for stateless conversations and explicit thread management for maintaining conversation context across multiple interactions.
azure_responses_client_basic.py The simplest way to create an agent using Agent with AzureOpenAIResponsesClient. Shows both streaming and non-streaming responses for structured response generation with Azure OpenAI models.
azure_responses_client_code_interpreter_files.py Demonstrates using AzureOpenAIResponsesClient.get_code_interpreter_tool() with file uploads for data analysis. Shows how to create, upload, and analyze CSV files using Python code execution with Azure OpenAI Responses.
azure_responses_client_image_analysis.py Shows how to use Azure OpenAI Responses for image analysis and vision tasks. Demonstrates multi-modal messages combining text and image content using remote URLs.
azure_responses_client_with_code_interpreter.py Shows how to use AzureOpenAIResponsesClient.get_code_interpreter_tool() with Azure agents to write and execute Python code. Includes helper methods for accessing code interpreter data from response chunks.
azure_responses_client_with_explicit_settings.py Shows how to initialize an agent with a specific responses client, configuring settings explicitly including endpoint and deployment name.
azure_responses_client_with_file_search.py Demonstrates using AzureOpenAIResponsesClient.get_file_search_tool() with Azure OpenAI Responses Client for direct document-based question answering and information retrieval from vector stores.
azure_responses_client_with_foundry.py Shows how to create an agent using an Azure AI Foundry project endpoint instead of a direct Azure OpenAI endpoint. Requires the azure-ai-projects package.
azure_responses_client_with_function_tools.py Demonstrates how to use function tools with agents. Shows both agent-level tools (defined when creating the agent) and query-level tools (provided with specific queries).
azure_responses_client_with_hosted_mcp.py Shows how to integrate Azure OpenAI Responses Client with hosted Model Context Protocol (MCP) servers using AzureOpenAIResponsesClient.get_mcp_tool() for extended functionality.
azure_responses_client_with_local_mcp.py Shows how to integrate Azure OpenAI Responses Client with local Model Context Protocol (MCP) servers using MCPStreamableHTTPTool for extended functionality.
azure_responses_client_with_thread.py Demonstrates thread management with Azure agents, including automatic thread creation for stateless conversations and explicit thread management for maintaining conversation context across multiple interactions.

Environment Variables

Make sure to set the following environment variables before running the examples:

  • AZURE_OPENAI_ENDPOINT: Your Azure OpenAI endpoint
  • AZURE_OPENAI_CHAT_DEPLOYMENT_NAME: The name of your Azure OpenAI chat model deployment
  • AZURE_OPENAI_RESPONSES_DEPLOYMENT_NAME: The name of your Azure OpenAI Responses deployment

For the Foundry project sample (azure_responses_client_with_foundry.py), also set:

  • AZURE_AI_PROJECT_ENDPOINT: Your Azure AI Foundry project endpoint

Optionally, you can set:

  • AZURE_OPENAI_API_VERSION: The API version to use (default is 2024-02-15-preview)
  • AZURE_OPENAI_API_KEY: Your Azure OpenAI API key (if not using AzureCliCredential)
  • AZURE_OPENAI_BASE_URL: Your Azure OpenAI base URL (if different from the endpoint)

Authentication

All examples use AzureCliCredential for authentication. Run az login in your terminal before running the examples, or replace AzureCliCredential with your preferred authentication method.

Required role-based access control (RBAC) roles

To access the Azure OpenAI API, your Azure account or service principal needs one of the following RBAC roles assigned to the Azure OpenAI resource:

  • Cognitive Services OpenAI User: Provides read access to Azure OpenAI resources and the ability to call the inference APIs. This is the minimum role required for running these examples.
  • Cognitive Services OpenAI Contributor: Provides full access to Azure OpenAI resources, including the ability to create, update, and delete deployments and models.

For most scenarios, the Cognitive Services OpenAI User role is sufficient. You can assign this role through the Azure portal under the Azure OpenAI resource's "Access control (IAM)" section.

For more detailed information about Azure OpenAI RBAC roles, see: Role-based access control for Azure OpenAI Service