Fix docstring/load_dotenv ordering in foundry samples and add try/finally cleanup

Agent-Logs-Url: https://github.com/microsoft/agent-framework/sessions/382df85c-cf3c-4802-97e0-a6df35fc9876

Co-authored-by: chetantoshniwal <255221507+chetantoshniwal@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2026-04-01 18:56:51 +00:00
committed by Eduard van Valkenburg
Unverified
parent bbae6401b1
commit e26c3580fb
6 changed files with 33 additions and 30 deletions
@@ -7,8 +7,6 @@ from agent_framework.foundry import FoundryAgent
from azure.identity import AzureCliCredential
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
"""
Foundry Agent — Connect to a pre-configured agent in Microsoft Foundry
@@ -22,6 +20,9 @@ Environment variables:
FOUNDRY_AGENT_VERSION — Version of the agent (optional, for PromptAgents)
"""
# Load environment variables from .env file
load_dotenv()
async def main() -> None:
agent = FoundryAgent(
@@ -8,9 +8,6 @@ from agent_framework.foundry import FoundryAgent, RawFoundryAgentChatClient
from azure.identity import AzureCliCredential
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
"""
Foundry Agent — Custom client configuration
@@ -26,6 +23,9 @@ Environment variables:
FOUNDRY_AGENT_VERSION — Version of the agent (optional, for PromptAgents)
"""
# Load environment variables from .env file
load_dotenv()
async def main() -> None:
# Option 1: Default — full middleware on both agent and client
@@ -7,9 +7,6 @@ from agent_framework.foundry import FoundryAgent
from azure.identity import AzureCliCredential
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
"""
Foundry Agent — Connect to a HostedAgent (no version needed)
@@ -21,6 +18,9 @@ Environment variables:
FOUNDRY_AGENT_NAME — Name of the hosted agent
"""
# Load environment variables from .env file
load_dotenv()
async def main() -> None:
# HostedAgents don't need agent_version
@@ -7,8 +7,6 @@ from agent_framework.foundry import FoundryAgent
from azure.identity import AzureCliCredential
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
"""
Foundry Agent with Environment Variables
@@ -21,6 +19,9 @@ Environment variables:
FOUNDRY_AGENT_VERSION — Version of the agent (optional, for PromptAgents)
"""
# Load environment variables from .env file
load_dotenv()
async def main() -> None:
agent = FoundryAgent(
@@ -10,9 +10,6 @@ from azure.identity import AzureCliCredential
from dotenv import load_dotenv
from pydantic import Field
# Load environment variables from .env file
load_dotenv()
"""
Foundry Agent with Local Function Tools
@@ -29,6 +26,9 @@ Environment variables:
FOUNDRY_AGENT_VERSION — Version of the agent
"""
# Load environment variables from .env file
load_dotenv()
@tool(approval_mode="never_require")
def get_weather(
@@ -10,9 +10,6 @@ from azure.identity import AzureCliCredential
from dotenv import load_dotenv
from openai import AsyncOpenAI
# Load environment variables from .env file
load_dotenv()
"""
Foundry Chat Client with Code Interpreter and Files Example
@@ -24,6 +21,9 @@ Environment variables:
FOUNDRY_MODEL — Model deployment name (e.g. "gpt-4o")
"""
# Load environment variables from .env file
load_dotenv()
# Helper functions
@@ -82,22 +82,23 @@ async def main() -> None:
temp_file_path, file_id = await create_sample_file_and_upload(openai_client)
# Create code interpreter tool with file access
code_interpreter_tool = client.get_code_interpreter_tool(file_ids=[file_id])
try:
# Create code interpreter tool with file access
code_interpreter_tool = client.get_code_interpreter_tool(file_ids=[file_id])
agent = Agent(
client=client,
instructions="You are a helpful assistant that can analyze data files using Python code.",
tools=[code_interpreter_tool],
)
agent = Agent(
client=client,
instructions="You are a helpful assistant that can analyze data files using Python code.",
tools=[code_interpreter_tool],
)
# Test the code interpreter with the uploaded file
query = "Analyze the employee data in the uploaded CSV file. Calculate average salary by department."
print(f"User: {query}")
result = await agent.run(query)
print(f"Agent: {result.text}")
await cleanup_files(openai_client, temp_file_path, file_id)
# Test the code interpreter with the uploaded file
query = "Analyze the employee data in the uploaded CSV file. Calculate average salary by department."
print(f"User: {query}")
result = await agent.run(query)
print(f"Agent: {result.text}")
finally:
await cleanup_files(openai_client, temp_file_path, file_id)
if __name__ == "__main__":