feat(core): normalize nullable unions in Google tool schemas#8667
feat(core): normalize nullable unions in Google tool schemas#8667SunmiJJW wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request enhances the Google schema conversion logic in astrbot/core/agent/tool.py to handle anyOf and oneOf unions by collapsing them into a single nullable type when they contain only one non-null branch. It also marks list types containing "null" as nullable and adds corresponding unit tests. The reviewer suggested a code improvement to simplify the union collapsing logic and support single-element unions without null branches.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| if len(non_null_branches) == 1 and len(non_null_branches) < len( | ||
| converted_branches | ||
| ): | ||
| result = non_null_branches[0].copy() | ||
| result["nullable"] = True | ||
| apply_supported_fields(result, schema) | ||
| return result |
There was a problem hiding this comment.
We can simplify the condition and also support collapsing single-element anyOf/oneOf arrays (e.g., [{"type": "string"}]) by checking len(non_null_branches) == 1 and conditionally setting nullable only if there are other (null) branches in the union. Since Gemini does not support anyOf/oneOf at all, collapsing single-element unions to their underlying type is always a safe and beneficial normalization.
| if len(non_null_branches) == 1 and len(non_null_branches) < len( | |
| converted_branches | |
| ): | |
| result = non_null_branches[0].copy() | |
| result["nullable"] = True | |
| apply_supported_fields(result, schema) | |
| return result | |
| if len(non_null_branches) == 1: | |
| result = non_null_branches[0].copy() | |
| if len(converted_branches) > 1: | |
| result["nullable"] = True | |
| apply_supported_fields(result, schema) | |
| return result |
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- In the anyOf/oneOf handling, non-dict branches are silently dropped (
if isinstance(item, dict)); if the source schema ever includes literal values or refs there, you may want to preserve those branches unchanged rather than omitting them to avoid losing parts of the schema. - The new nullable-collapse logic only treats branches with
type == "null"as the null case; if you expect schemas that encode nullability differently (e.g., viaenum: [null, ...]or$refto a null type), consider whether those should also be normalized for consistency.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In the anyOf/oneOf handling, non-dict branches are silently dropped (`if isinstance(item, dict)`); if the source schema ever includes literal values or refs there, you may want to preserve those branches unchanged rather than omitting them to avoid losing parts of the schema.
- The new nullable-collapse logic only treats branches with `type == "null"` as the null case; if you expect schemas that encode nullability differently (e.g., via `enum: [null, ...]` or `$ref` to a null type), consider whether those should also be normalized for consistency.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Modifications / 改动点
This PR improves Google/Gemini tool schema conversion for common nullable JSON Schema unions.
本 PR 改进 Google/Gemini 工具 schema 转换,对常见 nullable JSON Schema union 做兼容归一化。
anyOf/oneOfforms such as[{type: string}, {type: null}]into a typed schema withnullable: true.type: ["string", "null"]schemas asnullable: truewhile keeping the selected non-null type.descriptionandenum.defaultvalues in the converted Google schema.tests/unit/test_tool_google_schema.py.Screenshots or Test Results / 运行截图或测试结果
Verification:
Smoke output for a nullable recency enum:
Duplicate check:
Checklist / 检查清单
😊 No user-facing feature is added; this checklist item is N/A.
/ 未新增面向用户的功能;此项不适用。
👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
/ 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。
🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in
requirements.txtandpyproject.toml./ 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到
requirements.txt和pyproject.toml文件相应位置。😮 My changes do not introduce malicious code.
/ 我的更改没有引入恶意代码。
Summary by Sourcery
Normalize nullable union types when converting JSON Schemas to Google/Gemini tool schemas.
Enhancements:
Tests: