Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion FirebaseAI/Sources/GenerateContentResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
38 changes: 37 additions & 1 deletion FirebaseAI/Tests/Unit/Types/GenerateContentResponseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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, *)
Expand Down Expand Up @@ -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."
)
}
}
Loading