Skip to main content
Portkey provides a robust and secure gateway to facilitate the integration of various Large Language Models (LLMs), and embedding models into your apps, including Google Vertex AI. With Portkey, you can take advantage of features like fast AI gateway access, observability, prompt management, and more, all while ensuring the secure management of your Vertex auth through a Portkey’s Model Catalog
Provider Slug. vertex-ai

Portkey SDK Integration with Google Vertex AI

Portkey provides a consistent API to interact with models from various providers. To integrate Google Vertex AI with Portkey:

1. Install the Portkey SDK

Add the Portkey SDK to your application to interact with Google Vertex AI API through Portkey’s gateway.

2. Initialize Portkey Client

To integrate Vertex AI with Portkey, you’ll need your Vertex Project Id Or Service Account JSON & Vertex Region, with which you can set up the Portkey’s AI Provider. Here’s a guide on how to find your Vertex Project details If you are integrating through Service Account File, refer to this guide.
If you do not want to add your Vertex AI details to Portkey vault, you can directly pass them while instantiating the Portkey client. More on that here.

3. Invoke Chat Completions with Vertex AI

Use the Portkey instance to send requests to any models hosted on Vertex AI. You can also override the Portkey’s AI Provider directly in the API call if needed.
Vertex AI uses OAuth2 to authenticate its requests, so you need to send the access token additionally along with the request.
To use Anthopic models on Vertex AI, prepend anthropic. to the model name.
Example: @VERTEX_PROVIDER/anthropic.claude-3-5-sonnet@20240620
Similarly, for Meta models, prepend meta. to the model name.
Example: @VERTEX_PROVIDER/meta.llama-3-8b-8192
Anthropic Beta Header Support: When using Anthropic models on Vertex AI, you can pass the anthropic-beta header (or x-portkey-anthropic-beta) to enable beta features. This header is forwarded to the underlying Anthropic API.

Using the /messages Route with Vertex AI Models

Access Claude models on Vertex AI through Anthropic’s native/messages endpoint using Portkey’s SDK or Anthropic’s SDK.
This route only works with Claude models. For other models, use the standard OpenAI compliant endpoint.

Counting Tokens

Portkey supports the Google Vertex AI CountTokens API to estimate token usage before sending requests. Check out the count-tokens guide for more details.

Explicit context caching

Vertex AI supports context caching to reduce costs and latency for repeated prompts with large amounts of context. You can explicitly create a cache and then reference it in subsequent inference requests.
This is different from Portkey’s gateway caching. Vertex AI context caching is a native Google feature that caches large context (system instructions, documents, etc.) on Google’s servers. Portkey’s simple and semantic caching caches complete request-response pairs at the gateway level. Use Vertex AI context caching when you have large, reusable context; use Portkey’s gateway caching for repeated identical or similar requests.
Gemini models only. Context caching is only available for Gemini models on Vertex AI (e.g., gemini-1.5-pro, gemini-1.5-flash). It is not supported for Anthropic, Meta, or other models hosted on Vertex AI.

Pricing

Context caching can significantly reduce costs for applications with large, reusable context: Cache reads are ~12x cheaper than cache writes, making this cost-effective for scenarios where you reference the same cached content multiple times.

Step 1: Create a context cache

Use Portkey’s proxy capability with the x-portkey-custom-host header to call Vertex AI’s native caching endpoints. This routes your request through Portkey while targeting Vertex AI’s cachedContents API directly.
Request variables:
Context caching requires a minimum of 1024 tokens in the cached content. The cache has a default TTL (time-to-live) which you can configure using the ttl parameter.
Proxy pattern limitation: Creating caches uses Portkey’s proxy capability to forward requests to Vertex AI’s native endpoints. Native endpoint support within Portkey’s standard API is not planned since context caching is Gemini-specific.

Step 2: Use the cache in inference requests

Once the cache is created, reference it in your chat completion requests using the cached_content parameter:
The model and region used in the inference request must match the model and region used when creating the cache.
For more details on context caching options like TTL configuration and cache management, refer to the Vertex AI context caching documentation.

Using Self-Deployed Models on Vertex AI (Hugging Face, Custom Models)

Portkey supports connecting to self-deployed models on Vertex AI, including models from Hugging Face or any custom models you’ve deployed to a Vertex AI endpoint. Requirements for Self-Deployed Models To use self-deployed models on Vertex AI through Portkey:
  1. Model Naming Convention: When making requests to your self-deployed model, you must prefix the model name with endpoints.
  2. Required Permissions: The Google Cloud service account used in your Portkey Model Catalog must have the aiplatform.endpoints.predict permission.
