Skip to content

Commit cedcd3d

Browse files
committed
fix: avoid setting Part.partMetadata when metadata is empty
1 parent e8b1c20 commit cedcd3d

2 files changed

Lines changed: 26 additions & 3 deletions

File tree

a2a/src/main/java/com/google/adk/a2a/converters/PartConverter.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,12 @@ public static com.google.genai.types.Part toGenaiPart(io.a2a.spec.Part<?> a2aPar
8787
com.google.genai.types.Part.Builder partBuilder =
8888
com.google.genai.types.Part.builder().text(textPart.getText());
8989
if (textPart.getMetadata() != null) {
90-
partBuilder.partMetadata(textPart.getMetadata());
9190
if (Objects.equals(textPart.getMetadata().get("thought"), true)) {
9291
partBuilder.thought(true);
9392
}
93+
if (!textPart.getMetadata().isEmpty()) {
94+
partBuilder.partMetadata(textPart.getMetadata());
95+
}
9496
}
9597
return partBuilder.build();
9698
}
@@ -122,7 +124,7 @@ private static com.google.genai.types.Part convertFilePartToGenAiPart(FilePart f
122124
.fileUri(fileWithUri.uri())
123125
.mimeType(fileWithUri.mimeType())
124126
.build());
125-
if (metadata != null) {
127+
if (metadata != null && !metadata.isEmpty()) {
126128
builder.partMetadata(metadata);
127129
}
128130
return builder.build();
@@ -137,7 +139,7 @@ private static com.google.genai.types.Part convertFilePartToGenAiPart(FilePart f
137139
com.google.genai.types.Part.Builder builder =
138140
com.google.genai.types.Part.builder()
139141
.inlineData(Blob.builder().data(decoded).mimeType(fileWithBytes.mimeType()).build());
140-
if (metadata != null) {
142+
if (metadata != null && !metadata.isEmpty()) {
141143
builder.partMetadata(metadata);
142144
}
143145
return builder.build();

a2a/src/test/java/com/google/adk/a2a/converters/PartConverterTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,27 @@ public void toGenaiPart_dataPartWithNonMapCoercedToMap() {
483483
assertThat(result.functionCall().get().args()).hasValue(ImmutableMap.of("value", 123));
484484
}
485485

486+
@Test
487+
public void toGenaiPart_withTextPartEmptyMetadata_doesNotSetPartMetadata() {
488+
TextPart textPart = new TextPart("Hello", ImmutableMap.of());
489+
490+
Part result = PartConverter.toGenaiPart(textPart);
491+
492+
assertThat(result.text()).hasValue("Hello");
493+
assertThat(result.partMetadata()).isEmpty();
494+
}
495+
496+
@Test
497+
public void toGenaiPart_withFilePartEmptyMetadata_doesNotSetPartMetadata() {
498+
FilePart filePart =
499+
new FilePart(
500+
new FileWithUri("text/plain", "file.txt", "http://file.txt"), ImmutableMap.of());
501+
502+
Part result = PartConverter.toGenaiPart(filePart);
503+
504+
assertThat(result.partMetadata()).isEmpty();
505+
}
506+
486507
@Test
487508
public void toGenaiPart_withTextPartMetadata_propagatesMetadata() {
488509
TextPart textPart = new TextPart("Hello", ImmutableMap.of("key", "value"));

0 commit comments

Comments
 (0)