Skip to content

Commit 0951022

Browse files
yinghsienwucopybara-github
authored andcommitted
feat: support speech to speech translation in Gemini Live
PiperOrigin-RevId: 913877556
1 parent 0b67828 commit 0951022

4 files changed

Lines changed: 168 additions & 0 deletions

File tree

src/main/java/com/google/genai/LiveConverters.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,13 @@ ObjectNode liveConnectConfigToMldev(JsonNode fromObject, ObjectNode parentObject
11311131
Common.setValueByPath(parentObject, new String[] {"setup", "safetySettings"}, result);
11321132
}
11331133

1134+
if (Common.getValueByPath(fromObject, new String[] {"streamTranslationConfig"}) != null) {
1135+
Common.setValueByPath(
1136+
parentObject,
1137+
new String[] {"setup", "generationConfig", "streamTranslationConfig"},
1138+
Common.getValueByPath(fromObject, new String[] {"streamTranslationConfig"}));
1139+
}
1140+
11341141
return toObject;
11351142
}
11361143

@@ -1296,6 +1303,13 @@ ObjectNode liveConnectConfigToVertex(JsonNode fromObject, ObjectNode parentObjec
12961303
Common.getValueByPath(fromObject, new String[] {"safetySettings"}));
12971304
}
12981305

1306+
if (!Common.isZero(
1307+
Common.getValueByPath(fromObject, new String[] {"streamTranslationConfig"}))) {
1308+
throw new IllegalArgumentException(
1309+
"streamTranslationConfig parameter is not supported in Gemini Enterprise Agent"
1310+
+ " Platform.");
1311+
}
1312+
12991313
return toObject;
13001314
}
13011315

src/main/java/com/google/genai/TokensConverters.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,13 @@ ObjectNode liveConnectConfigToMldev(JsonNode fromObject, ObjectNode parentObject
513513
Common.setValueByPath(parentObject, new String[] {"setup", "safetySettings"}, result);
514514
}
515515

516+
if (Common.getValueByPath(fromObject, new String[] {"streamTranslationConfig"}) != null) {
517+
Common.setValueByPath(
518+
parentObject,
519+
new String[] {"setup", "generationConfig", "streamTranslationConfig"},
520+
Common.getValueByPath(fromObject, new String[] {"streamTranslationConfig"}));
521+
}
522+
516523
return toObject;
517524
}
518525

