Repository navigation
feat(auth): add auth=False for self-hosted servers without authentication - #833
GregHolmes wants to merge 8 commits into
Conversation
…tion Self-hosted and local Deepgram servers can run without request authentication, but the client could not be constructed without an API key, so there was no way to connect without inventing one. DeepgramClient and AsyncDeepgramClient now accept auth=False: no key is required, DEEPGRAM_API_KEY is ignored, and no Authorization header is sent on HTTP requests or WebSocket handshakes. It cannot be combined with api_key or access_token. When no key is available and the environment is not a Deepgram host, the missing-key error now suggests auth=False. The message for Deepgram's hosted API is unchanged apart from fixing "instantiated be either". Also documents the self-hosted setup in the README.
|
An environment base with the scheme left off, such as "127.0.0.1:8000", parses with no hostname. It was treated as Deepgram's hosted API, so the developer got the bare missing-key error with no hint. A base that can't be parsed or has no host can never be Deepgram's; only an unset environment counts as hosted.
dg-coreylweathers
left a comment
There was a problem hiding this comment.
Thanks Greg, this is a feature people have been asking for since the v5 rewrite. The core of it works: I traced the header strip through every HTTP and WebSocket path, ran the test suite in Docker, and tried the shapes existing users rely on (explicit key, None, access_token, env var) and nothing changed for them. Requesting changes for one thing, and I'd like your call on a second before this merges.
1. auth=False quietly removes auth from an httpx_client the developer passed in, for everything that client does, not just Deepgram calls.
The code deletes the Authorization default header and sets .auth = None on their client object, at construction and again on every request (src/deepgram/core/http_client.py:195-200, called from :480, :657, :806, :986). I checked it: after building the Deepgram client with auth=False and a shared httpx.Client, a plain shared.get("http://other-service/") went out with no auth at all. Nothing in the README warns about this. At minimum, add a sentence after README line 253:
If you pass your own
httpx_clientwithauth=False, the SDK removes anyAuthorizationdefault header and anyauthsetting from that client object, so it won't authenticate other requests you make with it either.
Better: only strip the header on requests going to the configured Deepgram hosts and leave their client alone. Point 2 gets you there.
2. Your call: do we really want to freeze http_client.py?
It's a 1010-line generated file that Fern has rewritten in six of the last regens (retries, logging, redaction, multipart, query encoding). Freezing it for a 33-line patch means diffing a .bak of it every cycle. There's a way to get the same result from client.py, which is already frozen: an httpx request event hook on the underlying client (event_hooks["request"], async version for AsyncClient). It runs after httpx has applied default headers and auth, so it catches everything. I tried it with MockTransport across per-request headers, client default headers and basic auth, on request() and stream(), sync and async, and all six came back with no Authorization. If the hook checks request.url.host against the environment's base and agent_rest hosts, it also fixes point 1 without touching the caller's client. You own the Fern pipeline, so if you'd rather keep the freeze I won't push back, but I wanted the option on the table.
Smaller things
- Only Listen v2 is covered by the WebSocket handshake test. Listen v1, Speak v1, Speak v2 and Agent v1 use the same generated pattern today, but a regen could change one of them without anyone noticing. Parametrizing the existing
_CaptureConnecttest over all five would close that. - The
auth=Trueerror says what it won't accept but not what to do instead. Suggest:auth only accepts False, which disables authentication for self-hosted servers. To authenticate, omit auth and pass api_key or access_token. - The PR description still says all changes are in
client.pyand that there are 19 tests. After the later commits it's two source files and 24 tests. Worth fixing since the body becomes the squash commit message. - README heading could be shorter:
### Self-Hosted Servers Without Authentication.
One thing outside this repo: the public self-hosted docs page (using-sdks-with-self-hosted.mdx, lines 32 and 52) still tells Python users to pass api_key="a" as a placeholder. Once this ships that page should say auth=False.
What I verified: all 20 generated WebSocket connect sites build headers in the way the override intercepts, and the real websockets library accepts the header map. The HTTP strip covers request and stream paths and re-runs on retries. The JS parity claim holds (BaseClient.ts swaps in NoOpAuthProvider on auth: false). In Docker with python 3.12 and the CI commands: new test file 24 passed, tests/custom + tests/utils 1140 passed, mypy clean, ruff clean on the PR's files. CI green on 3.10 to 3.13.
|
Addressed in ccbfc25.
The separate public self-hosted documentation page should change from Verified: 32 focused tests; full suite 1,207 passed, 4 skipped, 1 expected failure; |
httpx keeps an explicit default port when the URL scheme is uppercase, so an environment base like HTTP://localhost:80 produced request.url.port == 80 while _origin had normalized it to None and the Authorization strip was skipped. Run the request URL through the same helper so both sides agree. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
dg-coreylweathers
left a comment
There was a problem hiding this comment.
Re-reviewed ccbfc25. Everything I asked for is in, and the shape is better than what I suggested: gating the strip on the session ID as well as the origin means two SDK clients sharing one httpx.Client behave independently, which I hadn't thought about. Approving.
What I checked against the new commit, in Docker (python 3.12, the CI commands):
- A shared
httpx.Clientwith a defaultAuthorizationheader and basic auth: the SDK's own request goes out without the header, a direct request to the same host keeps it, and the client's.authis untouched. Same forAsyncClient, and the async hook is a real coroutine so httpx accepts it. - A second, keyed
DeepgramClienton the same httpx client still sends its key while theauth=Falseone doesn't. - Retries: 503 then 200, both attempts stripped.
- Origin matching: explicit
:80/:443, mixed-case host, IPv6[::1], andagent_reston a different port frombaseall match. http_client.pyis byte-identical tomain; it's out of.fernignoreandAGENTS.md.- All five WebSocket surfaces now have sync and async handshake tests.
- Focused file 32 passed;
tests/custom+tests/utils1148 passed; mypy and ruff clean on the PR files; CI green on 3.10 to 3.13.
One small thing I fixed on the branch (838df8d) rather than bounce back to you: httpx keeps the default port when the scheme is uppercase, so base="HTTP://localhost:80" gave request.url.port == 80 while _origin had normalized it to None, and the strip was skipped. The request URL now goes through the same _origin helper, with a regression test. Post-fix: focused file 33 passed, tests/custom + tests/utils 1149 passed, mypy and ruff clean. Revert if you'd rather handle it differently.
The earlier trailing-dot-host nit is still open and still harmless. Agreed on sequencing the docs page change after the release.
Proposed changes
Self-hosted and local Deepgram servers can run without request authentication. The Python client can't connect to one without inventing a key: constructing
DeepgramClientorAsyncDeepgramClientwith noapi_keyand noDEEPGRAM_API_KEYraises:This adds
auth=False, matching the JavaScript SDK'sauth: false:DEEPGRAM_API_KEYis ignored.Authorizationheader is sent on this SDK client's HTTP requests or WebSocket handshakes. The client’s HTTPX request hook runs after HTTPX applies default headers and authentication, and removes the header only for this client’s session-ID-tagged requests to its configuredbaseandagent_restorigins. A shared caller-provided HTTPX client keeps its authentication for other requests.auth=Falsecan't be combined withapi_keyoraccess_token, andauthonly acceptsFalse.baseis notdeepgram.comor*.deepgram.com, the missing-key error ends with a hint to passauth=False. Only an unset environment counts as hosted; a base that can't be parsed or has no host (for example127.0.0.1:8000) gets the hint. For Deepgram's hosted API the message is unchanged, apart from fixing the generated "instantiated be either" wording: the custom client now raises the missing-key error itself in both cases, as it already did for an explicitapi_key=None.All credential handling lives in the permanently frozen
src/deepgram/client.py, shared by sync and async clients. The existingaccess_token, explicit-None, andDEEPGRAM_API_KEYre-read behavior remains unchanged.The README gains a "Self-Hosted Servers Without Authentication" section under Authentication.
Types of changes
Checklist
Further comments
tests/custom/test_self_hosted_no_auth.py(added to.fernignore), 32 tests:DEEPGRAM_API_KEYignored underauth=False.Authorizationheader over HTTP (sync and async, regular and streaming) and on every Listen v1/v2, Speak v1/v2, and Agent v1 WebSocket handshake (sync public client and async raw client).ValueErrorwhen combined withapi_keyoraccess_token, and for values other thanFalse.api_key=None, the async client, a look-alike host (notdeepgram.com), and a base with the scheme left off (127.0.0.1:8000, which parses with no host); the unchanged message for the default environment andapi.eu.deepgram.com.api_keyis still sent to a self-hosted environment.The focused suite passes 32/32.
ruff check,ruff format --check, andmypy src/deepgram/client.pyare clean.🤖 Generated with Claude Code