Add api_key: false to omit x-api-key header on ChatAnthropic#585
Conversation
Some deployments front the Anthropic Messages API with their own authentication — a corporate proxy, or an AWS SigV4-signed gateway such as Bedrock "Mantle" — where the `x-api-key` header is rejected or ignored. There was previously no way to suppress it on the direct (non-`BedrockConfig`) path: `headers/1` always injected `x-api-key`. Setting `api_key: false` now omits the header entirely, letting the caller's own auth (e.g. SigV4 signing supplied via `req_opts`) take over. `nil` and string API keys behave exactly as before. Because `:api_key` is an Ecto `:string` field, the boolean is pulled out of attrs before `cast/3` and set with `put_change/3`, so casting doesn't reject it.
|
@jersearls Will your situation work with just not setting the What happens if you don't set the API key at all? If you have a separate auth channel, is there something in the library preventing you from doing it? With Elixir moving more into a fully typed language, I don't like the idea of defining |
|
Hey Mark — thanks for taking a look, and I really appreciate the pushback on the typing angle. You're right that stuffing I tried the “just leave api_key || Config.resolve(:anthropic_key, "")So with
Either way the header is present, and those self-authenticated endpoints reject or ignore it. ( Totally happy to reshape this so we don't overload a string field. Would you prefer something like:
I'll go with whatever fits the direction you want for the library — the only goal on my side is a supported escape hatch so callers with their own auth (e.g. SigV4 via |
|
Thanks @jersearls for the additional detail. I lean towards a new, dedicated boolean key like |
Per maintainer feedback, replace the overloaded `api_key: false` (which stuffed a boolean into the :string field and required a put_change hack) with a dedicated `suppress_api_key` boolean field. Keeps :api_key strictly a string, so no type warnings as Elixir leans into typing. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
Sounds good — went with
ChatAnthropic.new!(%{
endpoint: "https://my-gateway.example.com/anthropic/v1/messages",
model: "claude-sonnet-4-5",
suppress_api_key: true,
req_opts: [aws_sigv4: [...]]
})Tests updated (default is |
Motivation
Some deployments front the Anthropic Messages API with their own authentication rather than an Anthropic key — a corporate egress proxy, or an AWS SigV4-signed gateway such as Bedrock "Mantle". On the direct (non-
BedrockConfig)ChatAnthropicpath,headers/1unconditionally injectsx-api-key, which those endpoints reject or ignore. There was no supported way to suppress it, so the only workaround was monkeypatching the dep at build time.Change
api_key: falsenow omits thex-api-keyheader entirely, letting the caller's own auth take over (for example, SigV4 signing supplied throughreq_opts).niland string API keys behave exactly as before.Because
:api_keyis an Ecto:stringfield,cast/3would reject the boolean, so thefalseis pulled out of the attrs before casting and set viaput_change/3. Thex-api-key/get_api_keybehavior is unchanged for every other value.Tests
Added a
describe "api_key: false"block covering:new!/1and string-keyednew/1acceptingfalse, the header being omitted whenfalse, and still sent for a string key. Fullchat_anthropic_test.exssuite passes (147 tests, 0 failures), formatted, no new compiler warnings.Notes
Happy to adjust the shape (naming, docs, or an alternative like an explicit
skip_api_keyfield) if you'd prefer a different API — the goal is just a supported escape hatch for self-authenticated endpoints.