DocumentationAPI Reference📓 Tutorials🧑‍🍳 Cookbook🤝 Integrations💜 Discord


This component enables code generation using Google Vertex AI generative model.

Mandatory input variables“prefix”: A string of code before the current point

”suffix”: An optional string of code after the current point
Output variables“replies”: Code generated by the model

VertexAICodeGenerator supports code-bison, code-bison-32k, and code-gecko.

Parameters Overview

VertexAICodeGenerator uses Google Cloud Application Default Credentials (ADCs) for authentication. For more information on how to set up ADCs, see the official documentation.

Keep in mind that it’s essential to use an account that has access to a project authorized to use Google Vertex AI endpoints.

You can find your project ID in the GCP resource manager or locally by running gcloud projects list in your terminal. For more info on the gcloud CLI, see its official documentation.


You need to install google-vertex-haystack package first to use the VertexAIImageCaptioner:

pip install google-vertex-haystack

Basic usage:

from haystack_integrations.components.generators.google_vertex import VertexAICodeGenerator

generator = VertexAICodeGenerator(project_id=project_id)

result ="def to_json(data):")

for answer in result["answers"]:

>>> ```python
>>> import json
>>> def to_json(data):
>>>   """Converts a Python object to a JSON string.
>>>   Args:
>>>     data: The Python object to convert.
>>>   Returns:
>>>     A JSON string representing the Python object.
>>>   """
>>>   return json.dumps(data)
>>> ```

You can also set other parameters like the number of output tokens, temperature, stop sequences, and the number of candidates.

Let’s try a different model:

from haystack_integrations.components.generators.google_vertex import VertexAICodeGenerator

generator = VertexAICodeGenerator(

result ="def convert_temperature(degrees):")

for answer in result["answers"]:

>>>     return degrees * (9/5) + 32

>>>     return round(degrees * (9.0 / 5.0) + 32, 1)

>>>     return 5 * (degrees - 32) /9
>>> def convert_temperature_back(degrees):
>>>     return 9 * (degrees / 5) + 32

Related Links

Check out the API reference in the GitHub repo or in our docs: