Skip to content

Commit 99fbf56

Browse files
committed
gptel-bedrock: AWS_BEARER_TOKEN_BEDROCK support
AWS has introduced Bearer token based authentication for Bedrock https://docs.aws.amazon.com/bedrock/latest/userguide/api-keys.html
1 parent 52748d0 commit 99fbf56

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@
7070
- gptel now handles Ollama models that return both reasoning content
7171
and tool calls in a single request.
7272

73+
- ~gptel-make-bedrock~ now checks for the ~AWS_BEARER_TOKEN_BEDROCK~ environment
74+
variable parameter and uses it for Bedrock API key based authentication if
75+
present. See
76+
https://docs.aws.amazon.com/bedrock/latest/userguide/api-keys.html.
77+
7378
* 0.9.8.5 2025-06-11
7479

7580
** Breaking changes

README.org

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,10 +1060,10 @@ Register a backend with
10601060
:model-region 'apac)
10611061
#+end_src
10621062

1063-
The Bedrock backend gets your AWS credentials from the environment variables. It expects to find either
1063+
The Bedrock backend gets your AWS credentials from the environment variables. If ~AWS_BEARER_TOKEN_BEDROCK~ is present, it uses the bearer token. Otherwise it expects to find either
10641064
~AWS_ACCESS_KEY_ID~, ~AWS_SECRET_ACCESS_KEY~, ~AWS_SESSION_TOKEN~ (optional), or if present, can use ~AWS_PROFILE~ to get these directly from the ~aws~ cli.
10651065

1066-
NOTE: The Bedrock backend needs curl >= 8.5 in order for the sigv4 signing to work properly,
1066+
NOTE: Unless ~AWS_BEARER_TOKEN_BEDROCK~ token is used, the Bedrock backend needs curl >= 8.9 in order for the sigv4 signing to work properly,
10671067
https://github.com/curl/curl/issues/11794
10681068

10691069
An error will be signalled if ~gptel-curl~ is ~NIL~.

gptel-bedrock.el

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -589,16 +589,18 @@ REGION is one of apac, eu or us."
589589
(defun gptel-bedrock--curl-args (region)
590590
"Generate the curl arguments to get a bedrock request signed for use in REGION."
591591
;; https://curl.se/docs/manpage.html#--aws-sigv4
592-
(cl-multiple-value-bind (key-id secret token) (gptel-bedrock--get-credentials)
593-
(nconc
594-
(list
595-
"--user" (format "%s:%s" key-id secret)
596-
"--aws-sigv4" (format "aws:amz:%s:bedrock" region))
597-
(unless (memq system-type '(windows-nt ms-dos))
598-
;; Without this curl swallows the output
599-
(list "--output" "/dev/stdout"))
600-
(when token
601-
(list (format "-Hx-amz-security-token: %s" token))))))
592+
(if ((getenv "AWS_BEARER_TOKEN_BEDROCK")
593+
(format "-H Authorization: Bearer %s" (getenv "AWS_BEARER_TOKEN_BEDROCK")))
594+
(cl-multiple-value-bind (key-id secret token) (gptel-bedrock--get-credentials)
595+
(nconc
596+
(list
597+
"--user" (format "%s:%s" key-id secret)
598+
"--aws-sigv4" (format "aws:amz:%s:bedrock" region))
599+
(unless (memq system-type '(windows-nt ms-dos))
600+
;; Without this curl swallows the output
601+
(list "--output" "/dev/stdout"))
602+
(when token
603+
(list (format "-Hx-amz-security-token: %s" token)))))))
602604

603605
(defun gptel-bedrock--curl-version ()
604606
"Check Curl version required for gptel-bedrock."
@@ -627,9 +629,10 @@ STREAM - Whether to use streaming responses or not.
627629
REQUEST-PARAMS - a plist of additional HTTP request
628630
parameters (as plist keys) and values supported by the API."
629631
(declare (indent 1))
630-
(unless (and gptel-use-curl (version<= "8.9" (gptel-bedrock--curl-version)))
631-
(error "Bedrock-backend requires curl >= 8.9, but gptel-use-curl := %s, curl-version := %s"
632-
gptel-use-curl (gptel-bedrock--curl-version)))
632+
(unless (getenv "AWS_BEARER_TOKEN_BEDROCK")
633+
(unless (and gptel-use-curl (version<= "8.9" (gptel-bedrock--curl-version)))
634+
(error "Bedrock-backend requires curl >= 8.9, but gptel-use-curl := %s, curl-version := %s"
635+
gptel-use-curl (gptel-bedrock--curl-version))))
633636
(let ((host (format "bedrock-runtime.%s.amazonaws.com" region)))
634637
(setf (alist-get name gptel--known-backends nil nil #'equal)
635638
(gptel--make-bedrock

0 commit comments

Comments
 (0)