Why the prefix? Vertex AI’s product offering for self-deployed models is called “Endpoints.” This naming convention indicates to Portkey that it should route requests to your custom endpoint rather than a standard Vertex AI model.This approach works for all models you can self-deploy on Vertex AI Model Garden, including Hugging Face models and your own custom models.

Document, Video, Audio Processing

Vertex AI supports attaching various file types to your Gemini messages including documents (pdf), images (jpg, png), videos (webm, mp4), and audio files.
Supported Audio Formats: mp3, wav, opus, ogg, flac, pcm, aac, m4a, mpeg, mpga, mp4, webmGemini Docs:
Using Portkey, here’s how you can send these media files:

Document Processing (PDF)

Gemini’s vision capabilities excel at understanding the content of PDF documents, including text, tables, and images.

Gemini Documents Understanding Docs

Method 1: Sending a Document via Google Files URL Upload your PDF using the Files API to get a Google Files URL.
Method 2: Sending a Local Document as Base64 Data This is suitable for smaller, local PDF files.
While you can send other document types like .txt or .html, they will be treated as plain text. Gemini’s native document vision capabilities are optimized for the application/pdf MIME type.

Media Resolution

The media_resolution parameter allows you to control token allocation for media inputs (images, videos, PDFs) when using Gemini models on Vertex AI. This helps balance between processing detail and cost/speed.

Supported values

Top-level configuration

Apply media resolution globally to all media in the request:

Per-part configuration (Gemini 3 only)

For Gemini 3 models, you can specify media resolution on individual media parts. Per-part settings take precedence over global settings when both are specified.

Google Vertex AI Media Resolution Documentation


Extended Thinking (Reasoning Models) (Beta)

The assistants thinking response is returned in the response_chunk.choices[0].delta.content_blocks array, not the response.choices[0].message.content string.Gemini models do not support plugging back the reasoning into multi turn conversations, so you don’t need to send the thinking message back to the model.
Models like google.gemini-2.5-flash-preview-04-17 anthropic.claude-3-7-sonnet@20250219 support extended thinking. This is similar to openai thinking, but you get the model’s reasoning as it processes the request as well. Note that you will have to set strict_open_ai_compliance=False in the headers to use this feature.

Single turn conversation

To disable thinking for gemini models like google.gemini-2.5-flash-preview-04-17, you are required to explicitly set budget_tokens to 0.

Using reasoning_effort Parameter

You can also control thinking using the OpenAI-compatible reasoning_effort parameter instead of thinking.budget_tokens:

Gemini 2.5 Models

For Gemini 2.5 models, reasoning_effort maps to thinking_budget with specific token allocations:

Gemini 3.0+ Models

For Gemini 3.0 and later models, reasoning_effort maps directly to thinkingLevel:

Multi turn conversation

Sending base64 Image

Here, you can send the base64 image data along with the url field too:
This same message format also works for all other media types — just send your media file in the url field, like "url": "gs://cloud-samples-data/video/animals.mp4" for google cloud urls and "url":"https://download.samplelib.com/mp3/sample-3s.mp3" for public urlsYour URL should have the file extension, this is used for inferring MIME_TYPE which is a required parameter for prompting Gemini models with files

Text Embedding Models

You can use any of Vertex AI’s English and Multilingual models through Portkey, in the familar OpenAI-schema.
The Gemini-specific parameter task_type is also supported on Portkey.

Function Calling

Portkey supports function calling mode on Google’s Gemini Models. Explore this Cookbook for a deep dive and examples:

Managing Vertex AI Prompts

You can manage all prompts to Google Gemini in the Prompt Library. All the models in the model garden are supported and you can easily start testing different prompts. Once you’re ready with your prompt, you can use the portkey.prompts.completions.create interface to use the prompt in your application.

Image Generation Models

Portkey supports the Imagen API on Vertex AI for image generations, letting you easily make requests in the familar OpenAI-compliant schema.
Image Generation API Reference

List of Supported Imagen Models

  • imagen-3.0-generate-001
  • imagen-3.0-fast-generate-001
  • imagegeneration@006
  • imagegeneration@005
  • imagegeneration@002

Custom Metadata Labels

