Skip to content

Bedrock: list what Converse can be asked with, use the API key, work inside an application that owns the AWS SDK - #73

Merged
alexey-milovidov merged 3 commits into
gemini-list-chat-modelsfrom
bedrock-converse-models
Dec 5, 2026
Merged

alexey-milovidov merged 3 commits into
gemini-list-chat-modelsfrom
bedrock-converse-models

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Dec 5, 2026 •

Copy link
Copy Markdown
Member

The Bedrock provider, as ClickHouse needs it for the AI agent of clickhouse-client. Stacked on #72: the base of this pull request is its branch, so only the changes of the Bedrock provider are shown.

  • The list of models. Client::list_models returned the foundation model IDs of the Region. A current model is invoked through an inference profile and not by its own ID, so the list had IDs that Converse refuses and lacked the ones it takes. It now returns the system-defined inference profiles of the models that output text (ListInferenceProfiles, all pages, ACTIVE only), followed by the text models that are invoked on demand by their own IDs (ListFoundationModels with byOutputModality=TEXT). An error of either request names the operation and the exception of AWS.
  • The API key of Amazon Bedrock (AWS_BEARER_TOKEN_BEDROCK) was never used: the AWS clients were constructed with a credentials provider, and such a client only signs. When the variable is set and the configuration names no identity of its own (a profile, a role, a web identity token), the clients are constructed with the bearer token scheme alone. The AWS SDK sends such a token only to an https endpoint.
  • BedrockConfig::aws_sdk_initialized_by_application, for an application that calls Aws::InitAPI and Aws::ShutdownAPI itself. The provider called Aws::InitAPI in any case; the AWS SDK counts the calls, so the second one initializes nothing and makes the Aws::ShutdownAPI of the application do nothing as well. The AWS SDK was then never shut down, and one of its threads used the logging after the static destructors had destroyed it: a segmentation fault when the process exits.
  • The build against an AWS SDK with custom memory management, where Aws::String is not std::string: the conversions are explicit.
  • InputValidator::is_valid_model_id matched the ID against five regular expressions, which refused IDs that Bedrock lists. It refuses only what cannot be an identifier now.
  • The regular expressions of CredentialRedactor were objects of namespace scope, compiled before main by every program that links the provider. They are built on first use, and two that nothing used are gone.
  • No request to the EC2 instance metadata service for a Region that is known: the configuration of an AWS client was default-constructed, which asks for the Region although it is set from BedrockConfig right after. The credential chain is made separately and still uses the role of the instance.
  • The list of models without Aws::Bedrock::BedrockClient. The generated client of the control plane has all 108 operations of the service as virtual functions, so a program that links the AWS SDK statically carries all of them, with their requests and responses, to call two (in ClickHouse: two fifths of what the provider added to the binary, and 557 files of the AWS SDK to compile). ControlPlaneClient in model_discovery.cpp has these two operations on the same base class (smithy::client::AwsSmithyClientT) with the same parameters as the generated client, and uses the generated requests and results, so the requests are signed, sent to the same endpoint and read in the same way. A build that links the whole aws-cpp-sdk-bedrock library is not affected.
  • ai/ai.h includes ai/bedrock.h when the provider is built.

The CI of this repository does not build the Bedrock provider (AI_SDK_ENABLE_BEDROCK is off), so it does not check this change beyond the formatting. It was built and tested inside ClickHouse, against a mock of Bedrock (requests signed with SigV4 and requests with the API key over TLS, the list of models in two pages, a tool call round trip), and the unit tests of the validator and of the redactor were run standalone. The real Amazon Bedrock was not called.

Related: #72
Related: ClickHouse/aws-sdk-cpp#48
Related: ClickHouse/ClickHouse#137792

🤖 Generated with Claude Code

alexey-milovidov and others added 2 commits December 5, 2026 07:13
…inside an application that owns the AWS SDK

`Client::list_models` of the Bedrock provider returned the foundation model IDs
of the Region. A current model is invoked through an inference profile and not
by its own ID, so the list had IDs that Converse refuses and lacked the ones it
takes. `ModelDiscovery::list_converse_model_ids` returns the system-defined
inference profiles of the models that output text (`ListInferenceProfiles`, all
pages, `ACTIVE` only), followed by the text models that are invoked on demand
by their own IDs (`ListFoundationModels` with `byOutputModality=TEXT`). An
error of either request names the operation and the exception of AWS.

An API key of Amazon Bedrock in the `AWS_BEARER_TOKEN_BEDROCK` environment
variable was never used: the AWS clients were constructed with a credentials
provider, and such a client signs with it and has no other way to authorize a
request, so with the key alone every request failed to be signed. When the
variable is set and the configuration names no identity of its own (a profile,
a role, a web identity token), the clients are constructed with the bearer
token scheme alone, with the key as its token. The AWS SDK sends such a token
only to an `https` endpoint.

`BedrockConfig::aws_sdk_initialized_by_application` is for an application that
calls `Aws::InitAPI` and `Aws::ShutdownAPI` itself. The provider called
`Aws::InitAPI` in any case. The AWS SDK counts the calls: the second one
initializes nothing, and it makes the `Aws::ShutdownAPI` of the application do
nothing as well. The AWS SDK was then never shut down, and a thread of it that
was still running when the process exited used the logging that the static
destructors had destroyed already: a segmentation fault at exit.

The provider did not compile against an AWS SDK that is built with custom
memory management, where `Aws::String` has an allocator of its own and is not
`std::string`: the conversions are explicit now.

`InputValidator::is_valid_model_id` matched the ID against five regular
expressions, which refused IDs that Bedrock lists. Which IDs exist is for
Bedrock to say: the validator refuses only what cannot be an identifier (an
empty one, or one with whitespace, control or non-ASCII characters).

The regular expressions of `CredentialRedactor` were objects of namespace
scope, so every program that links the provider compiled them before `main`.
They are built on first use, and two that nothing used are gone.

The configuration of an AWS client was default-constructed, which asks the EC2
instance metadata service for the Region when neither the environment nor the
profile has one, although the Region is set from `BedrockConfig` right after.
`client_configuration_with_known_region` skips that request. The credential
chain is made separately and still uses the role of the instance.

`ai/ai.h` includes `ai/bedrock.h` when the provider is built
(`AI_SDK_HAS_BEDROCK`), as it does for the other providers.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
`ModelDiscovery` used `Aws::Bedrock::BedrockClient`, which is generated with
every operation of the control plane of Bedrock (108 of them) as a virtual
function. A program that links the AWS SDK statically carries all of them, with
their requests and responses, although the provider calls only
`ListFoundationModels` and `ListInferenceProfiles`: in ClickHouse it was the
largest part of what the provider added to the binary, and 557 source files of
the AWS SDK to compile.

`ControlPlaneClient` in `model_discovery.cpp` is the same client without the
other operations: the same base class (`smithy::client::AwsSmithyClientT`) with
the same parameters as the generated client (the endpoint provider and the error
marshaller of Bedrock, the same authentication schemes), and the generated
requests and results of the two operations. So the requests are signed, sent to
the same endpoint and read in the same way. A build that links the whole
`aws-cpp-sdk-bedrock` library is not affected; a build that compiles the AWS SDK
itself needs only the endpoint provider, the error marshaller and the models of
the two operations from it.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
alexey-milovidov added a commit to ClickHouse/ClickHouse that referenced this pull request Dec 5, 2026
`Build (amd_msan)` failed on the `Relocation headroom` check: the binaries
took 6.1 to 6.4 MiB more of the reach of the 32-bit references, and a pull
request may take 5 MiB. Two fifths of what the provider added was the client
of the control plane of Bedrock, `Aws::Bedrock::BedrockClient`: it is
generated with all 108 operations of the service as virtual functions, so
the linker kept all of them, with their requests and responses, although the
provider calls two (`ListFoundationModels` and `ListInferenceProfiles`).

