Python: Add load_dotenv() to samples for .env file support#4043
Python: Add load_dotenv() to samples for .env file support#4043eavanvalkenburg merged 33 commits intomainfrom
Conversation
Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com>
Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com>
Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com>
abed4ea to
cd1b8ae
Compare
|
@copilot run formatting on all changed files |
Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com>
Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com>
Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com>
Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com>
|
@copilot There is a conflict with main. Fix it |
…uidance Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>
eavanvalkenburg
left a comment
There was a problem hiding this comment.
@copilot just looking at the first few samples, it is not consistently following the sample guidelines, make sure that the load_dotenv() call is in the right place in all samples!
…INES ordering Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>
Fixed in d9513cc. Moved |
python/samples/02-agents/providers/anthropic/anthropic_skills.py
Outdated
Show resolved
Hide resolved
python/samples/02-agents/providers/openai/openai_responses_client_with_hosted_mcp.py
Outdated
Show resolved
Hide resolved
python/samples/03-workflows/checkpoint/checkpoint_with_human_in_the_loop.py
Outdated
Show resolved
Hide resolved
python/samples/03-workflows/checkpoint/checkpoint_with_human_in_the_loop.py
Outdated
Show resolved
Hide resolved
|
@copilot just reviewed again, left a couple of comments, please address |
…move note Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>
|
@copilot when you renamed some sample in one of the last commits, you forgot to update the README that refers to it, please do that now |
Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>
Motivation and Context
Samples failed with missing API key errors even when credentials were present in
.envfiles. Thepython-dotenvpackage is already a dependency but samples weren't callingload_dotenv().Description
Added environment variable loading to 292 Python sample files that require API keys, endpoints, or other configuration:
from dotenv import load_dotenvandload_dotenv()call to 292 sample files following SAMPLE_GUIDELINES.md ordering:load_dotenv(), then docstring)if __name__ == "__main__"blocks)"user","assistant") consistent with current API whereRoleisNewType("Role", str)not an enumSAMPLE_GUIDELINES.mdordering guidance (lines 11-13) and example code (lines 53-70) to be consistent - both showload_dotenv()before module docstringsamples/README.mdwith comprehensive environment variable setup instructions:load_dotenv()is for local development convenience.envfile withload_dotenv(), direct shell exports, and per-clientenv_file_pathparameterpython/packages/redis/README.mdto reference renamed sample fileruff formaton all changed files (102 files reformatted with proper spacing and quote style)getting_started/sessions/folder (17 files) as it contained outdated samplesredis_chat_message_store_session.py→redis_history_provider.py,custom_chat_message_store_session.py→custom_history_provider.pyredis_conversation.pyto use sessions and document both RedisContextProvider and RedisHistoryProvidercheckpoint_with_human_in_the_loop.pyload_dotenv()additions. All merge conflicts resolved.Correct pattern applied (per SAMPLE_GUIDELINES.md):
Key principles:
load_dotenv()call appears before module docstrings (per SAMPLE_GUIDELINES.md ordering)load_dotenv()call and stay adjacent to their codeload_dotenv()is always placed at module-level, never insideif __name__ == "__main__"blocks"user","assistant") asRoleis aNewTypealias, not an enumAlternative configuration methods:
env_file_pathparameter:Handles multi-line imports correctly. Samples without environment variable usage unchanged.
Note: The
load_dotenv()call has no effect when a.envfile is not present, making it safe for production deployments where environment variables are set through the platform.Contribution Checklist
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.