The official Python library for the anthropic API
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Access Claude models on Google Cloud Platform with Vertex AI integration.
pip install anthropic[vertex]class AnthropicVertex:
"""Synchronous client for Claude on Google Vertex AI."""
def __init__(
self,
*,
project_id: str | None = None,
region: str | None = None,
timeout: float | httpx.Timeout = DEFAULT_TIMEOUT,
max_retries: int = DEFAULT_MAX_RETRIES,
default_headers: dict[str, str] | None = None,
http_client: httpx.Client | None = None,
):
"""
Initialize Vertex AI client.
Parameters:
project_id: GCP project ID (or CLOUD_ML_PROJECT_ID/GOOGLE_CLOUD_PROJECT env var)
region: GCP region (or CLOUD_ML_REGION env var, default: us-east5)
timeout: Request timeout
max_retries: Maximum retry attempts
default_headers: Custom headers
http_client: Custom httpx.Client
"""
...
class AsyncAnthropicVertex:
"""Asynchronous client for Claude on Google Vertex AI."""
# Same parameters as AnthropicVertex
...# Claude models on Vertex AI
"claude-3-5-sonnet-v2@20241022"
"claude-3-5-sonnet@20240620"
"claude-3-5-haiku@20241022"
"claude-3-opus@20240229"
"claude-3-sonnet@20240229"
"claude-3-haiku@20240307"from anthropic import AnthropicVertex
client = AnthropicVertex(
project_id="my-gcp-project",
region="us-east5"
)
message = client.messages.create(
model="claude-3-5-sonnet-v2@20241022",
max_tokens=1024,
messages=[{"role": "user", "content": "Hello!"}]
)# Set: CLOUD_ML_PROJECT_ID=my-gcp-project
# Set: CLOUD_ML_REGION=us-east5
client = AnthropicVertex() # Automatically uses env vars# First authenticate: gcloud auth application-default login
client = AnthropicVertex(
project_id="my-gcp-project",
region="us-east5"
)with client.messages.stream(
model="claude-3-5-sonnet-v2@20241022",
max_tokens=1024,
messages=[{"role": "user", "content": "Write a story"}]
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)regions = ["us-east5", "europe-west1", "asia-southeast1"]
for region in regions:
client = AnthropicVertex(
project_id="my-gcp-project",
region=region
)
message = client.messages.create(...)
client.close()import asyncio
from anthropic import AsyncAnthropicVertex
async def main():
client = AsyncAnthropicVertex(
project_id="my-gcp-project",
region="us-east5"
)
message = await client.messages.create(...)
await client.close()
asyncio.run(main())CLOUD_ML_PROJECT_ID or GOOGLE_CLOUD_PROJECT - GCP project IDCLOUD_ML_REGION - GCP region (default: us-east5)GOOGLE_APPLICATION_CREDENTIALS - Path to service account key JSONus-east5us-central1europe-west1europe-west4asia-southeast1