The SDK now has a client of its own with these two operations, on the same
base class of the AWS SDK and with the same parameters as the generated one
(ClickHouse/ai-sdk-cpp#73), so `BedrockClient.cpp` is
not built, and of the 557 files of the control plane only the 21 that the two
operations need are: the endpoint provider, the error marshaller, and the
requests and results of the two operations.

The stripped `clickhouse` binary of a reduced build on aarch64 now grows by
1.47 MB (0.57%) instead of 2.39 MB (0.93%), with 51 more static
initializers instead of 114, and the build compiles 290 more files of the
AWS SDK instead of 826.

The test `06038_client_ai_bedrock_provider` pressed Backspace, which leaves
the AI chat mode, as soon as it had read the answer. When that was before
the line editor took the terminal over, the terminal itself erased a
character of a line that had none, the client never read the key, and the
test waited for a prompt that did not come (1 run of 4 on a loaded
machine). It waits for the prompt of the AI chat mode first: 30 runs of 30
pass.

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=137792&sha=12e5a66485a1f3339ca4fc6dd2681c3387b25ae6&name_0=PR&name_1=Build%20%28amd_msan%29
#137792

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 8172f28. Configure here.

{smithy::SigV4AuthSchemeOption::sigV4AuthSchemeOption,
smithy::BearerTokenAuthSchemeOption::
bearerTokenAuthSchemeOption})),
auth_schemes) {}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

API key listing prefers SigV4

High Severity

ControlPlaneClient always resolves SigV4 before bearer, including when uses_bedrock_api_key installs only the Bedrock API-key scheme. The SDK selects that first scheme, which is absent from the API-key map, so ListFoundationModels and ListInferenceProfiles fail instead of sending AWS_BEARER_TOKEN_BEDROCK.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 8172f28. Configure here.

@alexey-milovidov alexey-milovidov self-assigned this Dec 5, 2026
@alexey-milovidov
alexey-milovidov merged commit 94631c3 into gemini-list-chat-models Dec 5, 2026
1 check passed
alexey-milovidov added a commit to ClickHouse/ClickHouse that referenced this pull request Dec 5, 2026
… for Amazon Bedrock

The question was whether the provider can reuse the AWS SDK that is already
linked: #137792 (comment)
It can, in two ways.

The requests are sent, signed and retried by `aws-cpp-sdk-core`, which
`contrib/aws-cmake` builds anyway, over the HTTP client of ClickHouse. The
provider of the AI SDK writes and reads the JSON of the Converse API itself
(ClickHouse/ai-sdk-cpp#73), so none of the generated
clients of `bedrock-runtime`, `bedrock` and `sts` is built: only the endpoint
rules of the two services, 6 files instead of 290.

The credentials of AWS are found by `S3::S3CredentialsProviderChain`, the chain
that ClickHouse has for the other services of AWS, and given to the provider.
The default chain of the AWS SDK is not linked for it. It also could not work
here for a web identity token or for a profile with a role: it asks AWS STS
through the HTTP client of the AWS Common Runtime, which ClickHouse builds
without TLS. When there are no credentials, the client says where it has looked
for them and sends nothing, instead of sending a request without a signature.

The stripped `clickhouse` binary (aarch64, a reduced build with the AWS SDK)
grows by 200,016 bytes with the provider instead of 1,465,336, and gets 2 more
static initializers instead of 51.

The mock of `06038_client_ai_bedrock_provider` now checks the signature of every
signed request, the way a service of AWS does: from the path as it has arrived.
It found that the path that was signed was not the path that was sent whenever
the model has a colon, as nearly every model of Bedrock has: the AWS SDK signs
a colon as it is, and `PocoHTTPClient` sends it encoded. It was so with the
generated clients as well. The provider now asks the AWS SDK for the strict
encoding, which is what `PocoHTTPClient` sends. The test also asks a model by
its ARN, which needs the previous commit.

`contrib/aws` is back at the commit of `master`: the patch of
ClickHouse/aws-sdk-cpp#48 was for the generated clients.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
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.

1 participant