Skip to content

Comments

[AI] Add GenerativeModel.generateObject(_:prompt:)#15868

Draft
andrewheard wants to merge 3 commits intomainfrom
ah/ai-generate-object
Draft

[AI] Add GenerativeModel.generateObject(_:prompt:)#15868
andrewheard wants to merge 3 commits intomainfrom
ah/ai-generate-object

Conversation

@andrewheard
Copy link
Contributor

WIP - Added a generateObject(_:prompt:) method to GenerativeModel based on the Android implementation of generateObject(jsonSchema:prompt:) / GenerateObjectResponse. This approach uses the "Decoder Spy" technique to automatically generate a JSON Schema from a Decodable type without a macro and works on iOS 15+. The SchemaConstraintsProvider protocol allows additional constraints (similar to @Guide).

Usage Example
let model = FirebaseAI.firebaseAI().generativeModel(modelName: "gemini-2.5-flash")
let prompt = """
Generate a medieval knight character for an action game. Give them a magical backstory.
"""

let response = try await model.generateObject(GameCharacter.self, prompt: prompt)

guard let gameCharacter = try response.getObject() else {
  fatalError("Failed to generate a character from response: \(response.response)")
}
print(gameCharacter)

Note: See the integration tests for examples with additional constraints provided by conforming to SchemaConstraintsProvider.

Auto-Generated JSON Schema
{
  "type": "object",
  "title": "GameCharacter",
  "properties": {
    "firstName": {
      "type": "string"
    },
    "backstory": {
      "type": "string"
    },
    "middleName": {
      "type": "string"
    },
    "age": {
      "type": "integer"
    },
    "lastName": {
      "type": "string"
    }
  },
  "required": [
    "firstName",
    "lastName",
    "age",
    "backstory"
  ],
  "additionalProperties": false
}
Example Output

GameCharacter(firstName: "Kaelan", middleName: nil, lastName: "Stonehaven", age: 32, backstory: "Sir Kaelan Stonehaven, born to a lineage of knights, seemed destined for a life of steel and valor. However, deep within his family\'s ancestral castle, an ancient pact with a forgotten nature spirit stirred. On his 18th birthday, while performing a solitary vigil in the crypts, a surge of emerald light infused him. He awoke with the ability to commune with stone and earth, and to mend his wounds with moss and root. He secretly trained with an old hermit who revealed the forgotten druidic bloodline that ran through Kaelan\'s veins, a power suppressed by generations of mundane knighthood. He now serves his kingdom, his silver armor shimmering not just from polish, but from the subtle, protective magic woven into its very mithril threads, guarding the realm not just with his sword, but with the ancient, living power of the earth.")

Cross-Platform Consistency

The GenerateObjectResponse should likely return the type directly and generateObject(_:prompt:) could throw, including the GenerateContentResponse, rather than going through an additional getObject() call. This approach was only taken to align as closely as possible across platforms.

#no-changelog

@gemini-code-assist
Copy link
Contributor

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

@danger-firebase-ios
Copy link

1 Warning
⚠️ New public headers were added, did you remember to add them to the umbrella header?

Generated by 🚫 Danger

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant