diff --git a/FirebaseVertexAI/CHANGELOG.md b/FirebaseVertexAI/CHANGELOG.md index f3d471969c3..21271329704 100644 --- a/FirebaseVertexAI/CHANGELOG.md +++ b/FirebaseVertexAI/CHANGELOG.md @@ -22,6 +22,9 @@ is now optional (`Int?`); it may be `null` in cases such as when a `GenerateContentRequest` contains only images or other non-text content. (#13721) +- [changed] The default request timeout is now 180 seconds instead of the + platform-default value of 60 seconds for a `URLRequest`; this timeout may + still be customized in `RequestOptions`. (#13722) # 11.3.0 - [added] Added `Decodable` conformance for `FunctionResponse`. (#13606) diff --git a/FirebaseVertexAI/Sources/GenerativeAIRequest.swift b/FirebaseVertexAI/Sources/GenerativeAIRequest.swift index 08b178d5808..55f7af7d1be 100644 --- a/FirebaseVertexAI/Sources/GenerativeAIRequest.swift +++ b/FirebaseVertexAI/Sources/GenerativeAIRequest.swift @@ -28,7 +28,7 @@ protocol GenerativeAIRequest: Encodable { public struct RequestOptions { /// The request’s timeout interval in seconds; if not specified uses the default value for a /// `URLRequest`. - let timeout: TimeInterval? + let timeout: TimeInterval /// The API version to use in requests to the backend. let apiVersion = "v2beta" @@ -36,9 +36,8 @@ public struct RequestOptions { /// Initializes a request options object. /// /// - Parameters: - /// - timeout The request’s timeout interval in seconds; if not specified uses the default value - /// for a `URLRequest`. - public init(timeout: TimeInterval? = nil) { + /// - timeout The request’s timeout interval in seconds; defaults to 180 seconds. + public init(timeout: TimeInterval = 180.0) { self.timeout = timeout } } diff --git a/FirebaseVertexAI/Sources/GenerativeAIService.swift b/FirebaseVertexAI/Sources/GenerativeAIService.swift index 3ebbf69f102..36a208440fb 100644 --- a/FirebaseVertexAI/Sources/GenerativeAIService.swift +++ b/FirebaseVertexAI/Sources/GenerativeAIService.swift @@ -191,10 +191,7 @@ struct GenerativeAIService { let encoder = JSONEncoder() encoder.keyEncodingStrategy = .convertToSnakeCase urlRequest.httpBody = try encoder.encode(request) - - if let timeoutInterval = request.options.timeout { - urlRequest.timeoutInterval = timeoutInterval - } + urlRequest.timeoutInterval = request.options.timeout return urlRequest } diff --git a/FirebaseVertexAI/Tests/Unit/GenerativeModelTests.swift b/FirebaseVertexAI/Tests/Unit/GenerativeModelTests.swift index c7cfe36df70..0dc352291b2 100644 --- a/FirebaseVertexAI/Tests/Unit/GenerativeModelTests.swift +++ b/FirebaseVertexAI/Tests/Unit/GenerativeModelTests.swift @@ -1267,7 +1267,7 @@ final class GenerativeModelTests: XCTestCase { private func httpRequestHandler(forResource name: String, withExtension ext: String, statusCode: Int = 200, - timeout: TimeInterval = URLRequest.defaultTimeoutInterval(), + timeout: TimeInterval = RequestOptions().timeout, appCheckToken: String? = nil, authToken: String? = nil) throws -> ((URLRequest) throws -> ( URLResponse, @@ -1316,14 +1316,6 @@ private extension String { } } -private extension URLRequest { - /// Returns the default `timeoutInterval` for a `URLRequest`. - static func defaultTimeoutInterval() -> TimeInterval { - let placeholderURL = URL(string: "https://example.com")! - return URLRequest(url: placeholderURL).timeoutInterval - } -} - @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) class AppCheckInteropFake: NSObject, AppCheckInterop { /// The placeholder token value returned when an error occurs