Omit Authorization header when api_key is empty - #1
Open
devin-ai-integration[bot] wants to merge 2 commits into
Open
Omit Authorization header when api_key is empty#1devin-ai-integration[bot] wants to merge 2 commits into
devin-ai-integration[bot] wants to merge 2 commits into
Conversation
Author
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
patched_get_headers called _get_token() a second time, after get_headers() had already called it. For the documented callable api_key form this invoked the supplier twice per request, and a supplier whose value changed between the two calls produced the wrong header: returning "real-token" then "" stripped the Authorization header despite a valid token, and the reverse sent "Bearer " while a valid token was available. Both yield a 401. Inspect the header get_headers() already built instead, matching what patched_async_get_headers has been doing. Co-Authored-By: Claude <noreply@anthropic.com>
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.
Summary
cohere.Client(api_key="")(andClientV2/AsyncClient/AsyncClientV2) now sends noAuthorizationheader, so the client can point at a proxy / self-hosted deployment that performs its own auth. A non-empty key — including a callable/async token supplier — behaves exactly as before.No generated file changes: the patch lives entirely in the already-handwritten, already-
.fernignoredsrc/cohere/overrides.py, which wraps the generated header builders at import time:The async variant keys off the rendered value (
"Bearer ") becauseasync_get_headersre-sets the header fromself._async_token()afterget_headers()runs. The patch is idempotent (guarded byBaseClientWrapper._omits_empty_authorization) sincerun_overrides()can be invoked more than once.Note on the base branch: this targets
upstream-main, a branch I pushed at currentcohere-ai/cohere-pythonmain(050f9c0), because this fork'smainis from 2024 and conflicts with upstream — that keeps the diff to just these two files. Happy to re-open against a freshfern-demo/cohere-pythonfork.Verification (
tests/test_optional_auth.py, header inspection only, no live calls): empty key omits the header on sync/async v1/v2 and forapi_key=lambda: "";api_key="n/a"still yieldsBearer n/a.pytest tests/test_optional_auth.py tests/test_client_init.py→ 4 passed, 1 skipped (boto3);mypy src/cohere/overrides.pyclean. Reverting the empty-token check fails exactly the 2 relevant tests.Link to Devin session: https://app.devin.ai/sessions/56fc23472ad04e0cbadf0e7f45d0893b