From 1488314909c47aaf8f5f694cda7f49d6dfe6f900 Mon Sep 17 00:00:00 2001 From: droideronline Date: Sun, 26 Jul 2026 23:15:13 +0530 Subject: [PATCH 1/3] Python: Add GitHub Copilot BYOK sample Demonstrates routing GitHubCopilotAgent requests through a custom OpenAI-compatible endpoint via ProviderConfig instead of the default GitHub Copilot backend. --- .../providers/github_copilot/README.md | 1 + .../github_copilot_with_byok.py | 58 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 python/samples/02-agents/providers/github_copilot/github_copilot_with_byok.py diff --git a/python/samples/02-agents/providers/github_copilot/README.md b/python/samples/02-agents/providers/github_copilot/README.md index 476f614b858..8e79a50987d 100644 --- a/python/samples/02-agents/providers/github_copilot/README.md +++ b/python/samples/02-agents/providers/github_copilot/README.md @@ -53,3 +53,4 @@ See the [observability samples](../../../02-agents/observability/) for full exam | [`github_copilot_with_mcp.py`](github_copilot_with_mcp.py) | Shows how to configure MCP (Model Context Protocol) servers, including local (stdio) and remote (HTTP) servers. | | [`github_copilot_with_instruction_directories.py`](github_copilot_with_instruction_directories.py) | Shows how to configure custom instruction directories for project-specific or team-shared guidelines. | | [`github_copilot_with_multiple_permissions.py`](github_copilot_with_multiple_permissions.py) | Shows how to combine multiple permission types for complex tasks that require shell, read, and write access. | +| [`github_copilot_with_byok.py`](github_copilot_with_byok.py) | Shows how to configure BYOK (Bring Your Own Key) to route requests through your own OpenAI-compatible endpoint instead of the GitHub Copilot backend. | diff --git a/python/samples/02-agents/providers/github_copilot/github_copilot_with_byok.py b/python/samples/02-agents/providers/github_copilot/github_copilot_with_byok.py new file mode 100644 index 00000000000..deddfdf382f --- /dev/null +++ b/python/samples/02-agents/providers/github_copilot/github_copilot_with_byok.py @@ -0,0 +1,58 @@ +# Copyright (c) Microsoft. All rights reserved. + +""" +GitHub Copilot Agent with BYOK (Bring Your Own Key) + +This sample demonstrates how to configure GitHubCopilotAgent to route model requests through +your own OpenAI-compatible endpoint (OpenAI, Azure OpenAI, Anthropic, or a compatible service +such as vLLM/LiteLLM/Ollama) instead of the default GitHub Copilot backend, using the Copilot +SDK's BYOK support. + +Set the following environment variables before running: + BYOK_BASE_URL - Base URL of your OpenAI-compatible endpoint. + BYOK_API_KEY - API key for that endpoint. + BYOK_MODEL_ID - Model name to request (e.g. "gpt-4o"). Defaults to "gpt-4o". + +SECURITY NOTE: BYOK uses static credentials (no automatic token refresh) and usage is tracked +by your provider rather than GitHub. Keep API keys out of source control; load them from +environment variables or a secret store, as shown here. +""" + +import asyncio +import os + +from agent_framework.github import GitHubCopilotAgent, GitHubCopilotOptions +from copilot.session import ProviderConfig + + +async def main() -> None: + print("=== GitHub Copilot Agent with BYOK (Bring Your Own Key) ===\n") + + model_id = os.environ.get("BYOK_MODEL_ID", "gpt-4o") + + # ProviderConfig routes the session through a custom endpoint instead of the GitHub + # Copilot backend. `wire_api="completions"` is the broadly compatible choice; use + # "responses" for providers that support the OpenAI Responses API. + provider: ProviderConfig = { + "type": "openai", + "base_url": os.environ["BYOK_BASE_URL"], + "api_key": os.environ["BYOK_API_KEY"], + "wire_api": "completions", + "model_id": model_id, + } + + # BYOK requires the model to also be set at the session level. + agent: GitHubCopilotAgent[GitHubCopilotOptions] = GitHubCopilotAgent( + instructions="You are a helpful assistant.", + default_options=GitHubCopilotOptions(model=model_id, provider=provider), + ) + + async with agent: + query = "What are the benefits of using your own API keys with an agent framework?" + print(f"User: {query}") + result = await agent.run(query) + print(f"\nAgent: {result}\n") + + +if __name__ == "__main__": + asyncio.run(main()) From dba20a811a889acb36ee0a5fbf343169c6d66c2b Mon Sep 17 00:00:00 2001 From: Dineshsuriya D <43177361+droideronline@users.noreply.github.com> Date: Sun, 26 Jul 2026 23:29:05 +0530 Subject: [PATCH 2/3] Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- .../providers/github_copilot/github_copilot_with_byok.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/python/samples/02-agents/providers/github_copilot/github_copilot_with_byok.py b/python/samples/02-agents/providers/github_copilot/github_copilot_with_byok.py index deddfdf382f..582e88f2cba 100644 --- a/python/samples/02-agents/providers/github_copilot/github_copilot_with_byok.py +++ b/python/samples/02-agents/providers/github_copilot/github_copilot_with_byok.py @@ -9,10 +9,10 @@ SDK's BYOK support. Set the following environment variables before running: - BYOK_BASE_URL - Base URL of your OpenAI-compatible endpoint. - BYOK_API_KEY - API key for that endpoint. - BYOK_MODEL_ID - Model name to request (e.g. "gpt-4o"). Defaults to "gpt-4o". - + BYOK_PROVIDER_TYPE - Provider type ("openai", "azure", "anthropic"). Defaults to "openai". + BYOK_BASE_URL - Base URL of your provider endpoint. + BYOK_API_KEY - API key for that endpoint. + BYOK_MODEL_ID - Model name to request (e.g. "gpt-4o"). Defaults to "gpt-4o". SECURITY NOTE: BYOK uses static credentials (no automatic token refresh) and usage is tracked by your provider rather than GitHub. Keep API keys out of source control; load them from environment variables or a secret store, as shown here. From 46a0d5ea1cdd60797ad9101c920e544ab725b669 Mon Sep 17 00:00:00 2001 From: droideronline Date: Mon, 27 Jul 2026 21:58:04 +0530 Subject: [PATCH 3/3] Python: Address BYOK sample review feedback MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Make the provider type configurable via BYOK_PROVIDER_TYPE (default "openai") instead of hardcoding "openai" — a partial autofix commit had already updated the docstring to document this env var but left the code hardcoded, which this finishes. - Stop calling the endpoint "OpenAI-compatible" everywhere; Anthropic isn't OpenAI-wire- compatible, so reword to "your own endpoint" and list the actual supported providers (mirrors the equivalent .NET sample fix). --- .../02-agents/providers/github_copilot/README.md | 2 +- .../github_copilot/github_copilot_with_byok.py | 11 +++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/python/samples/02-agents/providers/github_copilot/README.md b/python/samples/02-agents/providers/github_copilot/README.md index 8e79a50987d..143bd91a16b 100644 --- a/python/samples/02-agents/providers/github_copilot/README.md +++ b/python/samples/02-agents/providers/github_copilot/README.md @@ -53,4 +53,4 @@ See the [observability samples](../../../02-agents/observability/) for full exam | [`github_copilot_with_mcp.py`](github_copilot_with_mcp.py) | Shows how to configure MCP (Model Context Protocol) servers, including local (stdio) and remote (HTTP) servers. | | [`github_copilot_with_instruction_directories.py`](github_copilot_with_instruction_directories.py) | Shows how to configure custom instruction directories for project-specific or team-shared guidelines. | | [`github_copilot_with_multiple_permissions.py`](github_copilot_with_multiple_permissions.py) | Shows how to combine multiple permission types for complex tasks that require shell, read, and write access. | -| [`github_copilot_with_byok.py`](github_copilot_with_byok.py) | Shows how to configure BYOK (Bring Your Own Key) to route requests through your own OpenAI-compatible endpoint instead of the GitHub Copilot backend. | +| [`github_copilot_with_byok.py`](github_copilot_with_byok.py) | Shows how to configure BYOK (Bring Your Own Key) to route requests through your own endpoint instead of the GitHub Copilot backend. | diff --git a/python/samples/02-agents/providers/github_copilot/github_copilot_with_byok.py b/python/samples/02-agents/providers/github_copilot/github_copilot_with_byok.py index 582e88f2cba..20a0602f11f 100644 --- a/python/samples/02-agents/providers/github_copilot/github_copilot_with_byok.py +++ b/python/samples/02-agents/providers/github_copilot/github_copilot_with_byok.py @@ -4,15 +4,16 @@ GitHub Copilot Agent with BYOK (Bring Your Own Key) This sample demonstrates how to configure GitHubCopilotAgent to route model requests through -your own OpenAI-compatible endpoint (OpenAI, Azure OpenAI, Anthropic, or a compatible service -such as vLLM/LiteLLM/Ollama) instead of the default GitHub Copilot backend, using the Copilot -SDK's BYOK support. +your own endpoint (OpenAI, Azure OpenAI, Anthropic, or an OpenAI-compatible service such as +vLLM/LiteLLM/Ollama) instead of the default GitHub Copilot backend, using the Copilot SDK's +BYOK support. Set the following environment variables before running: BYOK_PROVIDER_TYPE - Provider type ("openai", "azure", "anthropic"). Defaults to "openai". BYOK_BASE_URL - Base URL of your provider endpoint. BYOK_API_KEY - API key for that endpoint. BYOK_MODEL_ID - Model name to request (e.g. "gpt-4o"). Defaults to "gpt-4o". + SECURITY NOTE: BYOK uses static credentials (no automatic token refresh) and usage is tracked by your provider rather than GitHub. Keep API keys out of source control; load them from environment variables or a secret store, as shown here. @@ -20,6 +21,7 @@ import asyncio import os +from typing import Literal, cast from agent_framework.github import GitHubCopilotAgent, GitHubCopilotOptions from copilot.session import ProviderConfig @@ -29,12 +31,13 @@ async def main() -> None: print("=== GitHub Copilot Agent with BYOK (Bring Your Own Key) ===\n") model_id = os.environ.get("BYOK_MODEL_ID", "gpt-4o") + provider_type = cast(Literal["openai", "azure", "anthropic"], os.environ.get("BYOK_PROVIDER_TYPE", "openai")) # ProviderConfig routes the session through a custom endpoint instead of the GitHub # Copilot backend. `wire_api="completions"` is the broadly compatible choice; use # "responses" for providers that support the OpenAI Responses API. provider: ProviderConfig = { - "type": "openai", + "type": provider_type, "base_url": os.environ["BYOK_BASE_URL"], "api_key": os.environ["BYOK_API_KEY"], "wire_api": "completions",