Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion astrbot/core/agent/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,17 @@ def convert_schema(schema: dict) -> dict:
if properties:
result["properties"] = properties

if "items" in schema:
if target_type == "array":
# Gemini rejects array schemas that omit `items`, while JSON Schema
# producers in the wild (especially external MCP tools) sometimes
# leave element types unspecified. Fall back to a permissive string
# item schema so tool calling stays functional instead of failing the
# whole request with `items: missing field`.
if isinstance(schema.get("items"), dict) and schema["items"]:
result["items"] = convert_schema(schema["items"])
else:
result["items"] = {"type": "string"}
elif "items" in schema:
result["items"] = convert_schema(schema["items"])

return result
Expand Down
47 changes: 47 additions & 0 deletions tests/unit/test_tool_google_schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from astrbot.core.agent.tool import FunctionTool, ToolSet


def test_google_schema_adds_default_items_for_array_parameters_without_items():
tool = FunctionTool(
name="lookup_sources",
description="Look up sources by UUID.",
parameters={
"type": "object",
"properties": {
"source_uuids": {
"type": "array",
"description": "Source UUIDs to fetch.",
}
},
},
)

schema = ToolSet(tools=[tool]).google_schema()
source_uuids = schema["function_declarations"][0]["parameters"]["properties"][
"source_uuids"
]

assert source_uuids["type"] == "array"
assert source_uuids["items"] == {"type": "string"}


def test_google_schema_preserves_explicit_array_item_schema():
tool = FunctionTool(
name="lookup_numbers",
description="Look up integer values.",
parameters={
"type": "object",
"properties": {
"numbers": {
"type": "array",
"items": {"type": "integer", "format": "int32"},
}
},
},
)

schema = ToolSet(tools=[tool]).google_schema()
numbers = schema["function_declarations"][0]["parameters"]["properties"]["numbers"]

assert numbers["type"] == "array"
assert numbers["items"] == {"type": "integer", "format": "int32"}