The recommended way to attribute costs and track usage is to use metadata which allows your workflows to be vendor agnostic
Vertex AI supports adding custom labels to your API calls for attribution. Pass labels in your request body or configure them in your gateway config using override_params.

Vertex AI supports grounding with Google Search. This is a feature that allows you to ground your LLM responses with real-time search results. Grounding is invoked by passing the google_search tool (for newer models like gemini-2.0-flash-001), and google_search_retrieval (for older models like gemini-1.5-flash) in the tools array.

Grounding with Google Maps

Vertex AI supports grounding with Google Maps for location-based queries—places, directions, ratings, and geographic information. Pass the google_maps (or googleMaps) tool in the tools array:

With Retrieval Configuration

Optionally configure location coordinates, language, and widget options by passing them inside the function parameters:
Mixing regular tools with grounding tools may cause errors—use only one tool type per request.

gemini-2.0-flash-thinking-exp and other thinking/reasoning models

gemini-2.0-flash-thinking-exp models return a Chain of Thought response along with the actual inference text, this is not openai compatible, however, Portkey supports this by adding a \r\n\r\n and appending the two responses together. You can split the response along this pattern to get the Chain of Thought response and the actual inference text. If you require the Chain of Thought response along with the actual inference text, pass the strict open ai compliance flag as false in the request. If you want to get the inference text only, pass the strict open ai compliance flag as true in the request.

Multiple Modalities on chat completions endpoint

gemini-2.5-flash-image (nano banana)

The image data is available in the content_parts field in the response and it can be plugged back in for multi turn conversations

single turn conversation


Thought Signatures (Tool Calling Verification)

Set x-portkey-strict-open-ai-compliance to false to receive the thought_signature in the response. This header must be included in all requests when using thought signatures.
Google’s Gemini 3 Pro model requires passing a thought_signature parameter in tool calling conversations for verifying the payload. This signature is returned by the model in the assistant’s tool call response and must be included when continuing multi-turn conversations.

Google Gemini Thought Signatures Documentation

Single turn conversation

In a single-turn conversation, you make a request with tools defined, and the model returns tool calls with thought signatures.

Multi turn conversation

In multi-turn conversations, you must include the thought_signature field in the assistant’s tool call when continuing the conversation.
The thought_signature is automatically generated by the model and returned in the tool call response. You must preserve this signature when including the assistant’s message in subsequent requests.

Computer Use (Browser Automation) (Preview)

This uses the Gemini computer-use preview model. Set strict_open_ai_compliance to false.

Single turn conversation

Multi turn conversation

multi turn conversation

Safety settings

Gemini models support configuring safety settings to block potentially harmful responses
The response contains the saftetyRatings keys in the choice object of the response. You only need to pass the strict-open-ai-compliance flag if you need the safetyRatings in response.

Making Requests Without Portkey’s Model Catalog

You can also pass your Vertex AI details & secrets directly without using the Portkey’s Model Catalog. Vertex AI expects a region, a project ID and the access token in the request for a successful completion request. This is how you can specify these fields directly in your requests:

Example Request

For further questions on custom Vertex AI deployments or fine-grained access tokens, reach out to us on support@portkey.ai

How to Find Your Google Vertex Project Details

To obtain your Vertex Project ID and Region, navigate to Google Vertex Dashboard.
  • You can copy the Project ID located at the top left corner of your screen.
  • Find the Region dropdown on the same page to get your Vertex Region.
Logo

Get Your Service Account JSON

When selecting Service Account File as your authentication method, you’ll need to:
  1. Upload your Google Cloud service account JSON file
  2. Specify the Vertex Region
This method is particularly important for using self-deployed models, as your service account must have the aiplatform.endpoints.predict permission to access custom endpoints. Learn more about permission on your Vertex IAM key here.
For Self-Deployed Models: Your service account must have the aiplatform.endpoints.predict permission in Google Cloud IAM. Without this specific permission, requests to custom endpoints will fail.

Using Project ID and Region Authentication

For standard Vertex AI models, you can simply provide:
  1. Your Vertex Project ID (found in your Google Cloud console)
  2. The Vertex Region where your models are deployed
This method is simpler but may not have all the permissions needed for custom endpoints.

Next Steps

The complete list of features supported in the SDK are available on the link below.

SDK

You’ll find more information in the relevant sections:
  1. Add metadata to your requests
  2. Add gateway configs to your Vertex AI requests
  3. Tracing Vertex AI requests
  4. Setup a fallback from OpenAI to Vertex AI APIs
Last modified on February 14, 2026