Python: Add load_dotenv() to samples for .env file support (#4043)

* 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>
This commit is contained in:
Copilot
2026-02-19 10:55:13 +00:00
committed by GitHub
Unverified
parent 3ea9c5fa5d
commit b05fc9e849
317 changed files with 1654 additions and 479 deletions
@@ -17,6 +17,11 @@ Prerequisites:
import asyncio
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
async def run_semantic_kernel() -> None:
from azure.identity.aio import AzureCliCredential
@@ -17,6 +17,11 @@ by AzureAIAgentClient (AF).
import asyncio
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
async def run_semantic_kernel() -> None:
from azure.identity.aio import AzureCliCredential
@@ -12,6 +12,11 @@
import asyncio
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
async def run_semantic_kernel() -> None:
from azure.identity.aio import AzureCliCredential
@@ -17,9 +17,15 @@ model of choice before running.
import asyncio
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
async def run_semantic_kernel() -> None:
"""Call SK's ChatCompletionAgent for a simple question."""
from semantic_kernel.agents import ChatCompletionAgent
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
@@ -16,6 +16,11 @@ exposes a "specials" tool that both SDKs call during the conversation.
import asyncio
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
async def run_semantic_kernel() -> None:
from semantic_kernel.agents import ChatCompletionAgent, ChatHistoryAgentThread
@@ -16,6 +16,11 @@ for the second turn.
import asyncio
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
async def run_semantic_kernel() -> None:
from semantic_kernel.agents import ChatCompletionAgent, ChatHistoryAgentThread
@@ -12,6 +12,11 @@
import asyncio
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
async def run_semantic_kernel() -> None:
from semantic_kernel.agents import CopilotStudioAgent
@@ -12,6 +12,11 @@
import asyncio
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
async def run_semantic_kernel() -> None:
from semantic_kernel.agents import CopilotStudioAgent
@@ -13,6 +13,11 @@
import asyncio
import os
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
ASSISTANT_MODEL = os.environ.get("OPENAI_ASSISTANT_MODEL", "gpt-4o-mini")
@@ -12,6 +12,11 @@
import asyncio
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
async def run_semantic_kernel() -> None:
from semantic_kernel.agents import OpenAIAssistantAgent
@@ -14,11 +14,17 @@ import asyncio
import os
from typing import Any
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
ASSISTANT_MODEL = os.environ.get("OPENAI_ASSISTANT_MODEL", "gpt-4o-mini")
async def fake_weather_lookup(city: str, day: str) -> dict[str, Any]:
"""Pretend to call a weather service."""
return {
"city": city,
"day": day,
@@ -12,6 +12,11 @@
import asyncio
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
async def run_semantic_kernel() -> None:
from semantic_kernel.agents import OpenAIResponsesAgent
@@ -12,6 +12,11 @@
import asyncio
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
async def run_semantic_kernel() -> None:
from semantic_kernel.agents import OpenAIResponsesAgent
@@ -37,8 +42,7 @@ async def run_semantic_kernel() -> None:
async def run_agent_framework() -> None:
from agent_framework import Agent
from agent_framework import tool
from agent_framework import Agent, tool
from agent_framework.openai import OpenAIResponsesClient
@tool(name="add", description="Add two numbers")
@@ -12,8 +12,12 @@
import asyncio
from dotenv import load_dotenv
from pydantic import BaseModel
# Load environment variables from .env file
load_dotenv()
class ReleaseBrief(BaseModel):
feature: str
@@ -19,11 +19,15 @@ from agent_framework import Message
from agent_framework.azure import AzureOpenAIChatClient
from agent_framework.orchestrations import ConcurrentBuilder
from azure.identity import AzureCliCredential
from dotenv import load_dotenv
from semantic_kernel.agents import ChatCompletionAgent, ConcurrentOrchestration
from semantic_kernel.agents.runtime import InProcessRuntime
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion
from semantic_kernel.contents import ChatMessageContent
# Load environment variables from .env file
load_dotenv()
PROMPT = "Explain the concept of temperature from multiple scientific perspectives."
@@ -20,6 +20,7 @@ from agent_framework import Agent, Message
from agent_framework.azure import AzureOpenAIChatClient, AzureOpenAIResponsesClient
from agent_framework.orchestrations import GroupChatBuilder
from azure.identity import AzureCliCredential
from dotenv import load_dotenv
from semantic_kernel.agents import ChatCompletionAgent, GroupChatOrchestration
from semantic_kernel.agents.orchestration.group_chat import (
BooleanResult,
@@ -41,6 +42,9 @@ if sys.version_info >= (3, 12):
else:
from typing_extensions import override # pragma: no cover
# Load environment variables from .env file
load_dotenv()
DISCUSSION_TOPIC = "What are the essential steps for launching a community hackathon?"
@@ -22,6 +22,7 @@ from agent_framework import (
from agent_framework.azure import AzureOpenAIChatClient
from agent_framework.orchestrations import HandoffAgentUserRequest, HandoffBuilder
from azure.identity import AzureCliCredential
from dotenv import load_dotenv
from semantic_kernel.agents import Agent, ChatCompletionAgent, HandoffOrchestration, OrchestrationHandoffs
from semantic_kernel.agents.runtime import InProcessRuntime
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion
@@ -39,6 +40,8 @@ if sys.version_info >= (3, 12):
else:
pass # pragma: no cover
# Load environment variables from .env file
load_dotenv()
CUSTOMER_PROMPT = "I need help with order 12345. I want a replacement and need to know when it will arrive."
SCRIPTED_RESPONSES = [
@@ -125,6 +128,7 @@ _sk_new_message = True
def _sk_streaming_callback(message: StreamingChatMessageContent, is_final: bool) -> None:
"""Display SK agent messages as they stream."""
global _sk_new_message
if _sk_new_message:
print(f"{message.name}: ", end="", flush=True)
@@ -13,11 +13,11 @@
import asyncio
from collections.abc import Sequence
from typing import cast
from agent_framework import Agent
from agent_framework.openai import OpenAIChatClient, OpenAIResponsesClient
from agent_framework.orchestrations import MagenticBuilder
from dotenv import load_dotenv
from semantic_kernel.agents import (
ChatCompletionAgent,
MagenticOrchestration,
@@ -28,6 +28,9 @@ from semantic_kernel.agents.runtime import InProcessRuntime
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion, OpenAISettings
from semantic_kernel.contents import ChatMessageContent
# Load environment variables from .env file
load_dotenv()
PROMPT = (
"I am preparing a report on the energy efficiency of different machine learning model architectures. "
"Compare the estimated training and inference energy consumption of ResNet-50, BERT-base, and GPT-2 "
@@ -157,9 +160,7 @@ async def run_agent_framework_example(prompt: str) -> str | None:
client=OpenAIChatClient(),
)
workflow = MagenticBuilder(
participants=[researcher, coder], manager_agent=manager_agent
).build()
workflow = MagenticBuilder(participants=[researcher, coder], manager_agent=manager_agent).build()
final_text: str | None = None
async for event in workflow.run(prompt, stream=True):
@@ -19,11 +19,15 @@ from agent_framework import Message
from agent_framework.azure import AzureOpenAIChatClient
from agent_framework.orchestrations import SequentialBuilder
from azure.identity import AzureCliCredential
from dotenv import load_dotenv
from semantic_kernel.agents import Agent, ChatCompletionAgent, SequentialOrchestration
from semantic_kernel.agents.runtime import InProcessRuntime
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion
from semantic_kernel.contents import ChatMessageContent
# Load environment variables from .env file
load_dotenv()
PROMPT = "Write a tagline for a budget-friendly eBike."
@@ -21,6 +21,7 @@ from typing import TYPE_CHECKING, ClassVar, cast
# region Agent Framework imports
######################################################################
from agent_framework import Executor, WorkflowBuilder, WorkflowContext, handler
from dotenv import load_dotenv
from pydantic import BaseModel, Field
######################################################################
@@ -39,6 +40,9 @@ if TYPE_CHECKING:
from semantic_kernel.processes.kernel_process import KernelProcess
from semantic_kernel.processes.local_runtime.local_kernel_process import LocalKernelProcessContext
# Load environment variables from .env file
load_dotenv()
async def _start_local_kernel_process(
*,
@@ -28,6 +28,7 @@ from agent_framework import (
WorkflowExecutor,
handler,
)
from dotenv import load_dotenv
from pydantic import BaseModel, Field
######################################################################
@@ -48,9 +49,11 @@ from typing_extensions import Never
######################################################################
# endregion
######################################################################
logging.basicConfig(level=logging.WARNING)
# Load environment variables from .env file
load_dotenv()
class ProcessEvents(Enum):
START_PROCESS = "StartProcess"