-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat: Allow Union types in input/output schemas #2691
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
This change allows the use of `Union` types in the `input_schema` and `output_schema` of `LlmAgent` and `AgentTool`. This is achieved by: - Removing the restriction on `anyOf` in the function parameter parsing utility. - Using `pydantic.TypeAdapter` to validate and serialize the input and output when the schema is a `Union`. New test cases have been added to cover these scenarios.
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.
Summary of Changes
Hello @dylan-apex, 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 support for Union types within the input_schema and output_schema of LlmAgent and AgentTool classes. This enhancement significantly increases the flexibility of defining LLM input and output structures, allowing for more complex and adaptive schema definitions. It resolves existing issues related to schema limitations by leveraging Pydantic's TypeAdapter for robust validation and serialization of union-typed data.
Highlights
- Enabled Union Types: LlmAgent and AgentTool now support Union types for their input_schema and output_schema, allowing for more flexible data structures.
- Pydantic TypeAdapter Integration: Utilized pydantic.TypeAdapter for dynamic validation and serialization of data against Union schemas, ensuring correct parsing of diverse input/output formats.
- Relaxed Schema Constraints: The input_schema and output_schema attributes in LlmAgent have been broadened from Optional[type[BaseModel]] to Optional[Any], accommodating non-BaseModel types within Unions.
- Removed anyOf Restriction: The previous limitation on anyOf (used by Pydantic for Union types) in function parameter parsing has been lifted, enabling full Union type support.
- Comprehensive Test Coverage: New unit tests have been added to validate the correct handling of Union types in both input and output schemas for LlmAgent and AgentTool, including scenarios with BaseModel and Literal types.
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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command>
or @gemini-code-assist <command>
. Below is a summary of the supported commands.
Feature | Command | Description |
---|---|---|
Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
Help | /gemini help |
Displays a list of available commands. |
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.
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
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
Response from ADK Triaging Agent Hello @dylan-apex, thank you for your contribution! To help reviewers better understand the scope and impact of your changes, could you please add a You can find more information about our contribution guidelines here: https://github.com/google/adk-python/blob/main/CONTRIBUTING.md#requirement-for-prs |
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 correctly implements support for Union
types in input_schema
and output_schema
by leveraging pydantic.TypeAdapter
and removing the restriction on anyOf
. The changes are accompanied by a comprehensive set of new unit tests that cover various scenarios involving Union
types.
I have identified a potential bug in the input serialization logic that could occur with Union
types containing primitives, a typo in a new test case, and an opportunity to refactor some duplicated code to enhance maintainability. Please see my detailed comments below.
Warning Gemini encountered an error creating the review. You can try again by commenting |
This change allows the use of
Union
types in theinput_schema
andoutput_schema
ofLlmAgent
andAgentTool
. It also allows you to use Union types in classes that inherit from BaseModel.According to googleapis/python-genai#447 this has been a supported feature since v1.6.0 and the minimum version for this repo is v1.21.0
Fixes #2670 , #2664
This is achieved by:
anyOf
in the function parameter parsing utility.pydantic.TypeAdapter
to validate and serialize the input and output when the schema is aUnion
.llm_agent.input_schema = Optional[Any] = None
andllm_agent.output_schema = Optional[Any]
New test cases have been added to cover these scenarios.
Real World Example
Response that matches `ResponseMessage` schema
Testing