Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
fix(llm): sanitize Gemini tool schemas
trohitg33afa114f8e2
diff --git a/server/services/llm/schema.py b/server/services/llm/schema.pyindex 1768fafa..8b20609b 100644--- a/server/services/llm/schema.py+++ b/server/services/llm/schema.py@@ -8,22 +8,41 @@ input and lets the final provider pass remove only keywords its API rejects.from __future__ import annotations+import jsonfrom copy import deepcopyfrom typing import Any, Dict, Mapping_META_KEYWORDS = {"$schema", "$id", "$defs", "definitions"}-_GEMINI_UNSUPPORTED = {- "$comment",- "const",- "default",- "dependentSchemas",- "else",- "examples",- "if",- "patternProperties",- "then",- "unevaluatedProperties",+# Gemini's function-schema surface does not accept OpenCompany UI annotations+# such as ``displayOptions``, ``rows`` and ``placeholder``. A denylist is+# therefore unsafe: every new UI annotation could invalidate a request. Keep+# this provider pass aligned with the public ``google.genai.types.Schema``+# surface instead. References are inlined above, and ``default`` remains+# excluded for Gemini API compatibility.+_GEMINI_SCHEMA_KEYWORDS = {+ "additionalProperties",+ "anyOf",+ "description",+ "enum",+ "example",+ "format",+ "items",+ "maxItems",+ "maxLength",+ "maxProperties",+ "maximum",+ "minItems",+ "minLength",+ "minProperties",+ "minimum",+ "nullable",+ "pattern",+ "properties",+ "propertyOrdering",+ "required",+ "title",+ "type",}@@ -96,39 +115,86 @@ def _clean_provider_schema(value: Any, *, provider: str) -> Any:if not isinstance(value, dict):return value+ if provider != "gemini":+ return {+ key: _clean_provider_schema(item, provider=provider)+ for key, item in value.items()+ if key not in _META_KEYWORDS+ }+missing = object()const_value = value.get("const", missing)- cleaned = {- key: _clean_provider_schema(item, provider=provider)- for key, item in value.items()- if key not in _META_KEYWORDS- and not (provider == "gemini" and key in _GEMINI_UNSUPPORTED)- }-- if provider == "gemini":- # Gemini rejects JSON Schema ``const`` while accepting ``enum``.- # Dropping const silently widened Pydantic Literal fields, so retain- # the exact constraint through the equivalent single-value enum.- if const_value is not missing and "enum" not in cleaned:- cleaned["enum"] = [- _clean_provider_schema(const_value, provider=provider)+ cleaned: Dict[str, Any] = {}+ for key, item in value.items():+ if key in _META_KEYWORDS:+ continue+ if key not in _GEMINI_SCHEMA_KEYWORDS:+ # ``const`` is restored below as a single-value enum. Omit other+ # provider-unsupported keywords and UI-only metadata.+ continue++ if key == "properties" and isinstance(item, dict):+ # Mapping keys are user-defined property names, not JSON Schema+ # keywords. Preserve them even when a field happens to be named+ # ``default``, ``examples`` or another unsupported keyword.+ cleaned[key] = {+ name: _clean_provider_schema(child, provider=provider)+ for name, child in item.items()+ }+ elif key == "anyOf" and isinstance(item, list):+ cleaned[key] = [+ _clean_provider_schema(child, provider=provider)+ for child in item]+ elif key in {"additionalProperties", "items"} and isinstance(+ item, (dict, list)+ ):+ cleaned[key] = _clean_provider_schema(item, provider=provider)+ else:+ # Scalar annotations and values (including enum members) are not+ # schemas and must not be filtered recursively.+ cleaned[key] = item++ # Gemini rejects JSON Schema ``const`` while accepting ``enum``.+ # Dropping const silently widened Pydantic Literal fields, so retain+ # the exact constraint through the equivalent single-value enum.+ if const_value is not missing and "enum" not in cleaned:+ cleaned["enum"] = [const_value]++ # The SDK's OpenAPI-style Schema model represents enum members as+ # strings even for INTEGER, NUMBER and BOOLEAN schemas. Encode+ # non-string JSON values using their JSON lexical form so numeric+ # Pydantic Literals remain constrained without failing local SDK+ # validation.+ enum_values = cleaned.get("enum")+ if isinstance(enum_values, list):+ cleaned["enum"] = [+ item+ if isinstance(item, str)+ else json.dumps(+ item,+ ensure_ascii=False,+ separators=(",", ":"),+ sort_keys=True,+ )+ for item in enum_values+ ]- schema_type = cleaned.get("type")- if isinstance(schema_type, list) and "null" in schema_type:- non_null = [item for item in schema_type if item != "null"]- if len(non_null) == 1:- cleaned["type"] = non_null[0]- cleaned["nullable"] = True- variants = cleaned.get("anyOf")- if isinstance(variants, list):- non_null_variants = [- item- for item in variants- if not (isinstance(item, dict) and item.get("type") == "null")- ]- if len(non_null_variants) == len(variants) - 1:- cleaned["anyOf"] = non_null_variants- cleaned["nullable"] = True+ schema_type = cleaned.get("type")+ if isinstance(schema_type, list) and "null" in schema_type:+ non_null = [item for item in schema_type if item != "null"]+ if len(non_null) == 1:+ cleaned["type"] = non_null[0]+ cleaned["nullable"] = True+ variants = cleaned.get("anyOf")+ if isinstance(variants, list):+ non_null_variants = [+ item+ for item in variants+ if not (isinstance(item, dict) and item.get("type") == "null")+ ]+ if len(non_null_variants) == len(variants) - 1:+ cleaned["anyOf"] = non_null_variants+ cleaned["nullable"] = Truereturn cleaneddiff --git a/server/tests/llm/test_providers.py b/server/tests/llm/test_providers.pyindex 7dfb29b5..f226a466 100644--- a/server/tests/llm/test_providers.py+++ b/server/tests/llm/test_providers.py@@ -248,6 +248,31 @@ class TestGeminiProvider:assert contents[0]["parts"][0]["function_response"]["id"] == "call-1"assert contents[0]["parts"][1]["function_response"]["id"] == "call-2"+ def test_tool_def_compiles_numeric_enums_for_sdk_schema(self, provider):+ tool = ToolDef(+ name="allocate",+ description="Allocate memory",+ parameters={+ "type": "object",+ "properties": {+ "memory": {+ "type": "integer",+ "enum": [128, 256],+ "displayOptions": {"show": {"mode": ["advanced"]}},+ }+ },+ },+ )++ api_tool = provider._to_api_tool(tool)+ declaration = api_tool["function_declarations"][0]++ assert "parameters_json_schema" not in declaration+ assert declaration["parameters"]["properties"]["memory"] == {+ "type": "integer",+ "enum": ["128", "256"],+ }+def test_normalize_text(self, provider):part = MagicMock(thought=False, function_call=None, text="Hello")candidate = MagicMock()diff --git a/server/tests/llm/test_tool_schema.py b/server/tests/llm/test_tool_schema.pyindex a460a24b..29ff9a78 100644--- a/server/tests/llm/test_tool_schema.py+++ b/server/tests/llm/test_tool_schema.py@@ -41,13 +41,100 @@ def test_gemini_schema_normalizes_nullable_and_drops_unsupported_metadata():"type": ["string", "null"],"default": None,"examples": ["celsius"],- }+ "displayOptions": {"show": {"operation": ["convert"]}},+ "placeholder": "celsius",+ "rows": 3,+ },+ # Property names are data, not schema keywords. A+ # structure-aware filter must retain this field even though+ # the Gemini Schema model does not accept the ``default``+ # annotation keyword.+ "default": {+ "type": "string",+ "displayOptions": {"show": {"advanced": [True]}},+ },+ "tags": {+ "type": "array",+ "items": {+ "type": "string",+ "widget": "tags",+ "displayOptions": {"show": {"advanced": [True]}},+ },+ },+ "settings": {+ "type": "object",+ "additionalProperties": False,+ },},},provider="gemini",)unit = compiled["properties"]["unit"]assert unit == {"type": "string", "nullable": True}+ assert compiled["properties"]["default"] == {"type": "string"}+ assert compiled["properties"]["tags"] == {+ "type": "array",+ "items": {"type": "string"},+ }+ assert compiled["properties"]["settings"]["additionalProperties"] is False+++def test_gemini_compiled_pydantic_ui_schema_is_accepted_by_sdk():+ """Regression: real Pydantic schemas must form a valid Gemini config."""++ from typing import Literal++ from google.genai import types+ from pydantic import BaseModel, Field++ class FileModifyParams(BaseModel):+ operation: str = "write"+ content: str = Field(+ default="",+ json_schema_extra={+ "displayOptions": {"show": {"operation": ["write"]}},+ "rows": 6,+ "placeholder": "File contents",+ },+ )+ replace_all: bool = Field(+ default=False,+ json_schema_extra={+ "displayOptions": {"show": {"operation": ["edit"]}},+ "hidden": True,+ },+ )+ memory: Literal[128, 256] = 128++ parameters = compile_tool_schema(+ FileModifyParams.model_json_schema(),+ provider="gemini",+ )+ config = types.GenerateContentConfig(+ tools=[+ {+ "function_declarations": [+ {+ "name": "file_modify",+ "description": "Write or edit a file",+ "parameters": parameters,+ }+ ]+ }+ ]+ )++ declaration = config.tools[0].function_declarations[0]+ assert declaration.parameters_json_schema is None+ schema = declaration.parameters+ assert schema is not None+ assert set(schema.properties) == {+ "operation",+ "content",+ "replace_all",+ "memory",+ }+ assert schema.properties["memory"].enum == ["128", "256"]def test_gemini_schema_preserves_literals_enums_and_nested_nullable_unions():