-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[Firebase AI] Add internal JSON Schema support in GenerationConfig
#15404
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
base: main
Are you sure you want to change the base?
Conversation
# Conflicts: # FirebaseAI/Tests/Unit/GenerationConfigTests.swift
4343ab6
to
397ae19
Compare
/gemini review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request adds internal support for response_json_schema
in GenerationConfig
. The changes include adding the new property, a corresponding internal initializer, and updating the Encodable
conformance. The implementation is well-tested with new unit and integration tests that cover various schema types. The changes look good and the new feature is well-implemented. I've left one comment regarding code duplication in the integration tests which could be addressed to improve maintainability.
func generateContentJSONSchemaItems(_ config: InstanceConfig) async throws { | ||
let model = FirebaseAI.componentInstance(config).generativeModel( | ||
modelName: ModelNames.gemini2_5_FlashLite, | ||
generationConfig: GenerationConfig( | ||
responseMIMEType: "application/json", | ||
responseJSONSchema: [ | ||
"type": .string("array"), | ||
"description": .string("A list of city names"), | ||
"items": .object([ | ||
"type": .string("string"), | ||
"description": .string("The name of the city"), | ||
]), | ||
"minItems": .number(3), | ||
"maxItems": .number(5), | ||
] | ||
), | ||
safetySettings: safetySettings | ||
) | ||
let prompt = "What are the biggest cities in Canada?" | ||
let response = try await model.generateContent(prompt) | ||
let text = try #require(response.text).trimmingCharacters(in: .whitespacesAndNewlines) | ||
let jsonData = try #require(text.data(using: .utf8)) | ||
let decodedJSON = try JSONDecoder().decode([String].self, from: jsonData) | ||
#expect(decodedJSON.count >= 3, "Expected at least 3 cities, but got \(decodedJSON.count)") | ||
#expect(decodedJSON.count <= 5, "Expected at most 5 cities, but got \(decodedJSON.count)") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a lot of duplicated code between this test and generateContentSchemaItems
. The logic for making the request and asserting the response is identical. Consider extracting this common logic into a private helper function to improve maintainability and reduce code duplication. This new helper could take a GenerativeModel
instance as a parameter.
This same pattern of duplication exists for other new tests in this file (generateContentJSONSchemaNumberRange
, generateContentJSONSchemaNumberRangeMultiType
, generateContentAnyOfJSONSchema
), and they would also benefit from a similar refactoring.
Added internal support for JSON Schema (
response_json_schema
) inGenerationConfig
.#no-changelog