Class: Anthropic::Helpers::GoogleCloud::Client

Inherits:
Client show all
Defined in:
lib/anthropic/helpers/google_cloud/client.rb,
sig/anthropic/helpers/google_cloud/client.rbs

Overview

Client for Claude Platform on Google Cloud — the first-party Anthropic API served through the Google Cloud gateway.

This is distinct from VertexClient, which targets the :rawPredict publisher-model API (publisher model IDs, messages only). This client speaks the full first-party Anthropic API: requests pass through the gateway unchanged — standard /v1/* paths, standard model names, the complete API surface. The deprecated text Completions endpoint is intentionally not exposed.

Constant Summary collapse

SCOPE =

OAuth2 scope required to call the gateway.

Returns:

  • (String)
"https://www.googleapis.com/auth/cloud-platform"
URL_TEMPLATE =

Gateway base URL template; override via base_url or ANTHROPIC_GOOGLE_CLOUD_BASE_URL.

Returns:

  • (String)
"https://claude.googleapis.com/v1alpha" \
"/projects/%<project>s/locations/%<location>s/workspaces/%<workspace_id>s/invoke"
Anthropic =

Returns:

  • (:APIRequest req)

Constants inherited from Client

Client::DEFAULT_INITIAL_RETRY_DELAY, Client::DEFAULT_MAX_RETRIES, Client::DEFAULT_MAX_RETRY_DELAY, Client::DEFAULT_TIMEOUT_IN_SECONDS, Client::MODEL_NONSTREAMING_TOKENS

Constants inherited from Internal::Transport::BaseClient

Internal::Transport::BaseClient::MAX_REDIRECTS, Internal::Transport::BaseClient::PLATFORM_HEADERS

Instance Attribute Summary collapse

Attributes inherited from Client

#api_key, #auth_token, #credentials, #token_cache, #webhook_key

Attributes inherited from Internal::Transport::BaseClient

#base_url, #headers, #idempotency_header, #initial_retry_delay, #max_retries, #max_retry_delay, #middleware, #requester, #timeout

Instance Method Summary collapse

Methods inherited from Client

#calculate_nonstreaming_timeout, #retry_request?

Methods inherited from Internal::Transport::BaseClient

follow_redirect, #inspect, reap_connection!, #request, #retry_request?, #send_request, should_retry?, validate!

Methods included from Internal::Util::SorbetRuntimeSupport

#const_missing, #define_sorbet_constant!, #sorbet_constant_defined?, #to_sorbet_type, to_sorbet_type

Constructor Details

#initialize(project: ENV["ANTHROPIC_GOOGLE_CLOUD_PROJECT"] || ENV["GOOGLE_CLOUD_PROJECT"], location: ENV["ANTHROPIC_GOOGLE_CLOUD_LOCATION"] || "global", workspace_id: ENV["ANTHROPIC_GOOGLE_CLOUD_WORKSPACE_ID"], base_url: ENV["ANTHROPIC_GOOGLE_CLOUD_BASE_URL"], google_credentials: nil, token_provider: nil, skip_auth: false, max_retries: self.class::DEFAULT_MAX_RETRIES, timeout: self.class::DEFAULT_TIMEOUT_IN_SECONDS, initial_retry_delay: self.class::DEFAULT_INITIAL_RETRY_DELAY, max_retry_delay: self.class::DEFAULT_MAX_RETRY_DELAY, middleware: nil) ⇒ Client

Creates a new client for the Anthropic API on the Google Cloud gateway.

Authentication uses Google credentials by precedence (unless skip_auth is true):

1. `token_provider` — a callable returning a GCP access token, called
 per attempt
2. `google_credentials` — a `googleauth` credentials object (anything
 responding to `#apply`), refreshed by the library as needed
3. Application Default Credentials (`Google::Auth.get_application_default`)
 with the `cloud-platform` scope

Parameters:

  • project (String, nil) (defaults to: ENV["ANTHROPIC_GOOGLE_CLOUD_PROJECT"] || ENV["GOOGLE_CLOUD_PROJECT"])

    GCP consumer project id. Defaults to ENV["ANTHROPIC_GOOGLE_CLOUD_PROJECT"], then ENV["GOOGLE_CLOUD_PROJECT"], then the project reported by the resolved Google credentials. Only required when the base URL must be derived.

  • location (String, nil) (defaults to: ENV["ANTHROPIC_GOOGLE_CLOUD_LOCATION"] || "global")

    GCP location. Defaults to ENV["ANTHROPIC_GOOGLE_CLOUD_LOCATION"], then "global"; an explicit nil falls back to the same defaults.

  • workspace_id (String, nil) (defaults to: ENV["ANTHROPIC_GOOGLE_CLOUD_WORKSPACE_ID"])

    Defaults to ENV["ANTHROPIC_GOOGLE_CLOUD_WORKSPACE_ID"]. Required unless skip_auth is true and an explicit base_url is given.

  • base_url (String, nil) (defaults to: ENV["ANTHROPIC_GOOGLE_CLOUD_BASE_URL"])

    Override the gateway base URL. Defaults to ENV["ANTHROPIC_GOOGLE_CLOUD_BASE_URL"], otherwise derived from project, location, and workspace_id via URL_TEMPLATE.

  • google_credentials (#apply, nil) (defaults to: nil)

    A googleauth-compatible credentials object. Mutually exclusive with skip_auth.

  • token_provider (#call, nil) (defaults to: nil)

    A callable returning a GCP access token string. Takes precedence over google_credentials. Mutually exclusive with skip_auth.

  • skip_auth (Boolean) (defaults to: false)

    When true, no token is attached and workspace_id is only required when the base URL must be derived. For callers fronting with their own authenticated proxy. Mutually exclusive with token_provider and google_credentials.

  • max_retries (Integer) (defaults to: self.class::DEFAULT_MAX_RETRIES)

    Max number of retries to attempt after a failed retryable request.

  • timeout (Float) (defaults to: self.class::DEFAULT_TIMEOUT_IN_SECONDS)
  • initial_retry_delay (Float) (defaults to: self.class::DEFAULT_INITIAL_RETRY_DELAY)
  • max_retry_delay (Float) (defaults to: self.class::DEFAULT_MAX_RETRY_DELAY)
  • middleware (Array<#call>, #call, nil) (defaults to: nil)

    Per-attempt HTTP around-middleware. See Middleware. The Google bearer is applied per attempt, after user middleware.

Raises:

  • (ArgumentError)


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/anthropic/helpers/google_cloud/client.rb', line 98

def initialize( # rubocop:disable Lint/MissingSuper
  project: ENV["ANTHROPIC_GOOGLE_CLOUD_PROJECT"] || ENV["GOOGLE_CLOUD_PROJECT"],
  location: ENV["ANTHROPIC_GOOGLE_CLOUD_LOCATION"] || "global",
  workspace_id: ENV["ANTHROPIC_GOOGLE_CLOUD_WORKSPACE_ID"],
  base_url: ENV["ANTHROPIC_GOOGLE_CLOUD_BASE_URL"],
  google_credentials: nil,
  token_provider: nil,
  skip_auth: false,
  max_retries: self.class::DEFAULT_MAX_RETRIES,
  timeout: self.class::DEFAULT_TIMEOUT_IN_SECONDS,
  initial_retry_delay: self.class::DEFAULT_INITIAL_RETRY_DELAY,
  max_retry_delay: self.class::DEFAULT_MAX_RETRY_DELAY,
  middleware: nil
)
  if skip_auth && (token_provider || google_credentials)
    raise ArgumentError.new(
      "`skip_auth` is mutually exclusive with `token_provider` and `google_credentials`; " \
      "`skip_auth` disables authentication entirely."
    )
  end

  @skip_auth = skip_auth
  @token_provider = token_provider
  @authorization = google_credentials

  # Resolve credentials eagerly so the client is thread-safe (no lazy
  # ADC race) and so the credentials' project can backfill `project`
  # before the base URL is derived.
  if !skip_auth && token_provider.nil? && google_credentials.nil?
    require_googleauth
    @authorization = Google::Auth.get_application_default([SCOPE])
  end

  if project.nil? && @authorization.respond_to?(:project_id)
    project = @authorization.project_id
  end
  @project = project
  # The kwarg default doesn't fire on an explicitly-passed nil — coerce
  # so nil can't reach the derived URL (matches the other SDKs).
  @location = location || ENV["ANTHROPIC_GOOGLE_CLOUD_LOCATION"] || "global"

  if workspace_id.nil? && !skip_auth
    raise ArgumentError.new(
      "No workspace ID was given. Set the `workspace_id` argument or the " \
      "`ANTHROPIC_GOOGLE_CLOUD_WORKSPACE_ID` environment variable."
    )
  end
  @workspace_id = workspace_id

  base_url = derive_base_url if base_url.nil?

  # Never inherit first-party Anthropic credentials from the
  # environment: clear the ivars `Anthropic::Client#auth_headers` reads,
  # and bind `BaseClient#initialize` directly so the parent's
  # credential-provider / `ANTHROPIC_BASE_URL` resolution is skipped
  # entirely.
  @api_key = nil
  @auth_token = nil
  @credentials = nil
  @token_cache = nil

  Anthropic::Internal::Transport::BaseClient.instance_method(:initialize).bind(self).call(
    base_url: base_url,
    timeout: timeout,
    max_retries: max_retries,
    initial_retry_delay: initial_retry_delay,
    max_retry_delay: max_retry_delay,
    headers: {"anthropic-version" => "2023-06-01"},
    middleware: middleware
  )

  @messages = Anthropic::Resources::Messages.new(client: self)
  @models = Anthropic::Resources::Models.new(client: self)
  @beta = Anthropic::Resources::Beta.new(client: self)
end

Instance Attribute Details

#betaAnthropic::Resources::Beta (readonly)



41
42
43
# File 'lib/anthropic/helpers/google_cloud/client.rb', line 41

def beta
  @beta
end

#locationString? (readonly)

Returns:

  • (String, nil)


29
30
31
# File 'lib/anthropic/helpers/google_cloud/client.rb', line 29

def location
  @location
end

#messagesAnthropic::Resources::Messages (readonly)



35
36
37
# File 'lib/anthropic/helpers/google_cloud/client.rb', line 35

def messages
  @messages
end

#modelsAnthropic::Resources::Models (readonly)



38
39
40
# File 'lib/anthropic/helpers/google_cloud/client.rb', line 38

def models
  @models
end

#projectString? (readonly)

Returns:

  • (String, nil)


26
27
28
# File 'lib/anthropic/helpers/google_cloud/client.rb', line 26

def project
  @project
end

#workspace_idString? (readonly)

Returns:

  • (String, nil)


32
33
34
# File 'lib/anthropic/helpers/google_cloud/client.rb', line 32

def workspace_id
  @workspace_id
end

Instance Method Details

#completionsbot

The deprecated text Completions endpoint is not supported on the Google Cloud gateway.

Returns:

  • (bot)

Raises:

  • (NotImplementedError)


178
179
180
181
182
# File 'lib/anthropic/helpers/google_cloud/client.rb', line 178

def completions
  raise NotImplementedError.new(
    "The deprecated text Completions API is not supported on the Google Cloud gateway."
  )
end