-
Notifications
You must be signed in to change notification settings - Fork 121
feat: Add JSON Schema generation for plugin manifests #283
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
Summary of ChangesHello @yamachu, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the developer experience for Dify plugin authors by integrating JSON Schema generation into the existing documentation pipeline. By providing a machine-readable schema for plugin manifests, IDEs can offer real-time validation and autocompletion, thereby reducing errors and streamlining the development workflow for creating and maintaining plugin configurations. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. 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. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
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 introduces a valuable feature for generating JSON Schemas from plugin manifests, which will greatly improve validation capabilities. The implementation is well-structured. I've provided a few suggestions in dify_plugin/core/documentation/generator.py to enhance the robustness of the schema generation, particularly around type detection for enums and handling of default values and unknown types. These changes should help prevent subtle bugs and make the generator more maintainable.
| if enum_values: | ||
| first_value = enum_values[0] | ||
| if isinstance(first_value, str): | ||
| value_type = "string" | ||
| elif isinstance(first_value, int): | ||
| value_type = "integer" | ||
| elif isinstance(first_value, float): | ||
| value_type = "number" | ||
| elif isinstance(first_value, bool): | ||
| value_type = "boolean" | ||
| else: | ||
| value_type = "string" | ||
| else: | ||
| value_type = "string" |
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.
The logic to determine the JSON schema type for an Enum only considers the type of the first enum value. This can lead to incorrect schema generation for enums with mixed value types (e.g., strings and integers).
A more robust approach is to inspect all enum values to determine the set of types present.
| if enum_values: | |
| first_value = enum_values[0] | |
| if isinstance(first_value, str): | |
| value_type = "string" | |
| elif isinstance(first_value, int): | |
| value_type = "integer" | |
| elif isinstance(first_value, float): | |
| value_type = "number" | |
| elif isinstance(first_value, bool): | |
| value_type = "boolean" | |
| else: | |
| value_type = "string" | |
| else: | |
| value_type = "string" | |
| if enum_values: | |
| json_types = set() | |
| for value in enum_values: | |
| if isinstance(value, str): | |
| json_types.add("string") | |
| elif isinstance(value, bool): | |
| json_types.add("boolean") | |
| elif isinstance(value, int): | |
| json_types.add("integer") | |
| elif isinstance(value, float): | |
| json_types.add("number") | |
| if not json_types: | |
| value_type = "string" # Fallback for unsupported types | |
| elif len(json_types) == 1: | |
| value_type = json_types.pop() | |
| else: | |
| value_type = sorted(list(json_types)) | |
| else: | |
| value_type = "string" |
| field_schema["description"] = description | ||
|
|
||
| # Handle default values | ||
| if field_info.default is not None and str(field_info.default) != "PydanticUndefined": |
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.
The check str(field_info.default) != "PydanticUndefined" is brittle as it relies on the string representation of an internal Pydantic object, which could change in future versions. A more robust way to check if a field has a meaningful default value is to use field_info.is_required().
| if field_info.default is not None and str(field_info.default) != "PydanticUndefined": | |
| if not field_info.is_required() and field_info.default is not None: |
| schemas = [self._get_json_schema_type(arg) for arg in args] | ||
| return {"anyOf": schemas} | ||
|
|
||
| return {} |
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.
The function _get_json_schema_type returns an empty dictionary {} for any type it cannot handle. While this is a valid JSON Schema that allows any value, it can hide issues where a type is not being converted correctly. This might lead to a less strict and less useful schema than intended.
Consider raising an error for unhandled types to make the schema generation process fail-fast and ensure all types are explicitly handled.
| return {} | |
| raise NotImplementedError(f"Type {field_type} cannot be converted to JSON Schema.") |
Summary
$schema.Pythondefinitions may be merged). This will need a follow-up refinement if collisions appear.I tested ...
uv run ./dify_plugin/cli.py generate-docsuv run ./dify_plugin/cli.py generate-docs --format json-schemaPull Request Checklist
Thank you for your contribution! Before submitting your PR, please make sure you have completed the following checks:
Compatibility Check
README.mdREADME.mdREADME.mdREADME.mdAvailable Checks