Skip to content

Commit 63083d8

Browse files
[AI] Update empty parts check for urlContextMetadata (#15355)
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 03cffc3 commit 63083d8

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

FirebaseAI/Sources/GenerateContentResponse.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ public struct Candidate: Sendable {
179179
// Returns `true` if the candidate contains no information that a developer could use.
180180
var isEmpty: Bool {
181181
content.parts
182-
.isEmpty && finishReason == nil && citationMetadata == nil && groundingMetadata == nil
182+
.isEmpty && finishReason == nil && citationMetadata == nil && groundingMetadata == nil &&
183+
urlContextMetadata == nil
183184
}
184185
}
185186

FirebaseAI/Tests/Unit/Types/GenerateContentResponseTests.swift

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import FirebaseAI
15+
@testable import FirebaseAI
1616
import XCTest
1717

1818
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
@@ -157,4 +157,40 @@ final class GenerateContentResponseTests: XCTestCase {
157157
XCTAssertFalse(textPart.isThought)
158158
XCTAssertEqual(candidate.finishReason, .stop)
159159
}
160+
161+
// MARK: - Candidate.isEmpty
162+
163+
func testCandidateIsEmpty_allEmpty_isTrue() throws {
164+
let candidate = Candidate(
165+
content: ModelContent(parts: []),
166+
safetyRatings: [],
167+
finishReason: nil,
168+
citationMetadata: nil,
169+
groundingMetadata: nil,
170+
urlContextMetadata: nil
171+
)
172+
173+
XCTAssertTrue(candidate.isEmpty, "A candidate with no content should be empty.")
174+
}
175+
176+
func testCandidateIsEmpty_withURLContextMetadata_isFalse() throws {
177+
let urlMetadata = try URLMetadata(
178+
retrievedURL: XCTUnwrap(URL(string: "https://google.com")),
179+
retrievalStatus: .success
180+
)
181+
let urlContextMetadata = URLContextMetadata(urlMetadata: [urlMetadata])
182+
let candidate = Candidate(
183+
content: ModelContent(parts: []),
184+
safetyRatings: [],
185+
finishReason: nil,
186+
citationMetadata: nil,
187+
groundingMetadata: nil,
188+
urlContextMetadata: urlContextMetadata
189+
)
190+
191+
XCTAssertFalse(
192+
candidate.isEmpty,
193+
"A candidate with only `urlContextMetadata` should not be empty."
194+
)
195+
}
160196
}

0 commit comments

Comments
 (0)