Skip to content

Commit baeb4c6

Browse files
matthew29tangcopybara-github
authored andcommitted
chore: Update java-genai header merging logic to handle vertex-genai-modules case
PiperOrigin-RevId: 914364665
1 parent 642ad10 commit baeb4c6

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,37 @@ private Optional<Map<String, String>> getTimeoutHeader(HttpOptions httpOptionsTo
714714
return Optional.empty();
715715
}
716716

717+
/**
718+
* Merges two user agent values, handling a special case for vertex-genai-modules.
719+
*
720+
* <p>Example:
721+
* <li>val1 = google-genai-sdk/1.42.0 gl-java/22.0.2
722+
* <li>val2 = vertex-genai-modules/1.2.3
723+
* <li>Result: google-genai-sdk/1.42.0+vertex-genai-modules/1.2.3 gl-java/22.0.2
724+
*
725+
* @param val1 The first value.
726+
* @param val2 The second value.
727+
* @return The merged user agent value.
728+
*/
729+
private String mergeUserAgentValues(String val1, String val2) {
730+
String extensionRegex = "^vertex-genai-modules/[^ ]+.*";
731+
boolean val1Matches = val1.matches(extensionRegex);
732+
boolean val2Matches = val2.matches(extensionRegex);
733+
734+
if (val1Matches || val2Matches) {
735+
String extensionHeader = val1Matches ? val1 : val2;
736+
String baseHeader = val1Matches ? val2 : val1;
737+
738+
// Find google-genai-sdk/x.y.z in baseHeader
739+
if (baseHeader.contains("google-genai-sdk/")) {
740+
// $1 refers to the matched (google-genai-sdk/[^ ]+) group
741+
return baseHeader.replaceFirst("(google-genai-sdk/[^ ]+)", "$1+" + extensionHeader);
742+
}
743+
}
744+
745+
return val1 + " " + val2;
746+
}
747+
717748
/**
718749
* Merges the http options to the client's http options.
719750
*
@@ -752,7 +783,7 @@ HttpOptions mergeHttpOptions(HttpOptions httpOptionsToApply) {
752783
(val1, val2) -> {
753784
if (entry.getKey().equals("user-agent")
754785
|| entry.getKey().equals("x-goog-api-client")) {
755-
return val1 + " " + val2;
786+
return mergeUserAgentValues(val1, val2);
756787
}
757788
return val2;
758789
}));

src/test/java/com/google/genai/HttpApiClientTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,26 @@ public void testInitHttpClientCustomUserAgent() throws Exception {
605605
+ "' does not match the expected format.");
606606
}
607607

608+
@Test
609+
public void testInitHttpClientExtensionsUserAgentMerge() throws Exception {
610+
String vertexHeader = "vertex-genai-modules/1.2.3";
611+
ImmutableMap<String, String> trackingHeaders =
612+
ImmutableMap.of(
613+
"x-goog-api-client", vertexHeader,
614+
"user-agent", vertexHeader);
615+
HttpOptions httpOptions = HttpOptions.builder().headers(trackingHeaders).build();
616+
HttpApiClient client =
617+
new HttpApiClient(Optional.of(API_KEY), Optional.of(httpOptions), Optional.empty());
618+
619+
String userAgent = client.httpOptions.headers().get().get("user-agent");
620+
String apiClient = client.httpOptions.headers().get().get("x-goog-api-client");
621+
622+
String expectedRegex = "^google-genai-sdk/[^ ]+vertex-genai-modules/1.2.3 gl-java/[^ ]+$";
623+
624+
assertTrue(userAgent.matches(expectedRegex), "User-Agent did not match: " + userAgent);
625+
assertTrue(apiClient.matches(expectedRegex), "x-goog-api-client did not match: " + apiClient);
626+
}
627+
608628
@Test
609629
public void testInitHttpClientMldev() throws Exception {
610630
HttpOptions httpOptions =

0 commit comments

Comments
 (0)