-
-
Notifications
You must be signed in to change notification settings - Fork 23.1k
feat: Add structured JSON output support to Agent Node #5470
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
- Add agentStructuredOutput input parameter matching LLM Node structure - Implement configureStructuredOutput method to convert schema to Zod - Add createZodSchemaFromJSON helper for complex JSON schemas - Configure structured output before binding tools (required order) - Disable streaming when structured output is enabled - Extract structured fields in prepareOutputObject method - Resolves issue FlowiseAI#5256
Summary of ChangesHello @aibysid, 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 introduces a significant enhancement to the Agent Node by adding robust support for structured JSON output. Users can now define a desired JSON schema for the agent's responses, which is then converted into a Zod schema for validation and enforcement. This feature provides greater control over the format of agent outputs, improving integration with downstream systems and ensuring predictable data structures. Highlights
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 structured JSON output capabilities to the Agent node, which is a great feature. The implementation correctly disables streaming when this feature is active and adds the necessary UI and backend logic to build a Zod schema from user input. My review focuses on improving the robustness and clarity of the new schema generation logic. I've identified an issue with how empty enums are handled, found an unreachable code block that should be removed, and suggest documenting a custom feature to make it usable for end-users.
| } else if (Array.isArray(value)) { | ||
| // Array values without a type property | ||
| if (value.length > 0) { | ||
| // If the array has items, recursively create a schema for the first item | ||
| const itemSchema = this.createZodSchemaFromJSON(value[0]) | ||
| schemaObj[key] = z.array(itemSchema) | ||
| } else { | ||
| // Empty array, allow any array | ||
| schemaObj[key] = z.array(z.any()) | ||
| } |
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.
| } | ||
| } | ||
| }`, | ||
| description: 'JSON schema for the structured output', |
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 description for jsonSchema is missing important information about a custom feature. The implementation allows marking fields as optional using a non-standard "optional": true property inside a field's definition. This should be documented here to make the feature discoverable and usable for users.
For example, you could update the description to:
JSON schema for the structured output. To mark a field as optional, add "optional": true to its definition.
| const enumValues = sch.enumValues?.split(',').map((item: string) => item.trim()) || [] | ||
| zodObj[sch.key] = z | ||
| .enum(enumValues.length ? (enumValues as [string, ...string[]]) : ['default']) | ||
| .describe(sch.description || '') |
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 current implementation for enum types has a potentially confusing fallback. If a user selects the enum type but leaves enumValues empty, the schema defaults to z.enum(['default']). This forces the model to output the literal string "default", which is likely not the user's intent and can be difficult to debug. It would be more robust to throw an error if enumValues is empty for an enum type, providing clear feedback to the user about the missing configuration.
| const enumValues = sch.enumValues?.split(',').map((item: string) => item.trim()) || [] | |
| zodObj[sch.key] = z | |
| .enum(enumValues.length ? (enumValues as [string, ...string[]]) : ['default']) | |
| .describe(sch.description || '') | |
| const enumValues = sch.enumValues?.split(',').map((item: string) => item.trim()).filter(Boolean) || []; | |
| if (enumValues.length === 0) { | |
| throw new Error(`Enum values for key "${sch.key}" cannot be empty when type is "enum".`); | |
| } | |
| zodObj[sch.key] = z.enum(enumValues as [string, ...string[]]).describe(sch.description || ''); |
Uh oh!
There was an error while loading. Please reload this page.