Skip to content

Commit 4c9cc64

Browse files
authored
[Vertex AI] Remove ImageConversionError from public API (#13735)
1 parent 5ed86cd commit 4c9cc64

File tree

4 files changed

+25
-53
lines changed

4 files changed

+25
-53
lines changed

FirebaseVertexAI/CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
is now optional (`Int?`); it may be `null` in cases such as when a
2323
`GenerateContentRequest` contains only images or other non-text content.
2424
(#13721)
25+
- [changed] **Breaking Change**: The `ImageConversionError` enum is no longer
26+
public; image conversion errors are still reported as
27+
`GenerateContentError.promptImageContentError`. (#13735)
2528
- [changed] The default request timeout is now 180 seconds instead of the
2629
platform-default value of 60 seconds for a `URLRequest`; this timeout may
2730
still be customized in `RequestOptions`. (#13722)

FirebaseVertexAI/Sources/GenerateContentError.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ import Foundation
1717
/// Errors that occur when generating content from a model.
1818
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
1919
public enum GenerateContentError: Error {
20-
/// An error occurred when constructing the prompt. Examine the related error for details.
21-
case promptImageContentError(underlying: ImageConversionError)
22-
2320
/// An internal error occurred. See the underlying error for more context.
2421
case internalError(underlying: Error)
2522

23+
/// An error occurred when constructing the prompt. Examine the related error for details.
24+
case promptImageContentError(underlying: Error)
25+
2626
/// A prompt was blocked. See the response's `promptFeedback.blockReason` for more information.
2727
case promptBlocked(response: GenerateContentResponse)
2828

FirebaseVertexAI/Sources/PartsRepresentable+Image.swift

+7-21
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,15 @@ private let imageCompressionQuality: CGFloat = 0.8
2424
/// An enum describing failures that can occur when converting image types to model content data.
2525
/// For some image types like `CIImage`, creating valid model content requires creating a JPEG
2626
/// representation of the image that may not yet exist, which may be computationally expensive.
27-
public enum ImageConversionError: Error {
28-
/// The image that could not be converted.
29-
public enum SourceImage {
30-
#if canImport(UIKit)
31-
case uiImage(UIImage)
32-
#elseif canImport(AppKit)
33-
case nsImage(NSImage)
34-
#endif // canImport(UIKit)
35-
case cgImage(CGImage)
36-
#if canImport(CoreImage)
37-
case ciImage(CIImage)
38-
#endif // canImport(CoreImage)
39-
}
40-
27+
enum ImageConversionError: Error {
4128
/// The image (the receiver of the call `toModelContentParts()`) was invalid.
4229
case invalidUnderlyingImage
4330

4431
/// A valid image destination could not be allocated.
4532
case couldNotAllocateDestination
4633

47-
/// JPEG image data conversion failed, accompanied by the original image, which may be an
48-
/// instance of `NSImage`, `UIImage`, `CGImage`, or `CIImage`.
49-
case couldNotConvertToJPEG(SourceImage)
34+
/// JPEG image data conversion failed.
35+
case couldNotConvertToJPEG
5036
}
5137

5238
#if canImport(UIKit)
@@ -55,7 +41,7 @@ public enum ImageConversionError: Error {
5541
extension UIImage: ThrowingPartsRepresentable {
5642
public func tryPartsValue() throws -> [ModelContent.Part] {
5743
guard let data = jpegData(compressionQuality: imageCompressionQuality) else {
58-
throw ImageConversionError.couldNotConvertToJPEG(.uiImage(self))
44+
throw ImageConversionError.couldNotConvertToJPEG
5945
}
6046
return [ModelContent.Part.inlineData(mimetype: "image/jpeg", data)]
6147
}
@@ -72,7 +58,7 @@ public enum ImageConversionError: Error {
7258
let bmp = NSBitmapImageRep(cgImage: cgImage)
7359
guard let data = bmp.representation(using: .jpeg, properties: [.compressionFactor: 0.8])
7460
else {
75-
throw ImageConversionError.couldNotConvertToJPEG(.nsImage(self))
61+
throw ImageConversionError.couldNotConvertToJPEG
7662
}
7763
return [ModelContent.Part.inlineData(mimetype: "image/jpeg", data)]
7864
}
@@ -97,7 +83,7 @@ public enum ImageConversionError: Error {
9783
if CGImageDestinationFinalize(imageDestination) {
9884
return [.inlineData(mimetype: "image/jpeg", output as Data)]
9985
}
100-
throw ImageConversionError.couldNotConvertToJPEG(.cgImage(self))
86+
throw ImageConversionError.couldNotConvertToJPEG
10187
}
10288
}
10389
#endif // !os(watchOS)
@@ -118,7 +104,7 @@ public enum ImageConversionError: Error {
118104
if let jpegData = jpegData {
119105
return [.inlineData(mimetype: "image/jpeg", jpegData)]
120106
}
121-
throw ImageConversionError.couldNotConvertToJPEG(.ciImage(self))
107+
throw ImageConversionError.couldNotConvertToJPEG
122108
}
123109
}
124110
#endif // canImport(CoreImage)

FirebaseVertexAI/Tests/Unit/PartsRepresentableTests.swift

+12-29
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
// limitations under the License.
1414

1515
import CoreGraphics
16-
import FirebaseVertexAI
1716
import XCTest
1817
#if canImport(UIKit)
1918
import UIKit
@@ -24,6 +23,8 @@ import XCTest
2423
import CoreImage
2524
#endif // canImport(CoreImage)
2625

26+
@testable import FirebaseVertexAI
27+
2728
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
2829
final class PartsRepresentableTests: XCTestCase {
2930
#if !os(watchOS)
@@ -61,22 +62,13 @@ final class PartsRepresentableTests: XCTestCase {
6162
do {
6263
_ = try image.tryPartsValue()
6364
XCTFail("Expected model content from invalid image to error")
64-
} catch {
65-
guard let imageError = (error as? ImageConversionError) else {
66-
XCTFail("Got unexpected error type: \(error)")
67-
return
68-
}
69-
switch imageError {
70-
case let .couldNotConvertToJPEG(source):
71-
guard case let .ciImage(ciImage) = source else {
72-
XCTFail("Unexpected image source: \(source)")
73-
return
74-
}
75-
XCTAssertEqual(ciImage, image)
76-
default:
77-
XCTFail("Expected image conversion error, got \(imageError) instead")
65+
} catch let imageError as ImageConversionError {
66+
guard case .couldNotConvertToJPEG = imageError else {
67+
XCTFail("Expected JPEG conversion error, got \(imageError) instead.")
7868
return
7969
}
70+
} catch {
71+
XCTFail("Got unexpected error type: \(error)")
8072
}
8173
}
8274
#endif // canImport(CoreImage)
@@ -87,22 +79,13 @@ final class PartsRepresentableTests: XCTestCase {
8779
do {
8880
_ = try image.tryPartsValue()
8981
XCTFail("Expected model content from invalid image to error")
90-
} catch {
91-
guard let imageError = (error as? ImageConversionError) else {
92-
XCTFail("Got unexpected error type: \(error)")
93-
return
94-
}
95-
switch imageError {
96-
case let .couldNotConvertToJPEG(source):
97-
guard case let .uiImage(uiImage) = source else {
98-
XCTFail("Unexpected image source: \(source)")
99-
return
100-
}
101-
XCTAssertEqual(uiImage, image)
102-
default:
103-
XCTFail("Expected image conversion error, got \(imageError) instead")
82+
} catch let imageError as ImageConversionError {
83+
guard case .couldNotConvertToJPEG = imageError else {
84+
XCTFail("Expected JPEG conversion error, got \(imageError) instead.")
10485
return
10586
}
87+
} catch {
88+
XCTFail("Got unexpected error type: \(error)")
10689
}
10790
}
10891

0 commit comments

Comments
 (0)