src/main/java/com/google/genai/types/LiveConnectConfig.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,10 @@ public abstract class LiveConnectConfig extends JsonSerializable {
169169
@JsonProperty("safetySettings")
170170
public abstract Optional<List<SafetySetting>> safetySettings();
171171

172+
/** Config for stream translation. */
173+
@JsonProperty("streamTranslationConfig")
174+
public abstract Optional<StreamTranslationConfig> streamTranslationConfig();
175+
172176
/** Instantiates a builder for LiveConnectConfig. */
173177
@ExcludeFromGeneratedCoverageReport
174178
public static Builder builder() {
@@ -866,6 +870,37 @@ public Builder clearSafetySettings() {
866870
return safetySettings(Optional.empty());
867871
}
868872

873+
/**
874+
* Setter for streamTranslationConfig.
875+
*
876+
* <p>streamTranslationConfig: Config for stream translation.
877+
*/
878+
@JsonProperty("streamTranslationConfig")
879+
public abstract Builder streamTranslationConfig(
880+
StreamTranslationConfig streamTranslationConfig);
881+
882+
/**
883+
* Setter for streamTranslationConfig builder.
884+
*
885+
* <p>streamTranslationConfig: Config for stream translation.
886+
*/
887+
@CanIgnoreReturnValue
888+
public Builder streamTranslationConfig(
889+
StreamTranslationConfig.Builder streamTranslationConfigBuilder) {
890+
return streamTranslationConfig(streamTranslationConfigBuilder.build());
891+
}
892+
893+
@ExcludeFromGeneratedCoverageReport
894+
abstract Builder streamTranslationConfig(
895+
Optional<StreamTranslationConfig> streamTranslationConfig);
896+
897+
/** Clears the value of streamTranslationConfig field. */
898+
@ExcludeFromGeneratedCoverageReport
899+
@CanIgnoreReturnValue
900+
public Builder clearStreamTranslationConfig() {
901+
return streamTranslationConfig(Optional.empty());
902+
}
903+
869904
public abstract LiveConnectConfig build();
870905
}
871906

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// Auto-generated code. Do not edit.
18+
19+
package com.google.genai.types;
20+
21+
import com.fasterxml.jackson.annotation.JsonCreator;
22+
import com.fasterxml.jackson.annotation.JsonProperty;
23+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
24+
import com.google.auto.value.AutoValue;
25+
import com.google.errorprone.annotations.CanIgnoreReturnValue;
26+
import com.google.genai.JsonSerializable;
27+
import java.util.Optional;
28+
29+
/** Config for stream translation. */
30+
@AutoValue
31+
@JsonDeserialize(builder = StreamTranslationConfig.Builder.class)
32+
public abstract class StreamTranslationConfig extends JsonSerializable {
33+
/**
34+
* If true, the model will generate audio when the target language is spoken, essentially it will
35+
* parrot the input. If false, we will not produce audio for the target language.
36+
*/
37+
@JsonProperty("echoTargetLanguage")
38+
public abstract Optional<Boolean> echoTargetLanguage();
39+
40+
/**
41+
* The target language for translation. Supported values are BCP-47 language codes (e.g. "en",
42+
* "es", "fr").
43+
*/
44+
@JsonProperty("targetLanguageCode")
45+
public abstract Optional<String> targetLanguageCode();
46+
47+
/** Instantiates a builder for StreamTranslationConfig. */
48+
@ExcludeFromGeneratedCoverageReport
49+
public static Builder builder() {
50+
return new AutoValue_StreamTranslationConfig.Builder();
51+
}
52+
53+
/** Creates a builder with the same values as this instance. */
54+
public abstract Builder toBuilder();
55+
56+
/** Builder for StreamTranslationConfig. */
57+
@AutoValue.Builder
58+
public abstract static class Builder {
59+
/** For internal usage. Please use `StreamTranslationConfig.builder()` for instantiation. */
60+
@JsonCreator
61+
private static Builder create() {
62+
return new AutoValue_StreamTranslationConfig.Builder();
63+
}
64+
65+
/**
66+
* Setter for echoTargetLanguage.
67+
*
68+
* <p>echoTargetLanguage: If true, the model will generate audio when the target language is
69+
* spoken, essentially it will parrot the input. If false, we will not produce audio for the
70+
* target language.
71+
*/
72+
@JsonProperty("echoTargetLanguage")
73+
public abstract Builder echoTargetLanguage(boolean echoTargetLanguage);
74+
75+
@ExcludeFromGeneratedCoverageReport
76+
abstract Builder echoTargetLanguage(Optional<Boolean> echoTargetLanguage);
77+
78+
/** Clears the value of echoTargetLanguage field. */
79+
@ExcludeFromGeneratedCoverageReport
80+
@CanIgnoreReturnValue
81+
public Builder clearEchoTargetLanguage() {
82+
return echoTargetLanguage(Optional.empty());
83+
}
84+
85+
/**
86+
* Setter for targetLanguageCode.
87+
*
88+
* <p>targetLanguageCode: The target language for translation. Supported values are BCP-47
89+
* language codes (e.g. "en", "es", "fr").
90+
*/
91+
@JsonProperty("targetLanguageCode")
92+
public abstract Builder targetLanguageCode(String targetLanguageCode);
93+
94+
@ExcludeFromGeneratedCoverageReport
95+
abstract Builder targetLanguageCode(Optional<String> targetLanguageCode);
96+
97+
/** Clears the value of targetLanguageCode field. */
98+
@ExcludeFromGeneratedCoverageReport
99+
@CanIgnoreReturnValue
100+
public Builder clearTargetLanguageCode() {
101+
return targetLanguageCode(Optional.empty());
102+
}
103+
104+
public abstract StreamTranslationConfig build();
105+
}
106+
107+
/** Deserializes a JSON string to a StreamTranslationConfig object. */
108+
@ExcludeFromGeneratedCoverageReport
109+
public static StreamTranslationConfig fromJson(String jsonString) {
110+
return JsonSerializable.fromJsonString(jsonString, StreamTranslationConfig.class);
111+
}
112+
}

0 commit comments

Comments
 (0)