Skip to content

feat(auth): add auth=False for self-hosted servers without authentication - #833

Open
GregHolmes wants to merge 8 commits into
mainfrom
feat/self-hosted-no-auth
Open

GregHolmes wants to merge 8 commits into
mainfrom
feat/self-hosted-no-auth

Conversation

@GregHolmes

@GregHolmes GregHolmes commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

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 DeepgramClient or AsyncDeepgramClient with no api_key and no DEEPGRAM_API_KEY raises:

The client must be instantiated by either passing in api_key or setting DEEPGRAM_API_KEY

This adds auth=False, matching the JavaScript SDK's auth: false:

client = DeepgramClient(
    environment=DeepgramClientEnvironment(
        base="http://127.0.0.1:8000",
        production="ws://127.0.0.1:8000",
        agent="ws://127.0.0.1:8000",
        agent_rest="http://127.0.0.1:8000",
    ),
    auth=False,
)
  • No key is required and DEEPGRAM_API_KEY is ignored.
  • No Authorization header 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 configured base and agent_rest origins. A shared caller-provided HTTPX client keeps its authentication for other requests.
  • auth=False can't be combined with api_key or access_token, and auth only accepts False.
  • When no key is available and the environment's base is not deepgram.com or *.deepgram.com, the missing-key error ends with a hint to pass auth=False. Only an unset environment counts as hosted; a base that can't be parsed or has no host (for example 127.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 explicit api_key=None.

All credential handling lives in the permanently frozen src/deepgram/client.py, shared by sync and async clients. The existing access_token, explicit-None, and DEEPGRAM_API_KEY re-read behavior remains unchanged.

The README gains a "Self-Hosted Servers Without Authentication" section under Authentication.

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update or tests (if none of the other choices apply)

Checklist

  • I have read the CONTRIBUTING doc
  • Lint and unit tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

Further comments

tests/custom/test_self_hosted_no_auth.py (added to .fernignore), 32 tests:

  • Construction without a key (sync and async), and DEEPGRAM_API_KEY ignored under auth=False.
  • No Authorization header 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).
  • ValueError when combined with api_key or access_token, and for values other than False.
  • A supplied HTTPX client's default headers and basic-auth setting remain intact for direct requests, including to the same host; SDK requests still omit authorization.
  • The hint for a loopback environment, an explicit 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 and api.eu.deepgram.com.
  • An explicit api_key is still sent to a self-hosted environment.

The focused suite passes 32/32. ruff check, ruff format --check, and mypy src/deepgram/client.py are clean.

🤖 Generated with Claude Code

…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.
@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Code Coverage

Package Line Rate Branch Rate Complexity Health
src.deepgram 95% 91% 0 ✔
src.deepgram.agent 100% 100% 0 ✔
src.deepgram.agent.v1 98% 100% 0 ✔
src.deepgram.agent.v1.settings 100% 100% 0 ✔
src.deepgram.agent.v1.settings.think 100% 100% 0 ✔
src.deepgram.agent.v1.settings.think.models 97% 100% 0 ✔
src.deepgram.auth 100% 100% 0 ✔
src.deepgram.auth.v1 100% 100% 0 ✔
src.deepgram.auth.v1.tokens 97% 100% 0 ✔
src.deepgram.core 89% 84% 0 ➖
src.deepgram.errors 100% 100% 0 ✔
src.deepgram.helpers 98% 92% 0 ✔
src.deepgram.listen 100% 100% 0 ✔
src.deepgram.listen.v1 98% 93% 0 ✔
src.deepgram.listen.v1.media 97% 100% 0 ✔
src.deepgram.listen.v2 98% 93% 0 ✔
src.deepgram.manage 100% 100% 0 ✔
src.deepgram.manage.v1 100% 100% 0 ✔
src.deepgram.manage.v1.models 96% 100% 0 ✔
src.deepgram.manage.v1.projects 97% 100% 0 ✔
src.deepgram.manage.v1.projects.billing 100% 100% 0 ✔
src.deepgram.manage.v1.projects.billing.balances 96% 100% 0 ✔
src.deepgram.manage.v1.projects.billing.breakdown 97% 100% 0 ✔
src.deepgram.manage.v1.projects.billing.fields 97% 100% 0 ✔
src.deepgram.manage.v1.projects.billing.purchases 97% 100% 0 ✔
src.deepgram.manage.v1.projects.keys 96% 100% 0 ✔
src.deepgram.manage.v1.projects.members 97% 100% 0 ✔
src.deepgram.manage.v1.projects.members.invites 96% 100% 0 ✔
src.deepgram.manage.v1.projects.members.scopes 96% 100% 0 ✔
src.deepgram.manage.v1.projects.models 96% 100% 0 ✔
src.deepgram.manage.v1.projects.usage 98% 100% 0 ✔
src.deepgram.manage.v1.projects.usage.breakdown 97% 100% 0 ✔
src.deepgram.manage.v1.projects.usage.fields 97% 100% 0 ✔
src.deepgram.read 100% 100% 0 ✔
src.deepgram.read.v1 100% 100% 0 ✔
src.deepgram.read.v1.text 98% 100% 0 ✔
src.deepgram.self_hosted 100% 100% 0 ✔
src.deepgram.self_hosted.v1 100% 100% 0 ✔
src.deepgram.self_hosted.v1.distribution_credentials 96% 100% 0 ✔
src.deepgram.speak 100% 100% 0 ✔
src.deepgram.speak.v1 98% 97% 0 ✔
src.deepgram.speak.v1.audio 91% 80% 0 ✔
src.deepgram.speak.v2 98% 93% 0 ✔
src.deepgram.speak.v2.audio 100% 100% 0 ✔
src.deepgram.voice_agent 100% 100% 0 ✔
src.deepgram.voice_agent.configurations 95% 100% 0 ✔
src.deepgram.voice_agent.variables 95% 100% 0 ✔
Summary 96% (6687 / 6994) 92% (1479 / 1606) 0 ✔

Scope: hand-maintained SDK logic. Fern-generated data models (types/, requests/), package __init__.py files, version.py, and the unused core/http_sse/ scaffolding are excluded — see .coveragerc. Unscoped whole-package coverage is ~70%.

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 dg-coreylweathers left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_client with auth=False, the SDK removes any Authorization default header and any auth setting 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 _CaptureConnect test over all five would close that.
  • The auth=True error 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.py and 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.

@GregHolmes

Copy link
Copy Markdown
Contributor Author

Addressed in ccbfc25.

  • Replaced the generated http_client.py patch with an HTTPX request hook in the already hand-maintained client.py, and removed http_client.py from .fernignore and AGENTS.md.
  • The hook runs after HTTPX applies default headers/authentication, but removes Authorization only when the request has this SDK client's session ID and targets its configured base or agent_rest origin. A caller's shared HTTPX client keeps its default headers and auth, including for a direct request to the same host.
  • Expanded WebSocket handshake coverage across Listen v1/v2, Speak v1/v2, and Agent v1 for the sync public client and async raw client.
  • Updated the auth=True validation error, README heading and shared-HTTPX explanation, and the PR body (one handwritten source file; 32 focused tests).

The separate public self-hosted documentation page should change from api_key="a" to auth=False after this SDK release is published, so the docs do not describe an unreleased option.

Verified: 32 focused tests; full suite 1,207 passed, 4 skipped, 1 expected failure; mypy src/, mypy tests/typecheck, and targeted Ruff all pass. Repository-wide Ruff still reports 57 existing issues in unrelated examples and manual tests.

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 dg-coreylweathers left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.Client with a default Authorization header 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 .auth is untouched. Same for AsyncClient, and the async hook is a real coroutine so httpx accepts it.
  • A second, keyed DeepgramClient on the same httpx client still sends its key while the auth=False one doesn't.
  • Retries: 503 then 200, both attempts stripped.
  • Origin matching: explicit :80/:443, mixed-case host, IPv6 [::1], and agent_rest on a different port from base all match.
  • http_client.py is byte-identical to main; it's out of .fernignore and AGENTS.md.
  • All five WebSocket surfaces now have sync and async handshake tests.
  • Focused file 32 passed; tests/custom + tests/utils 1148 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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants