Skip to content

Commit dcf145f

Browse files
committed
Add internal unspecified enums to prevent unknown enum value logs
1 parent 3a207b8 commit dcf145f

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

FirebaseAI/Sources/GenerateContentResponse.swift

+9-7
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ public struct Citation: Sendable {
171171
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
172172
public struct FinishReason: DecodableProtoEnum, Hashable, Sendable {
173173
enum Kind: String {
174+
case unspecified = "FINISH_REASON_UNSPECIFIED"
174175
case stop = "STOP"
175176
case maxTokens = "MAX_TOKENS"
176177
case safety = "SAFETY"
@@ -366,16 +367,17 @@ extension Candidate: Decodable {
366367
}
367368
}
368369

369-
if let safetyRatings = try container.decodeIfPresent(
370-
[SafetyRating].self,
371-
forKey: .safetyRatings
372-
) {
373-
self.safetyRatings = safetyRatings
374-
} else {
375-
safetyRatings = []
370+
let safetyRatings = try container
371+
.decodeIfPresent([SafetyRating].self, forKey: .safetyRatings) ?? []
372+
// Filter out safety ratings where the category and probability are both unspecified by the
373+
// backend.
374+
self.safetyRatings = safetyRatings.filter {
375+
$0.category.rawValue != HarmCategory.Kind.unspecified.rawValue
376+
&& $0.probability.rawValue != SafetyRating.HarmProbability.Kind.unspecified.rawValue
376377
}
377378

378379
finishReason = try container.decodeIfPresent(FinishReason.self, forKey: .finishReason)
380+
.flatMap { $0.rawValue == FinishReason.Kind.unspecified.rawValue ? nil : $0 }
379381

380382
citationMetadata = try container.decodeIfPresent(
381383
CitationMetadata.self,

FirebaseAI/Sources/GenerativeModel.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ public final class GenerativeModel: Sendable {
167167
}
168168

169169
// Check to see if an error should be thrown for stop reason.
170-
if let reason = response.candidates.first?.finishReason, reason != .stop {
170+
if let reason = response.candidates.first?.finishReason, reason != .stop,
171+
reason.rawValue != FinishReason.Kind.unspecified.rawValue {
171172
throw GenerateContentError.responseStoppedEarly(reason: reason, response: response)
172173
}
173174

FirebaseAI/Sources/Safety.swift

+3
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public struct SafetyRating: Equatable, Hashable, Sendable {
7878
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
7979
public struct HarmProbability: DecodableProtoEnum, Hashable, Sendable {
8080
enum Kind: String {
81+
case unspecified = "HARM_CATEGORY_UNSPECIFIED"
8182
case negligible = "NEGLIGIBLE"
8283
case low = "LOW"
8384
case medium = "MEDIUM"
@@ -114,6 +115,7 @@ public struct SafetyRating: Equatable, Hashable, Sendable {
114115
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
115116
public struct HarmSeverity: DecodableProtoEnum, Hashable, Sendable {
116117
enum Kind: String {
118+
case unspecified = "HARM_SEVERITY_UNSPECIFIED"
117119
case negligible = "HARM_SEVERITY_NEGLIGIBLE"
118120
case low = "HARM_SEVERITY_LOW"
119121
case medium = "HARM_SEVERITY_MEDIUM"
@@ -234,6 +236,7 @@ public struct SafetySetting: Sendable {
234236
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
235237
public struct HarmCategory: CodableProtoEnum, Hashable, Sendable {
236238
enum Kind: String {
239+
case unspecified = "HARM_CATEGORY_UNSPECIFIED"
237240
case harassment = "HARM_CATEGORY_HARASSMENT"
238241
case hateSpeech = "HARM_CATEGORY_HATE_SPEECH"
239242
case sexuallyExplicit = "HARM_CATEGORY_SEXUALLY_EXPLICIT"

0 commit comments

Comments
 (0)