* Initial plan * Add load_dotenv() to 303 Python samples for environment variable loading Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> * Update SAMPLE_GUIDELINES.md to document load_dotenv() requirement Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> * Update samples README.md to document .env file usage Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> * Run ruff format on all changed sample files Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> * Clarify load_dotenv() usage in README - local dev vs production Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> * Remove deprecated getting_started folder as requested Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> * Document env_file_path parameter for per-client configuration Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> * Merge main branch to resolve conflicts Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> * Fix run_evaluation.py file that was empty in merge commit Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> * Remove dotnet changes from merge - out of scope for this PR Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> * Remove package and test changes from merge - only sample changes needed Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> * Remove test_func_utils.py - only sample changes needed Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> * Revert sample files not in original changeset - keep only load_dotenv additions Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> * Move load_dotenv() outside snippet tag in 06_host_your_agent.py Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> * Fix comment placement - move load_dotenv before code comments Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> * Fix load_dotenv() placement across all samples - after docstring, before code comments Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> * Merge latest main branch with load_dotenv changes Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> * Remove non-sample changes from merge - keep only load_dotenv additions Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> * Revert non-load_dotenv sample changes from merge Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> * Fix run_evaluation.py - use main's improved version (file already had load_dotenv) Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> * Manual update * Manual update 2 * Fix Role usage and load_dotenv placement per PR review feedback Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com> * Fix Role usage - use string literals not enum attributes Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com> * Fix SAMPLE_GUIDELINES.md example - load_dotenv before docstring per guidance Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com> * Move load_dotenv() before docstrings in all samples per SAMPLE_GUIDELINES ordering Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com> * Address PR review: rename files, fix placement, add session usage, remove note Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com> * Update Redis README to reference renamed file redis_history_provider.py Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> Co-authored-by: Tao Chen <taochen@microsoft.com> Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com> Co-authored-by: Eduard van Valkenburg <eavanvalkenburg@users.noreply.github.com>
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_session.py |
Demonstrates session management with Azure agents, including automatic session creation for stateless conversations and explicit session 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_session.py |
Demonstrates session management with Azure agents, including automatic session creation for stateless conversations and explicit session 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_session.py |
Demonstrates session management with Azure agents, including automatic session creation for stateless conversations and explicit session 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 endpointAZURE_OPENAI_CHAT_DEPLOYMENT_NAME: The name of your Azure OpenAI chat model deploymentAZURE_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 is2024-02-15-preview)AZURE_OPENAI_API_KEY: Your Azure OpenAI API key (if not usingAzureCliCredential)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