feat(firebaseai): Add ability for Schema class to export to json schema#18131
feat(firebaseai): Add ability for Schema class to export to json schema#18131cynthiajoan wants to merge 18 commits intomainfrom
Conversation
Using Gemini Code AssistThe 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
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 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. |
|
/gemini review |
There was a problem hiding this comment.
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.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
…/firebase/flutterfire into firebaseai/schema_to_json_schema
| expect(schema.toJson(), { | ||
| 'type': 'object', | ||
| 'properties': { | ||
| 'metadata': {r'$ref': '#/metadata_schema'}, |
There was a problem hiding this comment.
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" }.
There was a problem hiding this comment.
in the example you linked, what does the address it resolves to?
"shipping_address": { "$ref": "/schemas/address" }
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
That explains, in that case I should add the #/$defs/, and add some extra documentation in the api usage.
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
it says "Can be a boolean or a schema.", confused of what data structure I should use if I add it.
There was a problem hiding this comment.
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."
There was a problem hiding this comment.
During my testing I don't have it and the model seems fine with that. So I would omit for now
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.yamland changelogs is not required.///).melos run analyze) does not report any problems on my PR.Breaking Change
Does your PR require plugin users to manually update their apps to accommodate your change?