Skip to content

feat(firebaseai): Add ability for Schema class to export to json schema#18131

Open
cynthiajoan wants to merge 18 commits intomainfrom
firebaseai/schema_to_json_schema
Open

feat(firebaseai): Add ability for Schema class to export to json schema#18131
cynthiajoan wants to merge 18 commits intomainfrom
firebaseai/schema_to_json_schema

Conversation

@cynthiajoan
Copy link
Collaborator

Description

Replace this paragraph with a description of what this PR is doing. If you're modifying existing behavior, describe the existing behavior, how this PR is changing it, and what motivated the change.

Related Issues

Replace this paragraph with a list of issues related to this PR from the issue database. Indicate, which of these issues are resolved or fixed by this PR. Note that you'll have to prefix the issue numbers with flutter/flutter#.

Checklist

Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes ([x]).
This will ensure a smooth and quick review process. Updating the pubspec.yaml and changelogs is not required.

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • My PR includes unit or integration tests for all changed/updated/fixed behaviors (See Contributor Guide).
  • All existing and new tests are passing.
  • I updated/added relevant documentation (doc comments with ///).
  • The analyzer (melos run analyze) does not report any problems on my PR.
  • I read and followed the Flutter Style Guide.
  • I signed the CLA.
  • I am willing to follow-up on review comments in a timely manner.

Breaking Change

Does your PR require plugin users to manually update their apps to accommodate your change?

  • Yes, this is a breaking change.
  • No, this is not a breaking change.
@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.

@cynthiajoan cynthiajoan changed the title feat(firebaseai) : Add ability for schema to json schema feat(firebaseai) : Add ability for Schema class to export to json schema Mar 20, 2026
@cynthiajoan cynthiajoan changed the title feat(firebaseai) : Add ability for Schema class to export to json schema feat(firebaseai): Add ability for Schema class to export to json schema Mar 20, 2026
@cynthiajoan
Copy link
Collaborator Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a 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 introduces a significant enhancement to the firebase_ai package by adding support for full JSON Schema in function declarations. The changes are well-implemented, with updates to the Schema and FunctionDeclaration classes to generate standard JSON Schema, comprehensive tests, and a detailed example demonstrating the new capabilities, including complex schemas and references. The code quality is high. I have one minor suggestion to improve the documentation for clarity.

cynthiajoan and others added 3 commits March 21, 2026 16:58
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
expect(schema.toJson(), {
'type': 'object',
'properties': {
'metadata': {r'$ref': '#/metadata_schema'},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not certain that #/metadata_schema would resolve to metadata_schema since it's nested in $defs. Since we can't reference any external schemas, I wonder if it would be safer to standardize on #/$defs/SomeName (so #/$defs/metadata_schema in this case).

It's definitely possible that there's a shorthand notation that I don't know about but I know that # works for self-referencing and #/$defs/schema_name works. In this example:

Sometimes we have small subschemas that are only intended for use in the current schema and it doesn't make sense to define them as separate schemas. Although we can identify any subschema using JSON Pointers or named anchors, the $defs keyword gives us a standardized place to keep subschemas intended for reuse in the current schema document.

name is a subschema within the schema and it's referenced with, e.g., "first_name": { "$ref": "#/$defs/name" }.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in the example you linked, what does the address it resolves to?
"shipping_address": { "$ref": "/schemas/address" }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It resolves to https://example.com/schemas/address (due to "$id": "https://example.com/schemas/customer" as the Base URI for the schema). The tutorial starts from Base URI on that page.

Note: None of the external schema stuff is supported by Gemini, as far as I'm aware though.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That explains, in that case I should add the #/$defs/, and add some extra documentation in the api usage.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but after making that change, I find it doesn't work with this test case. If i make it points to #/$defs/transactionDef, the model will complain reference to undefined schema at properties.transactionsBlock.properties.transactionsList.items

Map<String, Object> toJson() => {
if (type != SchemaType.anyOf && type != SchemaType.ref)
'type': nullable == true ? [type.name, 'null'] : type.name,
if (ref case final ref?) r'$ref': ref,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may want to consider prefixing ref with #/$defs/. See the comment below for more details.

/// This class supports `$ref` and `$defs` for reusable sub-schemas.
final class JSONSchema extends Schema {
// ignore: public_member_api_docs
JSONSchema(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: I'm not certain what Gemini's behaviour is if additionalProperties is omitted. The @Generable macro aways produces a schema with "additionalProperties" : false so that's all I've tested.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it says "Can be a boolean or a schema.", confused of what data structure I should use if I add it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd recommend just using a boolean if it can be changed later, or omitting it if we find there are no problems. "additionalProperties" : false is essentially saying "don't generate any properties that I didn't ask for."

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

During my testing I don't have it and the model seems fine with that. So I would omit for now

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

Labels

None yet

3 participants