Skip to content

[Vertex AI] Fix unsupported model name check introduced in #14610 #14629

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 28, 2025
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
16 changes: 5 additions & 11 deletions FirebaseVertexAI/Sources/GenerativeModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public final class GenerativeModel: Sendable {
/// Initializes a new remote model with the given parameters.
///
/// - Parameters:
/// - name: The name of the model to use, for example `"gemini-1.0-pro"`.
/// - modelResourceName: The resource name of the model to use, for example
/// `"projects/{project-id}/locations/{location-id}/publishers/google/models/{model-name}"`.
/// - firebaseInfo: Firebase data used by the SDK, including project ID and API key.
/// - apiConfig: Configuration for the backend API used by this model.
/// - generationConfig: The content generation parameters your model should use.
Expand All @@ -64,7 +65,7 @@ public final class GenerativeModel: Sendable {
/// only text content is supported.
/// - requestOptions: Configuration parameters for sending requests to the backend.
/// - urlSession: The `URLSession` to use for requests; defaults to `URLSession.shared`.
init(name: String,
init(modelResourceName: String,
firebaseInfo: FirebaseInfo,
apiConfig: APIConfig,
generationConfig: GenerationConfig? = nil,
Expand All @@ -74,14 +75,7 @@ public final class GenerativeModel: Sendable {
systemInstruction: ModelContent? = nil,
requestOptions: RequestOptions,
urlSession: URLSession = .shared) {
if !name.starts(with: GenerativeModel.geminiModelNamePrefix) {
VertexLog.warning(code: .unsupportedGeminiModel, """
Unsupported Gemini model "\(name)"; see \
https://firebase.google.com/docs/vertex-ai/models for a list supported Gemini model names.
""")
}

modelResourceName = name
self.modelResourceName = modelResourceName
self.apiConfig = apiConfig
generativeAIService = GenerativeAIService(
firebaseInfo: firebaseInfo,
Expand All @@ -108,7 +102,7 @@ public final class GenerativeModel: Sendable {
`\(VertexLog.enableArgumentKey)` as a launch argument in Xcode.
""")
}
VertexLog.debug(code: .generativeModelInitialized, "Model \(name) initialized.")
VertexLog.debug(code: .generativeModelInitialized, "Model \(modelResourceName) initialized.")
}

/// Generates content from String and/or image inputs, given to the model as a prompt, that are
Expand Down
11 changes: 2 additions & 9 deletions FirebaseVertexAI/Sources/Types/Public/Imagen/ImagenModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,14 @@ public final class ImagenModel {
/// Configuration parameters for sending requests to the backend.
let requestOptions: RequestOptions

init(name: String,
init(modelResourceName: String,
firebaseInfo: FirebaseInfo,
apiConfig: APIConfig,
generationConfig: ImagenGenerationConfig?,
safetySettings: ImagenSafetySettings?,
requestOptions: RequestOptions,
urlSession: URLSession = .shared) {
if !name.starts(with: ImagenModel.imagenModelNamePrefix) {
VertexLog.warning(code: .unsupportedImagenModel, """
Unsupported Imagen model "\(name)"; see \
https://firebase.google.com/docs/vertex-ai/models for a list supported Imagen model names.
""")
}

modelResourceName = name
self.modelResourceName = modelResourceName
self.apiConfig = apiConfig
generativeAIService = GenerativeAIService(
firebaseInfo: firebaseInfo,
Expand Down
18 changes: 16 additions & 2 deletions FirebaseVertexAI/Sources/VertexAI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,15 @@ public class VertexAI {
systemInstruction: ModelContent? = nil,
requestOptions: RequestOptions = RequestOptions())
-> GenerativeModel {
if !modelName.starts(with: GenerativeModel.geminiModelNamePrefix) {
VertexLog.warning(code: .unsupportedGeminiModel, """
Unsupported Gemini model "\(modelName)"; see \
https://firebase.google.com/docs/vertex-ai/models for a list supported Gemini model names.
""")
}

return GenerativeModel(
name: modelResourceName(modelName: modelName),
modelResourceName: modelResourceName(modelName: modelName),
firebaseInfo: firebaseInfo,
apiConfig: apiConfig,
generationConfig: generationConfig,
Expand Down Expand Up @@ -102,8 +109,15 @@ public class VertexAI {
public func imagenModel(modelName: String, generationConfig: ImagenGenerationConfig? = nil,
safetySettings: ImagenSafetySettings? = nil,
requestOptions: RequestOptions = RequestOptions()) -> ImagenModel {
if !modelName.starts(with: ImagenModel.imagenModelNamePrefix) {
VertexLog.warning(code: .unsupportedImagenModel, """
Unsupported Imagen model "\(modelName)"; see \
https://firebase.google.com/docs/vertex-ai/models for a list supported Imagen model names.
""")
}

return ImagenModel(
name: modelResourceName(modelName: modelName),
modelResourceName: modelResourceName(modelName: modelName),
firebaseInfo: firebaseInfo,
apiConfig: apiConfig,
generationConfig: generationConfig,
Expand Down
2 changes: 1 addition & 1 deletion FirebaseVertexAI/Tests/Unit/ChatTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
#if os(watchOS)
throw XCTSkip("Custom URL protocols are unsupported in watchOS 2 and later.")
#endif // os(watchOS)
MockURLProtocol.requestHandler = { request in

Check warning on line 48 in FirebaseVertexAI/Tests/Unit/ChatTests.swift

View workflow job for this annotation

GitHub Actions / spm-unit (macos-15, Xcode_16.2, watchOS)

code after 'throw' will never be executed
let response = HTTPURLResponse(
url: request.url!,
statusCode: 200,
Expand All @@ -59,7 +59,7 @@
options: FirebaseOptions(googleAppID: "ignore",
gcmSenderID: "ignore"))
let model = GenerativeModel(
name: "my-model",
modelResourceName: "my-model",
firebaseInfo: FirebaseInfo(
projectID: "my-project-id",
apiKey: "API_KEY",
Expand Down
28 changes: 14 additions & 14 deletions FirebaseVertexAI/Tests/Unit/GenerativeModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
configuration.protocolClasses = [MockURLProtocol.self]
urlSession = try XCTUnwrap(URLSession(configuration: configuration))
model = GenerativeModel(
name: testModelResourceName,
modelResourceName: testModelResourceName,
firebaseInfo: testFirebaseInfo(),
apiConfig: apiConfig,
tools: nil,
Expand Down Expand Up @@ -276,7 +276,7 @@
)
let model = GenerativeModel(
// Model name is prefixed with "models/".
name: "models/test-model",
modelResourceName: "models/test-model",
firebaseInfo: testFirebaseInfo(),
apiConfig: apiConfig,
tools: nil,
Expand Down Expand Up @@ -399,7 +399,7 @@
func testGenerateContent_appCheck_validToken() async throws {
let appCheckToken = "test-valid-token"
model = GenerativeModel(
name: testModelResourceName,
modelResourceName: testModelResourceName,
firebaseInfo: testFirebaseInfo(appCheck: AppCheckInteropFake(token: appCheckToken)),
apiConfig: apiConfig,
tools: nil,
Expand All @@ -420,7 +420,7 @@
func testGenerateContent_dataCollectionOff() async throws {
let appCheckToken = "test-valid-token"
model = GenerativeModel(
name: testModelResourceName,
modelResourceName: testModelResourceName,
firebaseInfo: testFirebaseInfo(appCheck: AppCheckInteropFake(token: appCheckToken),
privateAppID: true),
apiConfig: apiConfig,
Expand All @@ -442,7 +442,7 @@

func testGenerateContent_appCheck_tokenRefreshError() async throws {
model = GenerativeModel(
name: testModelResourceName,
modelResourceName: testModelResourceName,
firebaseInfo: testFirebaseInfo(appCheck: AppCheckInteropFake(error: AppCheckErrorFake())),
apiConfig: apiConfig,
tools: nil,
Expand All @@ -463,7 +463,7 @@
func testGenerateContent_auth_validAuthToken() async throws {
let authToken = "test-valid-token"
model = GenerativeModel(
name: testModelResourceName,
modelResourceName: testModelResourceName,
firebaseInfo: testFirebaseInfo(auth: AuthInteropFake(token: authToken)),
apiConfig: apiConfig,
tools: nil,
Expand All @@ -483,7 +483,7 @@

func testGenerateContent_auth_nilAuthToken() async throws {
model = GenerativeModel(
name: testModelResourceName,
modelResourceName: testModelResourceName,
firebaseInfo: testFirebaseInfo(auth: AuthInteropFake(token: nil)),
apiConfig: apiConfig,
tools: nil,
Expand All @@ -503,7 +503,7 @@

func testGenerateContent_auth_authTokenRefreshError() async throws {
model = GenerativeModel(
name: "my-model",
modelResourceName: "my-model",
firebaseInfo: testFirebaseInfo(auth: AuthInteropFake(error: AuthErrorFake())),
apiConfig: apiConfig,
tools: nil,
Expand Down Expand Up @@ -900,7 +900,7 @@
)
let requestOptions = RequestOptions(timeout: expectedTimeout)
model = GenerativeModel(
name: testModelResourceName,
modelResourceName: testModelResourceName,
firebaseInfo: testFirebaseInfo(),
apiConfig: apiConfig,
tools: nil,
Expand Down Expand Up @@ -1204,7 +1204,7 @@
func testGenerateContentStream_appCheck_validToken() async throws {
let appCheckToken = "test-valid-token"
model = GenerativeModel(
name: testModelResourceName,
modelResourceName: testModelResourceName,
firebaseInfo: testFirebaseInfo(appCheck: AppCheckInteropFake(token: appCheckToken)),
apiConfig: apiConfig,
tools: nil,
Expand All @@ -1225,7 +1225,7 @@

func testGenerateContentStream_appCheck_tokenRefreshError() async throws {
model = GenerativeModel(
name: testModelResourceName,
modelResourceName: testModelResourceName,
firebaseInfo: testFirebaseInfo(appCheck: AppCheckInteropFake(error: AppCheckErrorFake())),
apiConfig: apiConfig,
tools: nil,
Expand Down Expand Up @@ -1375,7 +1375,7 @@
)
let requestOptions = RequestOptions(timeout: expectedTimeout)
model = GenerativeModel(
name: testModelResourceName,
modelResourceName: testModelResourceName,
firebaseInfo: testFirebaseInfo(),
apiConfig: apiConfig,
tools: nil,
Expand Down Expand Up @@ -1451,7 +1451,7 @@
parts: "You are a calculator. Use the provided tools."
)
model = GenerativeModel(
name: testModelResourceName,
modelResourceName: testModelResourceName,
firebaseInfo: testFirebaseInfo(),
apiConfig: apiConfig,
generationConfig: generationConfig,
Expand Down Expand Up @@ -1511,7 +1511,7 @@
)
let requestOptions = RequestOptions(timeout: expectedTimeout)
model = GenerativeModel(
name: testModelResourceName,
modelResourceName: testModelResourceName,
firebaseInfo: testFirebaseInfo(),
apiConfig: apiConfig,
tools: nil,
Expand Down Expand Up @@ -1552,7 +1552,7 @@
#if os(watchOS)
throw XCTSkip("Custom URL protocols are unsupported in watchOS 2 and later.")
#endif // os(watchOS)
return { request in

Check warning on line 1555 in FirebaseVertexAI/Tests/Unit/GenerativeModelTests.swift

View workflow job for this annotation

GitHub Actions / spm-unit (macos-15, Xcode_16.2, watchOS)

code after 'throw' will never be executed
// This is *not* an HTTPURLResponse
let response = URLResponse(
url: request.url!,
Expand Down Expand Up @@ -1580,7 +1580,7 @@
#if os(watchOS)
throw XCTSkip("Custom URL protocols are unsupported in watchOS 2 and later.")
#endif // os(watchOS)
let bundle = BundleTestUtil.bundle()

Check warning on line 1583 in FirebaseVertexAI/Tests/Unit/GenerativeModelTests.swift

View workflow job for this annotation

GitHub Actions / spm-unit (macos-15, Xcode_16.2, watchOS)

code after 'throw' will never be executed
let fileURL = try XCTUnwrap(
bundle.url(forResource: name, withExtension: ext, subdirectory: subpath)
)
Expand Down
Loading