fix(auth): treat a blank api_key as unset so the OPENROUTER_API_KEY fallback still applies - #595
Open
rajarshidattapy wants to merge 1 commit into
Conversation
… authorization flow
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.
Fixes #591.
Problem
get_security_from_envonly falls back toOPENROUTER_API_KEYwhen no security was suppliedat all, so
OpenRouter(api_key="")short-circuits it — which is exactly what the documentedapi_key=os.getenv("OPENROUTER_API_KEY", "")pattern produces when the variable is unset.The failure mode is worse than #591 assumed. Verified against a stub server, no request is sent
at all; it dies inside
httpxbefore leaving the process:A
401at least points at credentials.Illegal header valuedoesn't mention them, and there isno request in any log to correlate against.
Change
#591 suggests fixing this at
sdk.py:190-196, but that file is generated and actively churns —every regen adds namespaces — so it cannot be
.genignored the waylogger.pycould in #590._hooks/registration.pyis the right home. It is the only file undersrc/without aDO NOT EDITheader, its own comment says it is generated once and then free to modify, andsdk.py:237callshooks.sdk_init(config)and assigns the result — so a hook can normalizeconfig.securityat construction, before anything reads it. Nothing is frozen and there isnothing to re-fix after a regeneration.
Full resolution matrix after the change:
A callable
api_keyis deliberately left untouched — it is resolved per-request, and inspectingit at init would defeat that. A callable returning
""still hits the old error; that is notedin the hook's docstring and is not the documented pattern.
Verification
Authorizationheader it receivestest_blank_api_key_falls_back_to_the_env_varandtest_no_credentials_anywhere_sends_no_authorization_headermypy769 files clean,pyright0 errors,pylint10.00/10Fix 1 (docs) is not included
The ~119
api_key=os.getenv("OPENROUTER_API_KEY", "")snippets live in generateddocs/sdks/**files..genignoreonly coversdocs/overview.mdx, and listing 119 generatedfiles would freeze them all. I looked for a
gen.yamlknob —usageSnippetsexposes onlyoptionalPropertyRenderingandsdkInitStyle, and security renders as a required constructorargument, so neither applies. That half needs Speakeasy to change how usage snippets render
security.
This hook makes the pattern harmless in the meantime, which is why it is worth landing
independently of what happens to the docs.