Skip to content

Python: OpenAI Clients accepting api_key callback - #1139

Merged
giles17 merged 6 commits into
microsoft:mainfrom
giles17:callback_openai_client
Oct 6, 2025
Merged

Python: OpenAI Clients accepting api_key callback#1139
giles17 merged 6 commits into
microsoft:mainfrom
giles17:callback_openai_client

Conversation

@giles17

@giles17 giles17 commented Oct 2, 2025

Copy link
Copy Markdown
Contributor

Motivation and Context

Closes #1070

Description

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • Is this a breaking change? If yes, add "[BREAKING]" prefix to the title of the PR.

Copilot AI review requested due to automatic review settings October 2, 2025 23:13
@markwallace-microsoft markwallace-microsoft added the python Usage: [Issues, PRs], Target: Python label Oct 2, 2025
@github-actions github-actions Bot changed the title OpenAI Clients accepting api_key callback Python: OpenAI Clients accepting api_key callback Oct 2, 2025

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Adds support for supplying OpenAI API keys via a callable (sync or async) to enable dynamic token refresh. Implements a refresh hook invoked before each request/stream across Chat, Responses, and Assistants clients, and adds corresponding tests.

  • Extend api_key parameters/types to accept callables returning str or Awaitable[str]
  • Add _refresh_api_key logic and invoke it before request/stream calls
  • Add tests for callable API key initialization and refresh behavior

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
python/packages/core/agent_framework/openai/_shared.py Introduces callable/awaitable API key support and refresh mechanism in shared base classes.
python/packages/core/agent_framework/openai/_responses_client.py Integrates API key refresh into responses client request/stream paths; updates constructor to accept callable key.
python/packages/core/agent_framework/openai/_chat_client.py Adds refresh calls and callable api_key handling to chat client.
python/packages/core/agent_framework/openai/_assistants_client.py Adds refresh call for streaming and callable api_key handling to assistants client.
python/packages/core/tests/openai/test_openai_responses_client.py Adds tests for callable API key and refresh sequence (responses client).
python/packages/core/tests/openai/test_openai_chat_client.py Adds tests for callable API key and refresh sequence (chat client).
python/packages/core/tests/openai/test_openai_assistants_client.py Adds tests for callable API key and refresh sequence (assistants client).

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment thread python/packages/core/agent_framework/openai/_responses_client.py
Comment thread python/packages/core/agent_framework/openai/_chat_client.py
Comment thread python/packages/core/agent_framework/openai/_assistants_client.py
Comment thread python/packages/core/agent_framework/openai/_chat_client.py Outdated
@markwallace-microsoft

markwallace-microsoft commented Oct 2, 2025

Copy link
Copy Markdown
Contributor

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/core/agent_framework/openai
   _assistants_client.py2012886%175, 188, 191, 197–198, 201, 204, 207–208, 237, 239, 241, 243, 246, 249, 252, 256, 266, 350, 387, 423, 452–455, 505–506, 508
   _chat_client.py2314580%106–107, 111, 129, 139–140, 142, 153, 160, 164, 166–169, 218, 249, 254–268, 275–277, 354, 357, 359, 361, 384–385, 391, 407, 439, 441, 452
   _responses_client.py4036484%141–142, 145–146, 152–153, 156, 163, 195, 204, 216, 246, 274–275, 298, 319, 381, 476, 479, 494–495, 499–501, 549, 569–570, 583–584, 600–601, 634, 636, 680, 684, 686, 695, 697, 706–707, 720, 722, 795–801, 818–823, 842, 860, 870, 872, 890–891, 893–895
   _shared.py931287%65, 71–74, 139, 152, 154, 161, 163, 237, 261
TOTAL10267170183% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
1246 98 💤 0 ❌ 0 🔥 28.036s ⏱️

Comment thread python/packages/core/agent_framework/openai/_assistants_client.py Outdated
Comment thread python/packages/core/agent_framework/openai/_shared.py Outdated
@giles17
giles17 added this pull request to the merge queue Oct 6, 2025
Merged via the queue into microsoft:main with commit 4e6ae44 Oct 6, 2025
24 checks passed
ReubenBond pushed a commit to ReubenBond/agent-framework that referenced this pull request Oct 28, 2025
* openai clients accepting callable

* fixes

* version checks
arisng pushed a commit to arisng/agent-framework that referenced this pull request Feb 2, 2026
* openai clients accepting callable

* fixes

* version checks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: OpenAIChatClient should accept api_key token provider callback

5 participants