Controlled generation JSON output with null values

Allow the model to output null responses when it doesn't have enough context to generate a meaningful response.

Code sample

C#

Before trying this sample, follow the C# setup instructions in the Vertex AI quickstart using client libraries. For more information, see the Vertex AI C# API reference documentation.

To authenticate to Vertex AI, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

public async Task<string> GenerateContentWithResponseSchema3(
    string projectId = "your-project-id",
    string location = "us-central1",
    string publisher = "google",
    string model = "gemini-2.0-flash-001")
{

    var predictionServiceClient = new PredictionServiceClientBuilder
    {
        Endpoint = $"{location}-aiplatform.googleapis.com"
    }.Build();

    var responseSchema = new OpenApiSchema
    {
        Type = Type.Object,
        Properties =
        {
            ["forecast"] = new()
            {
                Type = Type.Array,
                Items = new()
                {
                    Type = Type.Object,
                    Properties =
                    {
                        ["Forecast"] = new() { Type = Type.String },
                        ["Humidity"] = new() { Type = Type.String },
                        ["Temperature"] = new() { Type = Type.Integer },
                        ["Wind Speed"] = new() { Type = Type.Integer }
                    }
                }
            }
        }
    };

    string prompt = @"
    The week ahead brings a mix of weather conditions.
    Sunday is expected to be sunny with a temperature of 77°F and a humidity level of 50%. Winds will be light at around 10 km/h.
    Monday will see partly cloudy skies with a slightly cooler temperature of 72°F and humidity increasing to 55%. Winds will pick up slightly to around 15 km/h.
    Tuesday brings rain showers, with temperatures dropping to 64°F and humidity rising to 70%. Expect stronger winds at 20 km/h.
    Wednesday may see thunderstorms, with a temperature of 68°F and high humidity of 75%. Winds will be gusty at 25 km/h.
    Thursday will be cloudy with a temperature of 66°F and moderate humidity at 60%. Winds will ease slightly to 18 km/h.
    Friday returns to partly cloudy conditions, with a temperature of 73°F and lower humidity at 45%. Winds will be light at 12 km/h.
    Finally, Saturday rounds off the week with sunny skies, a temperature of 80°F, and a humidity level of 40%. Winds will be gentle at 8 km/h.";

    var generateContentRequest = new GenerateContentRequest
    {
        Model = $"projects/{projectId}/locations/{location}/publishers/{publisher}/models/{model}",
        Contents =
        {
            new Content
            {
                Role = "USER",
                Parts =
                {
                    new Part { Text = prompt }
                }
            }
        },
        GenerationConfig = new GenerationConfig
        {
            ResponseMimeType = "application/json",
            ResponseSchema = responseSchema
        },
    };

    GenerateContentResponse response = await predictionServiceClient.GenerateContentAsync(generateContentRequest);

    string responseText = response.Candidates[0].Content.Parts[0].Text;
    Console.WriteLine(responseText);

    return responseText;
}

Java

Before trying this sample, follow the Java setup instructions in the Vertex AI quickstart using client libraries. For more information, see the Vertex AI Java API reference documentation.

To authenticate to Vertex AI, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

import com.google.cloud.vertexai.VertexAI;
import com.google.cloud.vertexai.api.GenerateContentResponse;
import com.google.cloud.vertexai.api.GenerationConfig;
import com.google.cloud.vertexai.api.Schema;
import com.google.cloud.vertexai.api.Type;
import com.google.cloud.vertexai.generativeai.GenerativeModel;
import com.google.cloud.vertexai.generativeai.ResponseHandler;
import java.io.IOException;
import java.util.Arrays;

public class ControlledGenerationSchema3 {
  public static void main(String[] args) throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "genai-java-demos";
    String location = "us-central1";
    String modelName = "gemini-1.5-pro-001";

    controlGenerationWithJsonSchema3(projectId, location, modelName);
  }

  // Generate responses that are always valid JSON and comply with a JSON schema
  public static String controlGenerationWithJsonSchema3(
      String projectId, String location, String modelName)
      throws IOException {
    // Initialize client that will be used to send requests. This client only needs
    // to be created once, and can be reused for multiple requests.
    try (VertexAI vertexAI = new VertexAI(projectId, location)) {
      GenerationConfig generationConfig = GenerationConfig.newBuilder()
          .setResponseMimeType("application/json")
          .setResponseSchema(Schema.newBuilder()
              .setType(Type.OBJECT)
              .putProperties("forecast", Schema.newBuilder()
                      .setType(Type.ARRAY)
                      .setItems(Schema.newBuilder()
                          .setType(Type.OBJECT)
                          .putProperties("Day", Schema.newBuilder()
                              .setType(Type.STRING)
                              .build())
                          .putProperties("Forecast", Schema.newBuilder()
                              .setType(Type.STRING)
                              .build())
                          .putProperties("Humidity", Schema.newBuilder()
                              .setType(Type.STRING)
                              .build())
                          .putProperties("Temperature", Schema.newBuilder()
                              .setType(Type.INTEGER)
                              .build())
                          .putProperties("Wind Speed", Schema.newBuilder()
                              .setType(Type.INTEGER)
                              .build())
                          .addAllRequired(Arrays.asList("Day", "Temperature", "Forecast"))
                          .build())
                      .build())
              )
          .build();

      GenerativeModel model = new GenerativeModel(modelName, vertexAI)
          .withGenerationConfig(generationConfig);

      GenerateContentResponse response = model.generateContent(
          "The week ahead brings a mix of weather conditions.\n"
              + "Sunday is expected to be sunny with a temperature of 77°F and a humidity level "
              + "of 50%. Winds will be light at around 10 km/h.\n"
              + "Monday will see partly cloudy skies with a slightly cooler temperature of 72°F "
              + "and humidity increasing to 55%. Winds will pick up slightly to around 15 km/h.\n"
              + "Tuesday brings rain showers, with temperatures dropping to 64°F and humidity"
              + "rising to 70%. Expect stronger winds at 20 km/h.\n"
              + "Wednesday may see thunderstorms, with a temperature of 68°F and high humidity "
              + "of 75%. Winds will be gusty at 25 km/h.\n"
              + "Thursday will be cloudy with a temperature of 66°F and moderate humidity at 60%. "
              + "Winds will ease slightly to 18 km/h.\n"
              + "Friday returns to partly cloudy conditions, with a temperature of 73°F and lower "
              + "humidity at 45%. Winds will be light at 12 km/h.\n"
              + "Finally, Saturday rounds off the week with sunny skies, a temperature of 80°F, "
              + "and a humidity level of 40%. Winds will be gentle at 8 km/h."
      );

      String output = ResponseHandler.getText(response);
      System.out.println(output);
      return output;
    }
  }
}

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.