Python: OpenAI Clients accepting api_key callback - #1139
Merged
Conversation
Contributor
There was a problem hiding this comment.
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.
Contributor
Python Test Coverage Report •
Python Unit Test Overview
|
|||||||||||||||||||||||||||||||||||||||||||||
dmytrostruk
reviewed
Oct 2, 2025
dmytrostruk
approved these changes
Oct 6, 2025
eavanvalkenburg
approved these changes
Oct 6, 2025
dmytrostruk
approved these changes
Oct 6, 2025
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation and Context
Closes #1070
Description
Contribution Checklist