diff --git a/FirebaseAI/Sources/GenerateContentResponse.swift b/FirebaseAI/Sources/GenerateContentResponse.swift index c13d3d53c8a..a7d7da85d67 100644 --- a/FirebaseAI/Sources/GenerateContentResponse.swift +++ b/FirebaseAI/Sources/GenerateContentResponse.swift @@ -179,7 +179,8 @@ public struct Candidate: Sendable { // Returns `true` if the candidate contains no information that a developer could use. var isEmpty: Bool { content.parts - .isEmpty && finishReason == nil && citationMetadata == nil && groundingMetadata == nil + .isEmpty && finishReason == nil && citationMetadata == nil && groundingMetadata == nil && + urlContextMetadata == nil } } diff --git a/FirebaseAI/Tests/Unit/Types/GenerateContentResponseTests.swift b/FirebaseAI/Tests/Unit/Types/GenerateContentResponseTests.swift index 276308f63aa..dfc393e2d29 100644 --- a/FirebaseAI/Tests/Unit/Types/GenerateContentResponseTests.swift +++ b/FirebaseAI/Tests/Unit/Types/GenerateContentResponseTests.swift @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -import FirebaseAI +@testable import FirebaseAI import XCTest @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) @@ -157,4 +157,40 @@ final class GenerateContentResponseTests: XCTestCase { XCTAssertFalse(textPart.isThought) XCTAssertEqual(candidate.finishReason, .stop) } + + // MARK: - Candidate.isEmpty + + func testCandidateIsEmpty_allEmpty_isTrue() throws { + let candidate = Candidate( + content: ModelContent(parts: []), + safetyRatings: [], + finishReason: nil, + citationMetadata: nil, + groundingMetadata: nil, + urlContextMetadata: nil + ) + + XCTAssertTrue(candidate.isEmpty, "A candidate with no content should be empty.") + } + + func testCandidateIsEmpty_withURLContextMetadata_isFalse() throws { + let urlMetadata = try URLMetadata( + retrievedURL: XCTUnwrap(URL(string: "https://google.com")), + retrievalStatus: .success + ) + let urlContextMetadata = URLContextMetadata(urlMetadata: [urlMetadata]) + let candidate = Candidate( + content: ModelContent(parts: []), + safetyRatings: [], + finishReason: nil, + citationMetadata: nil, + groundingMetadata: nil, + urlContextMetadata: urlContextMetadata + ) + + XCTAssertFalse( + candidate.isEmpty, + "A candidate with only `urlContextMetadata` should not be empty." + ) + } }