fix callable api_key_provider refresh in beta.responses.connect#3487
Open
trrwilson wants to merge 1 commit into
Open
fix callable api_key_provider refresh in beta.responses.connect#3487trrwilson wants to merge 1 commit into
trrwilson wants to merge 1 commit into
Conversation
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.
Changes being requested
Warning
Clients using
callableAPI key providers can't reliably usebeta.responses.connect; manual fetch/refresh and management of credential lifetime is necessary without a fix.Refresh callable API-key providers before opening sync and async Responses API WebSocket connections.
Normal HTTP requests, including
client.responses.create(), refresh a callableapi_keyduring request preparation. However,client.beta.responses.connect()readsclient.auth_headersdirectly without first invoking_refresh_api_key().When a client is constructed with a callable provider, its initial
api_keyis empty. Consequently, the WebSocket handshake may be sent without anAuthorizationheader. The same provider works with HTTP requests but fails with the Responses WebSocket API.This change calls:
self.__client._refresh_api_key()before a sync WebSocket handshakeawait self.__client._refresh_api_key()before an async WebSocket handshakeThe refresh occurs inside
_connect_ws(), ensuring that it applies to both initial connections and reconnection attempts. Static API keys retain their existing behavior.Focused tests verify that:
Additional context & links
The issue was reproduced against a Foundry Responses WebSocket endpoint using an Azure Identity bearer-token provider.
Observed behavior before the change:
client.responses.create()completed successfully and invoked the provider once.client.beta.responses.connect()failed its handshake and invoked the provider zero times.client.beta.responses.connect()likewise failed because its handshake lacked a refreshed credential.Authorizationheader.Observed behavior after the change:
Local validation performed:
git diff --check: passed