Skip to content

Commit 2b42f27

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 2b42f27

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
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: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -588,17 +588,20 @@ REGION is one of apac, eu or us."
588588

589589
(defun gptel-bedrock--curl-args (region)
590590
"Generate the curl arguments to get a bedrock request signed for use in REGION."
591-
;; 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))))))
591+
(let ((bearer-token (getenv "AWS_BEARER_TOKEN_BEDROCK"))
592+
(output-args (unless (memq system-type '(windows-nt ms-dos))
593+
'("--output" "/dev/stdout"))))
594+
(if bearer-token
595+
(append
596+
(list "-H" (format "Authorization: Bearer %s" bearer-token))
597+
output-args)
598+
(cl-multiple-value-bind (key-id secret token) (gptel-bedrock--get-credentials)
599+
(append
600+
(list "--user" (format "%s:%s" key-id secret)
601+
"--aws-sigv4" (format "aws:amz:%s:bedrock" region))
602+
output-args
603+
(when token (list "-H" (format "x-amz-security-token: %s" token)))))))
604+
602605

603606
(defun gptel-bedrock--curl-version ()
604607
"Check Curl version required for gptel-bedrock."
@@ -627,9 +630,10 @@ STREAM - Whether to use streaming responses or not.
627630
REQUEST-PARAMS - a plist of additional HTTP request
628631
parameters (as plist keys) and values supported by the API."
629632
(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)))
633+
(unless (getenv "AWS_BEARER_TOKEN_BEDROCK")
634+
(unless (and gptel-use-curl (version<= "8.9" (gptel-bedrock--curl-version)))
635+
(error "Bedrock-backend requires curl >= 8.9, but gptel-use-curl := %s, curl-version := %s"
636+
gptel-use-curl (gptel-bedrock--curl-version))))
633637
(let ((host (format "bedrock-runtime.%s.amazonaws.com" region)))
634638
(setf (alist-get name gptel--known-backends nil nil #'equal)
635639
(gptel--make-bedrock

0 commit comments

Comments
 (0)